在 WinApi 中捕获 window 的关闭事件
Catching closing event of a window in WinApi
基本上,我想在用户单击 X(关闭)特定 window 后执行某些操作。
这是我的方法:
HWND handle;
LRESULT WINAPI procedure(int code, WPARAM wParam, LPARAM, lParam){
CWPSTRUCT* j = (CWPSTRUCT*) lParam;
WPSTRUCT* w = (CWPSTRUCT*) wParam;
if(w->message == WM_SYSCOMMAND && j->message == SC_CLOSE){
//do something
}
}
Void doSomething(){
HINSTANCE hDLL = LoadLibrary("User32.dll");
HWND WINAPI FindWindow(_In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowName);
HHOOK WINAPI SetWindowsHookEx(_In_ int idHook, _In_ HOOKPROC lpfn, _In_ HINSTANCE hMod, _In_ DWORD dwThreadId);
handle = FindWindow("Notepad", NULL) //for example
DWORD threadId = GetWindowThreadProcessId(handle, NULL);
HHOOK hhook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) &procedure, (HINSTANCE) NULL, threadId);
return;
}
这不起作用,因为应用程序永远不会进入 过程()。
编辑:根据-RbMm的建议,我从[=21]找到了threadId =]句柄。谢谢。
DWORD threadId = GetWindowThreadProcessId(handle, NULL);
HHOOK hhook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) &procedure, (HINSTANCE) NULL, threadId);
您必须在 hook 之后发送消息,而您的示例不会这样做。
其他应用程序中线程的挂钩程序必须在 .DLL 中:
A thread-specific hook procedure is called only in the context of the associated thread. If an application installs a hook procedure for one of its own threads, the hook procedure can be in either the same module as the rest of the application's code or in a DLL. If the application installs a hook procedure for a thread of a different application, the procedure must be in a DLL.
WH_CBT
或 WH_SHELL
挂钩可能更适合您要执行的操作。
或者您可以尝试用 event hook 捕捉 EVENT_OBJECT_DESTROY
。事件挂钩明确支持上下文挂钩。
基本上,我想在用户单击 X(关闭)特定 window 后执行某些操作。
这是我的方法:
HWND handle;
LRESULT WINAPI procedure(int code, WPARAM wParam, LPARAM, lParam){
CWPSTRUCT* j = (CWPSTRUCT*) lParam;
WPSTRUCT* w = (CWPSTRUCT*) wParam;
if(w->message == WM_SYSCOMMAND && j->message == SC_CLOSE){
//do something
}
}
Void doSomething(){
HINSTANCE hDLL = LoadLibrary("User32.dll");
HWND WINAPI FindWindow(_In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowName);
HHOOK WINAPI SetWindowsHookEx(_In_ int idHook, _In_ HOOKPROC lpfn, _In_ HINSTANCE hMod, _In_ DWORD dwThreadId);
handle = FindWindow("Notepad", NULL) //for example
DWORD threadId = GetWindowThreadProcessId(handle, NULL);
HHOOK hhook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) &procedure, (HINSTANCE) NULL, threadId);
return;
}
这不起作用,因为应用程序永远不会进入 过程()。
编辑:根据-RbMm的建议,我从[=21]找到了threadId =]句柄。谢谢。
DWORD threadId = GetWindowThreadProcessId(handle, NULL);
HHOOK hhook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC) &procedure, (HINSTANCE) NULL, threadId);
您必须在 hook 之后发送消息,而您的示例不会这样做。
其他应用程序中线程的挂钩程序必须在 .DLL 中:
A thread-specific hook procedure is called only in the context of the associated thread. If an application installs a hook procedure for one of its own threads, the hook procedure can be in either the same module as the rest of the application's code or in a DLL. If the application installs a hook procedure for a thread of a different application, the procedure must be in a DLL.
WH_CBT
或 WH_SHELL
挂钩可能更适合您要执行的操作。
或者您可以尝试用 event hook 捕捉 EVENT_OBJECT_DESTROY
。事件挂钩明确支持上下文挂钩。