如何用Sendmessage模拟鼠标单击事件? ( 积分: 100 )

  • 主题发起人 主题发起人 xiaodolphin
  • 开始时间 开始时间
X

xiaodolphin

Unregistered / Unconfirmed
GUEST, unregistred user!
我想让我的鼠标在鼠标所在窗口而且所在位置实现单击左键!(或者在某个特定窗口)
我写过这样,但是好像不行!
GetCursorPos(&MousePoint);// 获取鼠标位置
SetCursorPos(MousePoint.x,MousePoint.y);
LPARAM lParam = MAKELPARAM(MousePoint.x,MousePoint.y);
HWND hwnd=::WindowFromPoint(MousePoint);
:: SendMessage(hwnd,WM_RBUTTONDOWN,0,lParam);
:: SendMessage(hwnd,WM_RBUTTONUP,0,lParam );
请大家帮个忙啊,谢谢了!
 
我想让我的鼠标在鼠标所在窗口而且所在位置实现单击左键!(或者在某个特定窗口)
我写过这样,但是好像不行!
GetCursorPos(&MousePoint);// 获取鼠标位置
SetCursorPos(MousePoint.x,MousePoint.y);
LPARAM lParam = MAKELPARAM(MousePoint.x,MousePoint.y);
HWND hwnd=::WindowFromPoint(MousePoint);
:: SendMessage(hwnd,WM_RBUTTONDOWN,0,lParam);
:: SendMessage(hwnd,WM_RBUTTONUP,0,lParam );
请大家帮个忙啊,谢谢了!
 
当然不行了。
在sendMessage前面
加setfocus(hwnd);就行了
你试试看
 
嗯,我那样试过了,是可以出来菜单,但是位置不对,可能是屏幕坐标和窗口坐标没转换,但是我用以下程序转换,好像位置也还是不对。能帮我再看看?
GetCursorPos(&MousePoint);// 获取鼠标位置
SetCursorPos(MousePoint.x,MousePoint.y);
HWND hwnd=::WindowFromPoint(MousePoint);
CPoint p2 = MousePoint;
ClientToScreen(&p2);
::SetFocus(hwnd);
LPARAM lParam = MAKELPARAM(p2.x,p2.y);
:: SendMessage(hwnd,WM_RBUTTONDOWN,0,lParam);
:: SendMessage(hwnd,WM_RBUTTONUP,0,lParam);
 
用mouse_event好了。比如:
mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
 
谢谢你,用 mouse_event的可以,我试过了!
我只是想深入了解一下SendMessage的用法,因为SendMessage有好多用途!
 
在GetCursorPos后加上procedure TForm1.Button1Click(Sender: TObject);
Windows.ScreenToClient(hwnd, MousePoint);
就可以了;
此外,SetCursorPos是不必要的。
 
GetCursorPos(&MousePoint);// 获取鼠标位置
HWND hwnd=::WindowFromPoint(MousePoint);
::ScreenToClient(hwnd, &MousePoint);
LPARAM lParam = MAKELPARAM(MousePoint.x,MousePoint.y);
::SetFocus(hwnd);
:: SendMessage(hwnd,WM_RBUTTONDOWN,0,lParam);
:: SendMessage(hwnd,WM_RBUTTONUP,0,lParam);
这样吗?但是好像只能出来一次菜单(模拟一次右键),鼠标移动时不能再出来了!(我用了定时)
但是用
GetCursorPos(&MousePoint);// 获取鼠标位置
SetCursorPos(MousePoint.x,MousePoint.y);
LPARAM lParam1 = MAKELPARAM(MousePoint.x,MousePoint.y);
HWND hwnd=::WindowFromPoint(MousePoint);
::SetFocus(hwnd);
mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
就可以随鼠标移动出现菜单!
 
后退
顶部