NativeWindow.CreateHandle メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)



cp パラメータは、ウィンドウとそのハンドルを作成するために Win32 の ネイティブな CreateWindowEx メソッドに渡される値を指定します。
ClassName フィールドが null 参照 (Visual Basic では Nothing) ではない場合、新しく作成されるウィンドウ ハンドルは、指定したクラスから継承します。たとえば、ClassName を BUTTON に設定した場合、Win32 の BUTTON ウィンドウ クラスに基づいた新しいウィンドウが作成されます。ClassName オブジェクトの Param プロパティは、null 参照 (Visual Basic では Nothing) または構造体として宣言されたクラスのインスタンスへの参照のいずれかである必要があります。
このコードは、NativeWindow クラスの概要で紹介されている例からの抜粋です。簡略にするため、コードの一部は示されていません。コード全体については、NativeWindow を参照してください。
![]() |
---|

特定のオペレーティング システム ウィンドウ クラス名を使用してウィンドウを作成するコード例を次に示します。この例では、これを達成するために、NativeWindow から継承するクラスを作成します。
MyNativeWindow クラスは、ClassName が BUTTON に設定された新しいウィンドウを作成します。これは Win32 ボタン ウィンドウを作成します。ボタンの位置とサイズは、追加のウィンドウ スタイルと併せて設定されます。このクラスは、CreateHandle メソッドを使用し、WndProc メソッドをオーバーライドして、受信されたウィンドウ メッセージを受け取る方法を示しています。この例では WM_ACTIVATEAPP メッセージを処理していますが、実際のプログラムでは、作成した型に対応するウィンドウ メッセージを使用できます。
![]() |
---|
コントロールの種類によっては、ウィンドウではなく、ウィンドウの親にウィンドウ メッセージを送信します。詳細については、Windows プラットフォーム SDK を参照してください。 |
// MyNativeWindow class to create a window given a class name. ref class MyNativeWindow: public NativeWindow { private: // Constant values were found in the S"windows.h" header file. literal int WS_CHILD = 0x40000000,WS_VISIBLE = 0x10000000,WM_ACTIVATEAPP = 0x001C; int windowHandle; public: MyNativeWindow( Form^ parent ) { CreateParams^ cp = gcnew CreateParams; // Fill in the CreateParams details. cp->Caption = "Click here"; cp->ClassName = "Button"; // Set the position on the form cp->X = 100; cp->Y = 100; cp->Height = 100; cp->Width = 100; // Specify the form as the parent. cp->Parent = parent->Handle; // Create as a child of the specified parent cp->Style = WS_CHILD | WS_VISIBLE; // Create the actual window this->CreateHandle( cp ); } protected: // Listen to when the handle changes to keep the variable in sync virtual void OnHandleChange() override { windowHandle = (int)this->Handle; } virtual void WndProc( Message % m ) override { // Listen for messages that are sent to the button window. Some messages are sent // to the parent window instead of the button's window. switch ( m.Msg ) { case WM_ACTIVATEAPP: // Do something here in response to messages break; } NativeWindow::WndProc( m ); } };
// MyNativeWindow class to create a window given a class name. /** @attribute SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode) */ public class MyNativeWindow extends NativeWindow { // Constant values were found in the "windows.h" header file. private int WS_CHILD = 0x40000000; private int WS_VISIBLE = 0x10000000; private int WM_ACTIVATEAPP = 0x1C; private int windowHandle; public MyNativeWindow(Form parent) { CreateParams cp = new CreateParams(); // Fill in the CreateParams details. cp.set_Caption("Click here"); cp.set_ClassName("Button"); // Set the position on the form cp.set_X(100); cp.set_Y(100); cp.set_Height(100); cp.set_Width(100); // Specify the form as the parent. cp.set_Parent(parent.get_Handle()); // Create as a child of the specified parent cp.set_Style(WS_CHILD | WS_VISIBLE); // Create the actual window this.CreateHandle(cp); } //MyNativeWindow // Listen to when the handle changes to keep the variable in sync protected void OnHandleChange() { windowHandle = this.get_Handle().ToInt32(); } //OnHandleChange protected void WndProc(Message m) { // Listen for messages that are sent to the button window. // Some messages are sent to the parent window // instead of the button's window. if (m.get_Msg() == WM_ACTIVATEAPP) { // Do something here in response to messages } super.WndProc(m); } //WndProc } //MyNativeWindow


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からNativeWindow.CreateHandle メソッドを検索する場合は、下記のリンクをクリックしてください。

- NativeWindow.CreateHandle メソッドのページへのリンク