protected
virtual
void
WndProc(
ref Message m
);
参数 m
与当前 Windows 消息相关联的 Message。
备注
当窗口消息发送到窗口的句柄时,将调用此方法。对继承者的说明: 重写此方法以实现特定消息处理。对未处理的消息调用 base.WndProc。
示例
[Visual Basic, C#, C++] 下面的示例说明了如何利用窗口过程截获操作系统窗口消息。此示例将创建一个从 NativeWindow 继承的类来完成此操作。
[Visual Basic, C#, C++] MyNativeWindowListener 类与窗体中传递给构造函数的窗口过程挂钩,并重写 WndProc 方法来截获 WM_ACTIVATEAPP 窗口消息。此类说明了如何使用 AssignHandle 和 ReleaseHandle 方法来标识 NativeWindow 将要使用的窗口句柄。句柄根据 Control.HandleCreated 和 Control.HandleDestroyed 事件进行分配。当收到 WM_ACTIVATEAPP 窗口消息时,该类会调用 form1 ApplicationActivated 方法。 [Visual Basic, C#, C++] 这段代码摘录自 NativeWindow 类概述中显示的示例。为了简洁,一些代码没有显示。有关全部代码的列表,请参见 NativeWindow。
ref Message m
);
参数 m
与当前 Windows 消息相关联的 Message。
备注
当窗口消息发送到窗口的句柄时,将调用此方法。对继承者的说明: 重写此方法以实现特定消息处理。对未处理的消息调用 base.WndProc。
示例
[Visual Basic, C#, C++] 下面的示例说明了如何利用窗口过程截获操作系统窗口消息。此示例将创建一个从 NativeWindow 继承的类来完成此操作。
[Visual Basic, C#, C++] MyNativeWindowListener 类与窗体中传递给构造函数的窗口过程挂钩,并重写 WndProc 方法来截获 WM_ACTIVATEAPP 窗口消息。此类说明了如何使用 AssignHandle 和 ReleaseHandle 方法来标识 NativeWindow 将要使用的窗口句柄。句柄根据 Control.HandleCreated 和 Control.HandleDestroyed 事件进行分配。当收到 WM_ACTIVATEAPP 窗口消息时,该类会调用 form1 ApplicationActivated 方法。 [Visual Basic, C#, C++] 这段代码摘录自 NativeWindow 类概述中显示的示例。为了简洁,一些代码没有显示。有关全部代码的列表,请参见 NativeWindow。
1
//
NativeWindow class to listen to operating system messages.
2 public class MyNativeWindowListener: NativeWindow {
3
4 // Constant value was found in the "windows.h" header file.
5 private const int WM_ACTIVATEAPP = 0x001C;
6
7 private Form1 parent;
8
9 public MyNativeWindowListener(Form1 parent){
10
11 parent.HandleCreated += new EventHandler(this.OnHandleCreated);
12 parent.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
13 this.parent = parent;
14 }
15
16 // Listen for the control's window creation and then hook into it.
17 internal void OnHandleCreated(object sender, EventArgs e){
18 // Window is now created, assign handle to NativeWindow.
19 AssignHandle(((Form1)sender).Handle);
20 }
21 internal void OnHandleDestroyed(object sender, EventArgs e) {
22 // Window was destroyed, release hook.
23 ReleaseHandle();
24 }
25[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
26 protected override void WndProc(ref Message m){
27 // Listen for operating system messages
28
29 switch (m.Msg){
30 case WM_ACTIVATEAPP:
31
32 // Notify the form that this message was received.
33 // Application is activated or deactivated,
34 // based upon the WParam parameter.
35 parent.ApplicationActived(((int)m.WParam != 0));
36
37 break;
38 }
39 base.WndProc(ref m);
40 }
41}
42
2 public class MyNativeWindowListener: NativeWindow {
3
4 // Constant value was found in the "windows.h" header file.
5 private const int WM_ACTIVATEAPP = 0x001C;
6
7 private Form1 parent;
8
9 public MyNativeWindowListener(Form1 parent){
10
11 parent.HandleCreated += new EventHandler(this.OnHandleCreated);
12 parent.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
13 this.parent = parent;
14 }
15
16 // Listen for the control's window creation and then hook into it.
17 internal void OnHandleCreated(object sender, EventArgs e){
18 // Window is now created, assign handle to NativeWindow.
19 AssignHandle(((Form1)sender).Handle);
20 }
21 internal void OnHandleDestroyed(object sender, EventArgs e) {
22 // Window was destroyed, release hook.
23 ReleaseHandle();
24 }
25[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
26 protected override void WndProc(ref Message m){
27 // Listen for operating system messages
28
29 switch (m.Msg){
30 case WM_ACTIVATEAPP:
31
32 // Notify the form that this message was received.
33 // Application is activated or deactivated,
34 // based upon the WParam parameter.
35 parent.ApplicationActived(((int)m.WParam != 0));
36
37 break;
38 }
39 base.WndProc(ref m);
40 }
41}
42