uMsgNotify = WinApi.RegisterWindowMessage("SHELLHOOK");
WinApi.RegisterShellHookWindow(this.Handle);
在我的表单构造函数中
这在我重写的 WndProc 中:
protected override void WndProc(ref System.Windows.Forms.Message m)
{
IntPtr handle;
if (m.Msg == uMsgNotify)
{
switch (m.WParam.ToInt32())
{
case WinApi.HSHELL_WINDOWCREATED:
handle = m.LParam;
string windowName = GetWindowName(handle);
MessageBox.Show(windowName+" "+handle.ToString());
break;
case WinApi.HSHELL_WINDOWDESTROYED:
handle = m.LParam;
MessageBox.Show(handle.ToString());
break;
}
}
base.WndProc(ref m);
}
因此,当默认 Windows shell 为 explorer.exe 时,此 Win Form 工作正常。我可以获取所有事件所有创建和销毁的窗口及其名称。当我将我的 app.exe 设置为默认 Windows Shell 时。它不接收消息。
我是否必须使用 dll 函数注入,单独的 dll 文件进行挂钩?我试图获得更简单和更容易的解决方案。
有任何想法吗?
谢谢