江湖救急:关于捕获鼠标移动(MouseMove)的消息

Luck_xyl 2003-12-23 10:54:01
在程序中我要用到捕获鼠标的移动消息

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_MOUSEMOVE,TMessage,OnMouseMove);
END_MESSAGE_MAP(TForm)

但是这个只能够捕获一个控件的鼠标移动消息,如果我要捕获主Form上面的所有鼠标移动消息,我要怎么样才能够实现。
...全文
325 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
teatool 2003-12-25
  • 打赏
  • 举报
回复
用Form的OnMouseMove事件就行了
南郁 2003-12-24
  • 打赏
  • 举报
回复
你是不是用CB6?如果是,在Form上放一个ApplicationEvents 控件(在Addtion页上)。
然后,双写这个控件的Event : OnMessage:
void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if (Msg.message == WM_MOUSEMOVE && Msg.hwnd == Handle)
{
/*
fwKeys = wParam; // key flags
xPos = LOWORD(lParam); // horizontal position of cursor
yPos = HIWORD(lParam); // vertical position of cursor
*/

int x = LOWORD(Msg.lParam);
int y = HIWORD(Msg.lParam);

int keyFlag = Msg.wParam;

Caption = Format("x = %d, y = %d",ARRAYOFCONST((x,y)));
}
}
如果你用的是CB5,就自己写:
Application->OnMessage = &ApplicationEvents1Message;
jbz 2003-12-24
  • 打赏
  • 举报
回复
也许用鼠标hook最有效。
geniusdhc 2003-12-24
  • 打赏
  • 举报
回复
OnMouseMove可以
constantine 2003-12-24
  • 打赏
  • 举报
回复
没错
南郁 2003-12-24
  • 打赏
  • 举报
回复
楼主要说的 "我要捕获主Form上面的所有鼠标移动消息"... 用Application Events 就是正确的啊.

了解VCL 的人都明白, 所有消息在分派(Dispatch函数)之前,都来自 Application ,正是因为像楼主这类的需求很普遍,Borland 才在 新版推出 ApplicationEvent.

如果直接使用 ApplicationEvents 控件,那么由于在分发消息之前,VcL已经判断过,所以不要也行,但对于CB5或4,没有这个控件,就得判断窗口是哪个了, 因为楼主只要"主Form上的"
nobill 2003-12-24
  • 打赏
  • 举报
回复
用鼠标钩子!
我不懂电脑 2003-12-24
  • 打赏
  • 举报
回复
你可以用getcursorpos API捕获鼠标位置然后和上次位置判断是否移动
没有bcb api可能记错,自己查一下吧
shadowstar 2003-12-24
  • 打赏
  • 举报
回复
to nanyu(南郁),其实你这样做和楼主没什么分别。
最简单的方法是为Form1添加OnMouseMove事件,然后把所有可视组件的OnMouseMove事件指向Form1的FormMouseMove。如下代码可测试:

void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
TControl* Control = dynamic_cast<TControl*>(Sender);

Edit1->Text = AnsiString(X) + ", " + Y;
Edit2->Text = AnsiString(X + Control->Left) + ", " + (Y + Control->Top);
}

当然nanyu(南郁)的方法也是可行的,只是要把Msg.hwnd == Handle去掉,不过注意这里得到的只是Mouse在控件上的坐标。
constantine 2003-12-24
  • 打赏
  • 举报
回复
TApplicationEvents可以,OnMouseMove也可以,楼主的也可以。
但是这些只能是针对程序本身,当你在其他地方移动时就不可以了。
只能用钩子。
ljlln 2003-12-24
  • 打赏
  • 举报
回复
Use TApplicationEvents to intercept the events of the global Application object. When you add a TApplicationEvents object to a form, the Application object forwards all events to the TApplicationEvents object. Thus, each event of the TApplicationEvents object is the same as the event with the same name on the Application object.

Each form in an application can have its own TApplicationEvents object. Each application event occurs for all the TApplicationEvents objects in the project. To change the order in which the different TApplicationEvents objects receive events, use the Activate method. To prevent other TApplicationEvents objects from receiving a specific event, use the CancelDispatch method.
ljlln 2003-12-24
  • 打赏
  • 举报
回复
TApplicationEvents可以截获所有的Applicaton 的Object 数据Event !应该是可以的!
Luck_xyl 2003-12-23
  • 打赏
  • 举报
回复
各位DX看看有没有什么方法可以建议的,

如果有了解MSN那种截获鼠标、键盘消息的方法也可以拿出来讨论讨论的

13,874

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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