《GDI+程序设计》笔记8
解决不寻常操作影响鼠标事件捕获的办法:
监视WM_CAPTURECHANGED事件,窗口在鼠标捕获状态改变时触发这个消息
Form1中定义一个内部类,处理将响应WM_CAPTURECHANGED消息的低级Windows过程

Class CaptureChangedWindow:NaticeWindow
{
public CaptureChanged OnCaptureChanged;
protected override void WndProc(ref Message m)
{
if(m.Msg==533)//WM_CAPTURECHANGED
{
OnCaptureChanged()
}
base.WndProc(ref m);
}
CaptureChangedWindow ccw=new CaptureChangedWindow();
ccw.assignHandle(Handle);//利用Form1实例所包含的相同窗口句柄初始化了ccw,被发送到Form1并通常会触发事件的消息,还会被发送到ccw在覆盖的WndProc()方法中进行处理
AssignHandle说明
WndProc 截取向 handle 参数发送的窗口消息。使用 ReleaseHandle 可将句柄的窗口过程重置为默认窗口过程。
AssignHandle 方法调用 OnHandleChange 方法来指示 Handle 属性的值已更改。
注意 分配的句柄不能位于另一应用程序进程中。
[C#]
Code
// NativeWindow class to listen to operating system messages.
public class MyNativeWindowListener: NativeWindow{
// Constant value was found in the "windows.h" header file.
private const int WM_ACTIVATEAPP = 0x001C;
private Form1 parent;
public MyNativeWindowListener(Form1 parent){
parent.HandleCreated += new EventHandler(this.OnHandleCreated);
parent.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
this.parent = parent;
}
// Listen for the control's window creation and then hook into it.
internal void OnHandleCreated(object sender, EventArgs e){
// Window is now created, assign handle to NativeWindow.
AssignHandle(((Form1)sender).Handle);
}
internal void OnHandleDestroyed(object sender, EventArgs e) {
// Window was destroyed, release hook.
ReleaseHandle();
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m){
// 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;
}
base.WndProc(ref m);
}
}

// NativeWindow class to listen to operating system messages.
public class MyNativeWindowListener: NativeWindow{
// Constant value was found in the "windows.h" header file.
private const int WM_ACTIVATEAPP = 0x001C;
private Form1 parent;
public MyNativeWindowListener(Form1 parent){
parent.HandleCreated += new EventHandler(this.OnHandleCreated);
parent.HandleDestroyed+= new EventHandler(this.OnHandleDestroyed);
this.parent = parent;
}
// Listen for the control's window creation and then hook into it.
internal void OnHandleCreated(object sender, EventArgs e){
// Window is now created, assign handle to NativeWindow.
AssignHandle(((Form1)sender).Handle);
}
internal void OnHandleDestroyed(object sender, EventArgs e) {
// Window was destroyed, release hook.
ReleaseHandle();
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m){
// 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;
}
base.WndProc(ref m);
}
}