有人可以解释一下为什么我WndProc
下面实现的方法没有收到任何消息吗?如果我把这个类放在下面的 WinForms 应用程序中并传入该应用程序的句柄,WndProc
则会按预期接收消息。但是,使用下面返回的 IntPtrGetForegroundWindow()
不会产生相同的结果。(FWIW,我的代码设置为GetForegroundWindow()
在我的应用程序隐藏时执行,所以我确定 IntPtr 指的是外部应用程序。)我的目标是监视来自外部应用程序的某些事件。
public class MyNativeWindow : NativeWindow
{
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
private static extern IntPtr GetForegroundWindow();
public MyNativeWindow()
{
this.AssignHandle(GetForegroundWindow());
}
// Never called... I set a breakpoint
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}
}