CreateParams クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


CreateParams の情報を使用して、コントロールの初期状態および外観についての情報を渡すことができます。Control から派生したほとんどのコントロールは、CreateParams プロパティをオーバーライドして、適切な値を渡したり、CreateParams に追加情報を含めたりします。
コントロール パラメータの作成の詳細については、MSDN ライブラリにあるプラットフォーム SDK ドキュメントの CreateWindow 関数、CreateWindowEx 関数、CREATESTRUCT 構造体の各トピックを参照してください。
![]() |
---|
Style プロパティ、ExStyle プロパティ、および ClassStyle プロパティを設定するときに使用する定数は、Winuser.h ヘッダー ファイルの中に定義されています。このファイルは、プラットフォーム SDK または Visual Studio .NET によってインストールされます。 |

Button から派生した MyIconButton という名前のクラスを作成し、ボタンにイメージではなくアイコンを表示するために必要な実装を提供するコード例を、次に示します。CreateParams プロパティが拡張され、ボタンに Image ではなく Icon を表示するための値が Style プロパティに追加されます。
import System.*; import System.Drawing.*; import System.Windows.Forms.*; import System.Runtime.InteropServices.*; import System.Diagnostics.*; import System.IO.*; import System.Security.Permissions.*; /** @attribute SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode) */ public class MyIconButton extends Button { private Icon icon; public MyIconButton() { // Set the button's FlatStyle property. set_FlatStyle(get_FlatStyle().System); } //MyIconButton public MyIconButton(Icon ButtonIcon) { // Assign the icon to the private field. this.icon = ButtonIcon; // Size the button to 4 pixels larger than the icon. this.set_Height(icon.get_Height() + 4); this.set_Width(icon.get_Width() + 4); } //MyIconButton /** @property */ protected CreateParams get_CreateParams() { // Extend the CreateParams property of the Button class. CreateParams cp = super.get_CreateParams(); // Update the button Style. cp.set_Style(cp.get_Style() | 0x40); // BS_ICON value return cp; } //get_CreateParams /** @property */ public Icon get_Icon() { return icon; } //get_Icon /** @property */ public void set_Icon(Icon value) { icon = value; UpdateIcon(); // Size the button to 4 pixels larger than the icon. this.set_Height(icon.get_Height() + 4); this.set_Width(icon.get_Width() + 4); } //set_Icon protected void OnHandleCreated(EventArgs e) { super.OnHandleCreated(e); // Update the icon on the button if there is currently an icon assigned // to the icon field. if (icon != null) { UpdateIcon(); } } //OnHandleCreated private void UpdateIcon() { IntPtr iconHandle = IntPtr.Zero; // Get the icon's handle. if (icon != null) { iconHandle = icon.get_Handle(); } // Send Windows the message to update the button. SendMessage(get_Handle(), 0x00F7 /*BM_SETIMAGE value*/, 1 /*IMAGE_ICON value*/, (iconHandle.ToInt32())); } //UpdateIcon // Import the SendMessage method of the User32 DLL. /** @attribute DllImport("user32.dll", CharSet = CharSet.Auto) */ public static native IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); } //MyIconButton
標準の Button コントロールのインスタンスと、上記の例で作成した派生コントロール MyIconButton のインスタンスを作成するコード例を、次に示します。この例では、アプリケーションと同じ場所に、Default.ico という Icon ファイルが格納されている必要があります。アプリケーションが起動すると、Default アイコンが MyIconButton ボタン上に表示されます。Default アイコンが存在しない場合、ボタンの表面は空白になります。標準の Button をクリックすると、OpenFileDialog ボックスが表示され、MyIconButton 上に表示する新しいアイコンを選択できるようになります。
public class MyApplication extends Form { private MyIconButton myIconButton; private Button stdButton; private OpenFileDialog openDlg; public static void main(String[] args) { Application.Run(new MyApplication()); } //main public MyApplication() { try { // Create the button with the default icon. myIconButton = new MyIconButton(new Icon(Application. get_StartupPath() + "\\Default.ico")); } catch (System.Exception ex) { // If the default icon does not exist, // create the button without an icon. myIconButton = new MyIconButton(); Debug.WriteLine(ex.ToString()); } finally { stdButton = new Button(); // Add the Click event handlers. myIconButton.add_Click(new EventHandler(this.myIconButton_Click)); stdButton.add_Click(new EventHandler(this.stdButton_Click)); // Set the location, text and width of the standard button. stdButton.set_Location(new Point(myIconButton.get_Location(). get_X(), myIconButton.get_Location().get_Y() + myIconButton.get_Height() + 20)); stdButton.set_Text("Change Icon"); stdButton.set_Width(100); // Add the buttons to the Form. this.get_Controls().Add(stdButton); this.get_Controls().Add(myIconButton); } } //MyApplication private void myIconButton_Click(Object Sender, EventArgs e) { // Make sure MyIconButton works. MessageBox.Show("MyIconButton was clicked!"); } //myIconButton_Click private void stdButton_Click(Object Sender, EventArgs e) { // Use an OpenFileDialog to allow the user to assign a new image to // the derived button. openDlg = new OpenFileDialog(); openDlg.set_InitialDirectory(Application.get_StartupPath()); openDlg.set_Filter("Icon files (*.ico)|*.ico"); openDlg.set_Multiselect(false); openDlg.ShowDialog(); if (!(openDlg.get_FileName().Equals(""))) { myIconButton.set_Icon(new Icon(openDlg.get_FileName())); } } //stdButton_Click } //MyApplication

System.Windows.Forms.CreateParams


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- CreateParams クラスのページへのリンク