WndProc(ref Message m)方法
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。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
参数 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
// Constant value was found in the "windows.h" header file.
5
private const int WM_ACTIVATEAPP = 0x001C;
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
private Form1 parent;
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
public MyNativeWindowListener(Form1 parent){
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
parent.HandleCreated += new EventHandler(this.OnHandleCreated);
12
parent.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
13
this.parent = parent;
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
switch (m.Msg){
30
case WM_ACTIVATEAPP:
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
break;
38
}
39
base.WndProc(ref m);
40
}
41
}
42![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
不进则退、与时俱进