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


このクラスは、ウィンドウ クラスの作成と登録を自動的に管理します。
ウィンドウは、ウィンドウ ハンドルが関連付けられている場合は、ガベージ コレクションの対象にはなりません。適切なガベージ コレクションを行うには、ハンドルを DestroyHandle を使用して手動で破棄するか、ReleaseHandle を使用して解放する必要があります。
NativeWindow クラスには、Handle、CreateHandle、AssignHandle、DestroyHandle、ReleaseHandle など、ハンドルを管理するためのプロシージャやメソッドがあります。

オペレーティング システムのウィンドウ メッセージをウィンドウ プロシージャで受け取り、特定のオペレーティング システム ウィンドウ クラス名を指定してウィンドウを作成するコード例を次に示します。この例では、これを行うために、NativeWindow の継承クラスを 2 つ作成しています。
MyNativeWindowListener クラスは、コンストラクタに渡されたフォームのウィンドウ プロシージャにフックし、WndProc メソッドをオーバーライドして WM_ACTIVATEAPP ウィンドウ メッセージを受け取ります。このクラスでは、NativeWindow が使用するウィンドウ ハンドルを識別するために AssignHandle メソッドと ReleaseHandle メソッドを使用する方法を示しています。このハンドルは、Control.HandleCreated イベントと Control.HandleDestroyed イベントを基に割り当てられます。WM_ACTIVATEAPP ウィンドウ メッセージが受信されると、このクラスは form1ApplicationActivated メソッドを呼び出します。
MyNativeWindow クラスは、ClassName が BUTTON に設定された新しいウィンドウを作成します。このクラスでは、CreateHandle メソッドを使用し、WndProc メソッドをオーバーライドして、受信されたウィンドウ メッセージを受け取る方法を示しています。
using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::Runtime::InteropServices; ref class MyNativeWindowListener; ref class MyNativeWindow; // Summary description for Form1. ref class Form1: public System::Windows::Forms::Form { private: MyNativeWindowListener^ nwl; MyNativeWindow^ nw; internal: void ApplicationActived( bool ApplicationActivated ) { // The application has been activated or deactivated System::Diagnostics::Debug::WriteLine( "Application Active = {0}", ApplicationActivated.ToString() ); } public: Form1(); }; // NativeWindow class to listen to operating system messages. ref class MyNativeWindowListener: public NativeWindow { private: // Constant value was found in the S"windows.h" header file. literal int WM_ACTIVATEAPP = 0x001C; Form1^ parent; public: MyNativeWindowListener( Form1^ parent ) { parent->HandleCreated += gcnew EventHandler( this, &MyNativeWindowListener::OnHandleCreated ); parent->HandleDestroyed += gcnew EventHandler( this, &MyNativeWindowListener::OnHandleDestroyed ); this->parent = parent; } internal: // Listen for the control's window creation and then hook into it. void OnHandleCreated( Object^ sender, EventArgs^ /*e*/ ) { // Window is now created, assign handle to NativeWindow. AssignHandle( (dynamic_cast<Form1^>(sender))->Handle ); } void OnHandleDestroyed( Object^ /*sender*/, EventArgs^ /*e*/ ) { // Window was destroyed, release hook. ReleaseHandle(); } protected: virtual void WndProc( Message %m ) override { // Listen for operating system messages switch ( m.Msg ) { case WM_ACTIVATEAPP: // Notify the form that this message was received. // Application is activated or deactivated, // based upon the WParam parameter. parent->ApplicationActived( ((int)m.WParam != 0) ); break; } NativeWindow::WndProc( m ); } }; // 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 ); } }; Form1::Form1() { this->Size = System::Drawing::Size( 300, 300 ); this->Text = "Form1"; nwl = gcnew MyNativeWindowListener( this ); nw = gcnew MyNativeWindow( this ); } // The main entry point for the application. [STAThread] int main() { Application::Run( gcnew Form1 ); }
package NativeWindowApplication; import System.*; import System.Drawing.*; import System.Windows.Forms.*; import System.Runtime.InteropServices.*; import System.Security.Permissions.*; // Summary description for Form1. public class Form1 extends System.Windows.Forms.Form { private MyNativeWindowListener nwl; private MyNativeWindow nw; void ApplicationActived(boolean applicationActivated) { // The application has been activated or deactivated System.Diagnostics.Debug.WriteLine("Application Active = " + Convert.ToString(applicationActivated)); } //ApplicationActived public Form1() { this.set_Size(new System.Drawing.Size(300, 300)); this.set_Text("Form1"); nwl = new MyNativeWindowListener(this); nw = new MyNativeWindow(this); } //Form1 // The main entry point for the application. /** @attribute STAThread() */ public static void main(String[] args) { Application.Run(new Form1()); } //main } //Form1 // NativeWindow class to listen to operating system messages. /** @attribute SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode) */ public class MyNativeWindowListener extends NativeWindow { // Constant value was found in the "windows.h" header file. private int WM_ACTIVATEAPP = 0x1C; private Form1 parent; public MyNativeWindowListener(Form1 parent) { parent.add_HandleCreated(new EventHandler(this.OnHandleCreated)); parent.add_HandleDestroyed(new EventHandler(this.OnHandleDestroyed)); this.parent = parent; } //MyNativeWindowListener // Listen for the control's window creation and then hook into it. void OnHandleCreated(Object sender, EventArgs e) { // Window is now created, assign handle to NativeWindow. AssignHandle(((Form1)sender).get_Handle()); } //OnHandleCreated void OnHandleDestroyed(Object sender, EventArgs e) { // Window was destroyed, release hook. ReleaseHandle(); } //OnHandleDestroyed protected void WndProc(Message m) { // Listen for operating system messages if (m.get_Msg() == WM_ACTIVATEAPP) { // Notify the form that this message was received. // Application is activated or deactivated, // based upon the WParam parameter. parent.ApplicationActived(m.get_WParam().ToInt32() != 0); } super.WndProc(m); } //WndProc } //MyNativeWindowListener // 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


System.MarshalByRefObject
System.Windows.Forms.NativeWindow


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


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


これは、コンパイラが提供する既定の NativeWindow コンストラクタです。NativeWindow クラスには、アプリケーション ドメイン全体におけるメッセージ ハンドラとハッシュ テーブルを初期化する静的コンストラクタもあります。

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


NativeWindow プロパティ
NativeWindow メソッド

名前 | 説明 | |
---|---|---|
![]() | AssignHandle | ウィンドウにハンドルを割り当てます。 |
![]() | CreateHandle | 作成パラメータを指定してウィンドウとそのハンドルを作成します。 |
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。) |
![]() | DefWndProc | ウィンドウに関連付けられている既定のウィンドウ プロシージャを呼び出します。 |
![]() | DestroyHandle | ウィンドウとそのハンドルを破棄します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | FromHandle | 指定したハンドルに関連付けられているウィンドウを取得します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ReleaseHandle | ウィンドウに関連付けられているハンドルを解放します。 |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | オーバーライドされます。 ウィンドウに関連付けられているリソースを解放します。 |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnHandleChange | ウィンドウのハンドルが変更されたときに呼び出される通知メソッドを指定します。 |
![]() | OnThreadException | 派生クラスでオーバーライドされた場合、未処理のスレッド例外を管理します。 |
![]() | WndProc | ウィンドウに関連付けられている既定のウィンドウ プロシージャを呼び出します。 |

NativeWindow メンバ
ウィンドウ ハンドルとウィンドウ プロシージャの低水準のカプセル化を提供します。
NativeWindow データ型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | AssignHandle | ウィンドウにハンドルを割り当てます。 |
![]() | CreateHandle | 作成パラメータを指定してウィンドウとそのハンドルを作成します。 |
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。) |
![]() | DefWndProc | ウィンドウに関連付けられている既定のウィンドウ プロシージャを呼び出します。 |
![]() | DestroyHandle | ウィンドウとそのハンドルを破棄します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | FromHandle | 指定したハンドルに関連付けられているウィンドウを取得します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ReleaseHandle | ウィンドウに関連付けられているハンドルを解放します。 |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | オーバーライドされます。 ウィンドウに関連付けられているリソースを解放します。 |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnHandleChange | ウィンドウのハンドルが変更されたときに呼び出される通知メソッドを指定します。 |
![]() | OnThreadException | 派生クラスでオーバーライドされた場合、未処理のスレッド例外を管理します。 |
![]() | WndProc | ウィンドウに関連付けられている既定のウィンドウ プロシージャを呼び出します。 |

- NativeWindowのページへのリンク