我使用RegisterWindowMessage(「SHELLHOOK」)註冊窗口創建和銷燬事件。我的代碼是用C#編寫的。代碼在我調試代碼時完美工作。但是當我不經調試運行該程序時,我沒有像調試時那樣獲取WndProc消息。 Windows阻止掛鉤?掛鉤接收窗口創建和銷燬通知
class ShellHook:NativeWindow
{
public ShellHook(IntPtr hWnd)
{
if (Win32.RegisterShellHookWindow(this.Handle))
{
WM_ShellHook = Win32.RegisterWindowMessage("SHELLHOOK");
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ShellHook)
{
switch((ShellEvents)m.WParam)
{
//m.lparam
case ShellEvents.HSHELL_WINDOWCREATED:
if (windowCreatedEvent != null)
{
windowCreatedEvent(m.LParam);
}
break;
case ShellEvents.HSHELL_WINDOWDESTROYED:
if (windowDestroyedEvent != null)
{
windowDestroyedEvent(m.LParam);
}
break;
}
}
base.WndProc(ref m);
}
}
這是我如何啓動我的wpf應用程序。
public partial class App : Application
{
MainWindow mainWindowView;
public App()
{
Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
mainWindowView = new MainWindow();
MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
mainWindowView.DataContext = mainWindowViewModel;
mainWindowView.ShowDialog();
}
}
我MainWindowViewModel構造如下:
public MainWindowViewModel()
{
EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
ShellHook shellHookObject = new ShellHook(f.Handle);
shellHookObject.windowCreatedEvent += shellHookObject_windowCreatedEvent;
shellHookObject.windowDestroyedEvent += shellHookObject_windowDestroyedEvent;
}
郵政編碼,這將有幫助 –
您是否正在創建一個外殼擴展?你在調用'RegisterShellHookWindow'函數嗎?你是否檢查所有你要求出錯的函數的返回碼? –
我沒有創建一個shell擴展。我只是想做一個任務欄,因爲我需要窗口創建和銷燬的通知,所以我調用了RegisterShellHookWindow函數。但有趣的部分是,我在調試時收到通知,但沒有在沒有調試的情況下運行應用程序時收到通知。 –