ウィンドウにハンドルを割り当てます。
名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
'宣言
Public Sub AssignHandle ( _
handle As IntPtr _
)
'使用
Dim instance As NativeWindow
Dim handle As IntPtr
instance.AssignHandle(handle)
public void AssignHandle (
IntPtr handle
)
public:
void AssignHandle (
IntPtr handle
)
public void AssignHandle (
IntPtr handle
)
public function AssignHandle (
handle : IntPtr
)
パラメータ
- handle
ウィンドウに割り当てるハンドル。
例外
例外の種類 | 条件 |
---|---|
ウィンドウには既にハンドルが割り当てられています。 |
|
関連付けられたネイティブ ウィンドウのウィンドウ プロシージャを取得できませんでした。 |
解説
WndProc は、handle パラメータに送られるウィンドウ メッセージを受け取ります。ハンドルのウィンドウ プロシージャを既定のウィンドウ プロシージャにリセットするには、ReleaseHandle を使用します。
AssignHandle メソッドは、Handle プロパティの値が変更されたことを示すために、OnHandleChange メソッドを呼び出します。
注意
別のアプリケーション プロセスのハンドルを割り当てることはできません。
使用例
ウィンドウ プロシージャでオペレーティング システムのウィンドウ メッセージを受け取る方法を次のコード例に示します。この例では、これを達成するために、NativeWindow から継承するクラスを作成します。
MyNativeWindowListener
クラスは、コンストラクタに渡されたフォームのウィンドウ プロシージャにフックし、WndProc メソッドをオーバーライドして WM_ACTIVATEAPP
ウィンドウ メッセージを受け取ります。このクラスでは、NativeWindow が使用するウィンドウ ハンドルを識別するために AssignHandle メソッドと ReleaseHandle メソッドを使用する方法を示しています。このハンドルは、Control.HandleCreated イベントと Control.HandleDestroyed イベントを基に割り当てられます。WM_ACTIVATEAPP
ウィンドウ メッセージが受信されると、このクラスは form1``ApplicationActivated
メソッドを呼び出します。
このコードは、NativeWindow クラスの概要で紹介されている例からの抜粋です。簡略にするため、コードの一部は示されていません。コード全体については、NativeWindow を参照してください。
// 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 );
}
};
// 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
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
参照
関連項目
NativeWindow クラス
NativeWindow メンバ
System.Windows.Forms 名前空間
Handle
CreateHandle
DestroyHandle
ReleaseHandle
Finalize
OnHandleChange