钩子教程 - 原理(二十) : MouseProc

原文地址:http://www.zdexe.com/program/201004/594.html

方法15 :MouseProc Function

The MouseProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function whenever an application calls the GetMessage or PeekMessage function and there is a mouse message to be processed. 

  MouseProc钩子子程是同SetWindowsHookEx方法一起使用的、应用程序定义的或者库定义的回调函数。无论什么时候,当应用程序一调用GetMessage 或者 PeekMessage方法,有鼠标消息即将被处理时,系统调用该方法。

 

The HOOKPROC type defines a pointer to this callback function. MouseProc is a placeholder for the application-defined or library-defined function name.  

  HOOKPROC类型定义了指向回调函数的指针。MouseProc是应用程序定义的或者库定义的方法名称。

Syntax:语法

LRESULT CALLBACK MouseProc(      

         int nCode,
    WPARAM wParam,
    LPARAM lParam
);

Parameters 参数

nCode :[in] Specifies a code 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 byCallNextHookEx. This parameter can be one of the following values. 

  指定钩子子程使用的用来决定如何处理消息的码值。如果nCode小于0,钩子子程必须将消息传递给CallNextHookEx方法,自己不进行进一步的处理,并且要返回由CallNextHookEx方法返回的返回值。该参数可以是下列值之一:

1.HC_ACTION :The wParam and lParam parameters contain information about a mouse message. 参数wParam 和 lParam包含和鼠标消息相关的信息。

2.HC_NOREMOVE :The wParam and lParam parameters contain information about a mouse message, and the mouse message has not been removed from the message queue. (An application called thePeekMessage function, specifying the PM_NOREMOVE flag.)

参数wParam 和 lParam包含和鼠标消息相关的信息,鼠标消息还没有从消息队列中移除。

wParam :[in] Specifies the identifier of the mouse message. 指定鼠标消息的标识符。

lParam:[in] Pointer to a MOUSEHOOKSTRUCT structure. 指向MOUSEHOOKSTRUCT结构的指针。

Return Value 返回值

If nCode is less than zero, the hook procedure must return the value returned byCallNextHookEx. If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_MOUSE hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the target window procedure.

如果code小于0,钩子子程必须返回由CallNextHookEx方法返回的返回值。如果code大于等于0,钩子子程还没有处理该消息,强烈要求调用CallNextHookEx方法并返回由它返回的返回值;否则,其它已经安装了WH_MOUSE钩子的应用程序将收不到钩子通知,可能导致行为的错误。如果钩子子程已经处理了该消息,应该返回非0值,以阻止系统将消息传递给钩子链表中剩余的钩子或者目标窗体程序。

Remarks备注

An application installs the hook procedure by specifying the WH_MOUSE hook type and a pointer to the hook procedure in a call to the SetWindowsHookEx function. The hook procedure must not install a WH_JOURNALPLAYBACK Hook callback function.

应用程序通过下面方法来安装钩子:指定WH_MOUSE钩子类型;指定在调用SetWindowsHookEx的方法中指向钩子子程的指针。该钩子子程不应安装WH_JOURNALPLAYBACK钩子的回调函数。

转载于:https://www.cnblogs.com/DuanLaoYe/p/5501649.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤使用 easyx 实现鼠标拖动图形: 1. 定义一个变量 `bool isDragging = false`,表示当前是否正在拖动图形。 2. 在 `initgraph` 函数中,注册鼠标消息处理函数 `MouseProc`。 3. 在 `MouseProc` 函数中,处理鼠标按下、移动和松开事件。如果鼠标按下,检查鼠标是否在图形内部,如果是,则将 `isDragging` 设为 `true`。如果鼠标移动,检查 `isDragging` 是否为 `true`,如果是,则将图形的坐标设置为鼠标的坐标。如果鼠标松开,将 `isDragging` 设为 `false`。 4. 在主循环中,调用 `MouseProc` 函数处理鼠标消息。 以下是示例代码: ```c++ #include <graphics.h> bool isDragging = false; // 是否正在拖动图形 int mouseX, mouseY; // 鼠标的坐标 int rectX = 100, rectY = 100, rectWidth = 50, rectHeight = 50; // 图形的位置和大小 void MouseProc(int msg, int x, int y) { switch (msg) { case WM_LBUTTONDOWN: // 鼠标左键按下 if (x >= rectX && x <= rectX + rectWidth && y >= rectY && y <= rectY + rectHeight) { isDragging = true; mouseX = x; mouseY = y; } break; case WM_MOUSEMOVE: // 鼠标移动 if (isDragging) { rectX += x - mouseX; rectY += y - mouseY; mouseX = x; mouseY = y; } break; case WM_LBUTTONUP: // 鼠标左键松开 isDragging = false; break; } } int main() { initgraph(640, 480); registermousehandler(WM_LBUTTONDOWN, MouseProc); registermousehandler(WM_MOUSEMOVE, MouseProc); registermousehandler(WM_LBUTTONUP, MouseProc); while (!kbhit()) { cleardevice(); rectangle(rectX, rectY, rectX + rectWidth, rectY + rectHeight); delay_fps(60); } closegraph(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值