NativeWindow.OnHandleChange 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定当更改窗口句柄时所调用的通知方法。
protected:
virtual void OnHandleChange();
protected virtual void OnHandleChange ();
abstract member OnHandleChange : unit -> unit
override this.OnHandleChange : unit -> unit
Protected Overridable Sub OnHandleChange ()
示例
下面的代码示例演示如何创建具有特定操作系统窗口类名称的窗口。 该示例创建一个从 NativeWindow 中继承的类来完成此操作。 该示例还演示了重写 OnHandleChange 更改时 Handle 要通知的方法。
该MyNativeWindow
类将创建一个新窗口,该窗口设置为 BUTTON
ClassName 。 这将创建 Win32 按钮窗口。 设置按钮的位置和大小,并指定其他窗口样式。 该类演示如何使用 CreateHandle 该方法并重写 WndProc 该方法来截获收到的窗口消息。 尽管该示例查找WM_ACTIVATEAPP消息,但可以在实际程序中用特定于所创建类型的窗口消息替换此消息。
此代码是类概述中显示的 NativeWindow 示例的摘录。 出于简洁起见,不显示某些代码。 请参阅 NativeWindow 整个代码列表。
备注
某些控件类型将窗口消息发送到窗口父级而不是窗口。 有关详细信息,请参阅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.
internal class MyNativeWindow : NativeWindow
{
// Constant values were found in the "windows.h" header file.
private const int WS_CHILD = 0x40000000,
WS_VISIBLE = 0x10000000,
WM_ACTIVATEAPP = 0x001C;
private int windowHandle;
public MyNativeWindow(Form parent)
{
CreateParams cp = new 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);
}
// Listen to when the handle changes to keep the variable in sync
protected override void OnHandleChange()
{
windowHandle = (int)this.Handle;
}
protected override void WndProc(ref 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.
switch (m.Msg)
{
case WM_ACTIVATEAPP:
// Do something here in response to messages
break;
}
base.WndProc(ref m);
}
}
' MyNativeWindow class to create a window given a class name.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Friend Class MyNativeWindow
Inherits NativeWindow
' Constant values were found in the "windows.h" header file.
Private Const WS_CHILD As Integer = &H40000000, _
WS_VISIBLE As Integer = &H10000000, _
WM_ACTIVATEAPP As Integer = &H1C
Private windowHandle As Integer
Public Sub New(ByVal parent As Form)
Dim cp As CreateParams = New 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 Or WS_VISIBLE
' Create the actual window
Me.CreateHandle(cp)
End Sub
' Listen to when the handle changes to keep the variable in sync
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub OnHandleChange()
windowHandle = Me.Handle.ToInt32()
End Sub
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for messages that are sent to the button window. Some messages are sent
' to the parent window instead of the button's window.
Select Case (m.Msg)
Case WM_ACTIVATEAPP
' Do something here in response to messages
End Select
MyBase.WndProc(m)
End Sub
End Class
注解
当属性的值 Handle 发生更改时,将调用此方法。
继承者说明
重写此方法以跟踪对窗口句柄所做的更改。