全局HOOK如何拦截到活动窗口发生改变的消息

Howard-Lu 2010-09-02 03:06:32
也就是说,我想通过它确保任何时候都能够获取当前活动窗口的句柄。然后我是通过GetForegroundWindow()实现的。在这之前还要编写一个动态库,实现HOOK。

如果不这样做我只知道用一个死循环,循环体内调用GetForegroundWindow(),所以用恰当的hook过程应该就会方便一些。

具体应该用哪个hook过程?以及如何设置该回调函数参数?
...全文
311 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Howard-Lu 2010-09-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 coding_hello 的回复:]

C/C++ code

ShellProc Callback Function

An application-defined or library-defined callback function used with the SetWindowsHookEx function. The function receives notifications of Shell events from……
[/Quote]

这是贴的哪里的?MSDN上的?
野男孩 2010-09-04
  • 打赏
  • 举报
回复

ShellProc Callback Function

An application-defined or library-defined callback function used with the SetWindowsHookEx function. The function receives notifications of Shell events from the system.

The HOOKPROC type defines a pointer to this callback function. ShellProc is a placeholder for the application-defined or library-defined function name.
Syntax
Copy
LRESULT CALLBACK ShellProc(
__in int nCode,
__in WPARAM wParam,
__in LPARAM lParam
);

Copy
LRESULT CALLBACK ShellProc(
__in int nCode,
__in WPARAM wParam,
__in LPARAM lParam
);

Parameters
nCode [in]
int

The hook code. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. This parameter can be one of the following values. Value Meaning
HSHELL_ACCESSIBILITYSTATE
11
The accessibility state has changed.
HSHELL_ACTIVATESHELLWINDOW
3
The shell should activate its main window.
HSHELL_APPCOMMAND
12
The user completed an input event (for example, pressed an application command button on the mouse or an application command key on the keyboard), and the application did not handle the WM_APPCOMMAND message generated by that input.

If the Shell procedure handles the WM_COMMAND message, it should not call CallNextHookEx. See the Return Value section for more information.
HSHELL_GETMINRECT
5
A window is being minimized or maximized. The system needs the coordinates of the minimized rectangle for the window.
HSHELL_LANGUAGE
8
Keyboard language was changed or a new keyboard layout was loaded.
HSHELL_REDRAW
6
The title of a window in the task bar has been redrawn.
HSHELL_TASKMAN
7
The user has selected the task list. A shell application that provides a task list should return TRUE to prevent Windows from starting its task list.
HSHELL_WINDOWACTIVATED
4
The activation has changed to a different top-level, unowned window.
HSHELL_WINDOWCREATED
1
A top-level, unowned window has been created. The window exists when the system calls this hook.
HSHELL_WINDOWDESTROYED
2
A top-level, unowned window is about to be destroyed. The window still exists when the system calls this hook.
HSHELL_WINDOWREPLACED
13
A top-level window is being replaced. The window exists when the system calls this hook.



wParam [in]
WPARAM

This parameter depends on the value of the nCode parameter, as shown in the following table. nCode wParam
HSHELL_ACCESSIBILITYSTATE Indicates which accessibility feature has changed state. This value is one of the following: ACCESS_FILTERKEYS, ACCESS_MOUSEKEYS, or ACCESS_STICKYKEYS.
HSHELL_APPCOMMAND Indicates where the WM_APPCOMMAND message was originally sent; for example, the handle to a window. For more information, see cmd parameter in WM_APPCOMMAND.
HSHELL_GETMINRECT A handle to the minimized or maximized window.
HSHELL_LANGUAGE A handle to the window.
HSHELL_REDRAW A handle to the redrawn window.
HSHELL_WINDOWACTIVATED A handle to the activated window.
HSHELL_WINDOWCREATED A handle to the created window.
HSHELL_WINDOWDESTROYED A handle to the destroyed window.
HSHELL_WINDOWREPLACED A handle to the window being replaced.
Windows 2000: Not supported.



lParam [in]
LPARAM

This parameter depends on the value of the nCode parameter, as shown in the following table. nCode lParam
HSHELL_APPCOMMAND
GET_APPCOMMAND_LPARAM(lParam) is the application command corresponding to the input event.

GET_DEVICE_LPARAM(lParam) indicates what generated the input event; for example, the mouse or keyboard. For more information, see the uDevice parameter description under WM_APPCOMMAND.

GET_FLAGS_LPARAM(lParam) depends on the value of cmd in WM_APPCOMMAND. For example, it might indicate which virtual keys were held down when the WM_APPCOMMAND message was originally sent. For more information, see the dwCmdFlags description parameter under WM_APPCOMMAND.
HSHELL_GETMINRECT A pointer to a RECT structure.
HSHELL_LANGUAGE A handle to a keyboard layout.
HSHELL_REDRAW The value is TRUE if the window is flashing, or FALSE otherwise.
HSHELL_WINDOWACTIVATED The value is TRUE if the window is in full-screen mode, or FALSE otherwise.
HSHELL_WINDOWREPLACED A handle to the new window.
Windows 2000: Not supported.



Return Value

LRESULT

The return value should be zero unless the value of nCode is HSHELL_APPCOMMAND and the shell procedure handles the WM_COMMAND message. In this case, the return should be nonzero.
Remarks

Install this hook procedure by specifying the WH_SHELL hook type and a pointer to the hook procedure in a call to the SetWindowsHookEx function.
oldforest 2010-09-03
  • 打赏
  • 举报
回复
用WH_SHELL,钩子函数中判断nCode==HSHELL_WINDOWACTIVATED

LRESULT CALLBACK fncHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode == HSHELL_WINDOWACTIVATED)
{
HWND hActiveHwnd = (HWND)wParam;
......
}
}
Howard-Lu 2010-09-03
  • 打赏
  • 举报
回复
为什么没人理我啊……
lazy_2010 2010-09-03
  • 打赏
  • 举报
回复
wParam

HSHELL_WINDOWACTIVATED Handle to the activated window.

被激活的 HWND

lParam

HSHELL_WINDOWACTIVATED The value is TRUE if the window is in full-screen mode, or FALSE otherwise.

是否全屏显示
Howard-Lu 2010-09-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 oldforest 的回复:]

用WH_SHELL,钩子函数中判断nCode==HSHELL_WINDOWACTIVATED
C/C++ code

LRESULT CALLBACK fncHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode == HSHELL_WINDOWACTIVATED)
{
HWND hActiveH……
[/Quote]

再请教一下,WH_SHELL对应的回调函数三个参数分别表示什么?

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧