原文地址:http://www.zdexe.com/program/201004/584.html
方法5 : CBTProc Function
The CBTProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function before activating, creating, destroying, minimizing, maximizing, moving, or sizing a window; before completing a system command; before removing a mouse or keyboard event from the system message queue; before setting the keyboard focus; or before synchronizing with the system message queue. A computer-based training (CBT) application uses this hook procedure to receive useful notifications from the system.
CBTProc钩子子程是和SetWindowsHookEx方法同时使用的、程序定义的或者库定义的回调函数。系统在下列事件发生之前调用该方法:
1.激活、创建、销毁、最小化、最大化、移动窗体、改变窗体大小;
2.完成系统命令;
3.从系统消息队列中移除鼠标或者键盘事件;
4.设置键盘焦点;
5.同步系统消息队列。
CBT应用程序使用该钩子子程接收来自系统的有用的通知。
The HOOKPROC type defines a pointer to this callback function. CBTProc is a placeholder for the application-defined or library-defined function name.
HOOKPROC类型定义了指向该回调函数的指针。CBTProc是程序定义的或者库定义的方法名字。
LRESULT CALLBACK CBTProc(
int nCode,
WPARAM wParam,
LPARAM lParam
);
Parameters 参数
nCode :[in] Specifies a code that the hook procedure uses to determine how to process the message. 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.
指定一个码值,钩子子程使用该值来决定如何处理消息。如果nCode小于0,钩子子程就必须将该消息传递给CallNextHookEx方法,自己不对消息做进一步的处理,并且应该返回由CallNextHookEx方法返回的返回值。该参数可以是以下值中的一个:
1.HCBT_ACTIVATE :The system is about to activate a window.
系统即将创建一个窗体。
2.HCBT_CLICKSKIPPED :The system has removed a mouse message from the system message queue. Upon receiving this hook code, a CBT application must install a WH_JOURNALPLAYBACK hook procedure in response to the mouse message.
系统已经将一个鼠标消息从系统消息队列中移除。一旦收到该钩子代码,CBT应用程序必须安装一个WH_JOURNALPLAYBAC钩子子程来响应鼠标消息。
3.HCBT_CREATEWND : A window is about to be created. The system calls the hook procedure before sending the WM_CREATE or WM_NCCREATE message to the window. If the hook procedure returns a nonzero value, the system destroys the window; the CreateWindow function returns NULL, but the WM_DESTROY message is not sent to the window. If the hook procedure returns zero, the window is created normally.
窗体即将被创建。系统在向窗体发出WM_CREATE 或者 WM_NCCREATE消息之前,调用该钩子子程。如果钩子子程返回非0值,表示系统销毁了窗体;CreateWindow方法返回Null,但是WM_DESTROY消息并不发送给窗体。如果钩子子程返回0,表示窗体正常被创建。
At the time of the HCBT_CREATEWND notification, the window has been created, but its final size and position may not have been determined and its parent window may not have been established. It is possible to send messages to the newly created window, although it has not yet received WM_NCCREATE or WM_CREATE messages. It is also possible to change the position in the z-order of the newly created window by modifying the hwndInsertAfter member of the CBT_CREATEWND structure.
在 HCBT_CREATEWND通知的时候,窗体已经被创建了,但是它的最终的大小和位置可能还没有被确定,它的父窗体也可能没有被创建起来。虽然一个新创建的窗体可能还没有接收到WM_NCCREATE或者WM_CREATE消息,但是向它发送消息是可能的。通过修改CBT_CREATEWND 结构体的hwndInsertAfter成员,改变新创建窗体的在Z轴次序的位置也是可能的。
4.HCBT_DESTROYWND :A window is about to be destroyed.
窗体即将被销毁。
5.HCBT_KEYSKIPPED :The system has removed a keyboard message from the system message queue. Upon receiving this hook code, a CBT application must install a WH_JOURNALPLAYBACK hook procedure in response to the keyboard message.
系统已经从系统的消息队列中移除了一个键盘消息。一旦接收到该钩子代码,CBT应用程序必须安装一个WH_JOURNALPLAYBAC钩子来响应键盘消息。
6.HCBT_MINMAX :A window is about to be minimized or maximized.
窗体即将被最小化或者最大化。
7.HCBT_MOVESIZE :A window is about to be moved or sized.
窗体即将被移动或者重置大小。
8.HCBT_QS :The system has retrieved a WM_QUEUESYNC message from the system message queue.
系统已经收到了一个来自系统消息队列的WM_QUEUESYNC消息。
9.HCBT_SETFOCUS :A window is about to receive the keyboard focus.
窗体即将接收键盘焦点。
10.HCBT_SYSCOMMAND :A system command is about to be carried out. This allows a CBT application to prevent task switching by means of hot keys.
系统命令即将被执行。这允许CBT程序阻止通过快捷键来进行任务切换。
wParam :[in] Depends on the nCode parameter.
取决于参数 nCode
lParam :[in] Depends on the nCode parameter.
取决于参数 nCode
Return Value 返回值
The value returned by the hook procedure determines whether the system allows or prevents one of these operations. For operations corresponding to the following CBT hook codes, the return value must be 0 to allow the operation, or 1 to prevent it:
钩子子程的返回值取决于系统允许还是阻止这种操作。对于下列这些操作的CBT钩子代码,如果允许则返回值必须是0,如果阻止返回值必须是1。
HCBT_ACTIVATE ;HCBT_CREATEWND ;HCBT_DESTROYWND;HCBT_MINMAX ;HCBT_MOVESIZE ;HCBT_SETFOCUS;HCBT_SYSCOMMAND
For operations corresponding to the following CBT hook codes, the return value is ignored:
对于下列这些操作的CBT钩子代码,返回值被忽略。
HCBT_CLICKSKIPPED ;HCBT_KEYSKIPPED ;HCBT_QS