为什么模拟鼠标点击(使用mouse_event)只对选定的组件起作用? [英] Why does a simulated mouse click (using mouse_event) work on selected components only?
问题描述
我有多个光标(实际上是表单),可以通过它们各自的鼠标来控制。 (1个用户的光标)
我使用 SetCursorPos
定位默认游标(原始系统光标)处于不会从我的应用程序中移除焦点的位置,并使用 ShowCursor(false)
隐藏它。
我有一个类可以获取鼠标的句柄和坐标。
当用户点击我使用 SetCursorPos
和 mouse_event
模拟该特定位置的点击。
我的模拟鼠标点击只适用于某些组件的OnClick事件(应该只是按钮和标签,但是我试验了我的项目中的东西,只是为了知道什么将会或不会工作):
它适用于:
$ b $ $($)
它不起作用:
- TLabel
- 面板(TPanel,TAdvSmoothPanel)
- TCoolBar
- TMenuItem(不是TMainMenu的直接子元素)
这是我的代码:
SetCursorPos(currentX,currentY);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
为什么它不适用于某些组件?是否有解决方法(因为我希望能够使用mouse_event单击标签)。
编辑:
我尝试检查点击功能是否真的被调用,所以我在SetCursorPos和mouse_event之前放置了 ShowMessage('clicked');
,但奇怪的是(小编编辑:除MenuItems之外的所有内容)都可以正常工作事实上,我有一个消息弹出,每次我尝试点击某事)。有没有人知道为什么这样做?
似乎在这里工作;
procedure TForm1.Panel1Click(Sender:TObject);
begin
ShowMessage('Click');
结束
procedure TForm1.Button1Click(Sender:TObject);
var
Pt:TPoint;
begin
Pt:= Panel1.ClientToScreen(Point(0,0));
SetCursorPos(Pt.x,Pt.y);
// SetCursorPos(Panel1.ClientOrigin.x,Panel1.ClientOrigin.y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
结束
或没有 SetCursorPos
;
procedure TForm1.Button1Click(Sender:TObject);
var
Pt:TPoint;
begin
Pt:= Panel1.ClientToScreen(Point(0,0));
Pt.x:= Round(((Pt.x + 1)* 65535)/ Screen.Width);
Pt.y:= Round(((Pt.y + 1)* 65535)/ Screen.Height);
mouse_event(MOUSEEVENTF_ABSOLUTE或MOUSEEVENTF_MOVE或MOUSEEVENTF_LEFTDOWN,
Pt.x,Pt.y,0,0);
mouse_event(MOUSEEVENTF_ABSOLUTE或MOUSEEVENTF_MOVE或MOUSEEVENTF_LEFTUP,
Pt.x,Pt.y,0,0);
结束
I have multiple cursors (which are actually forms) that can be controlled by their respective mouse. (1 cursor for 1 user).
I use SetCursorPos
to position the default cursor (the original system cursor) in a position that will not take away the focus from my application, and use ShowCursor(false)
to hide it.
I have a class that gets the handle of the mouse and the coordinates.
When the user clicks I use the SetCursorPos
and the mouse_event
to simulate the clicks in that particular position.
My simulated mouse clicks only work on certain components' OnClick event (It was supposed to be only buttons and labels, but I experimented with the stuff on my project just to know what will or won't work):
It works on:
- Buttons (TButton, TBitBtn, TAdvSmoothButton)
- TAdvGrid
- TMenuItem (but the direct child of the TMainMenu only)
- TRadioButton
It doesn't work on:
- TLabel
- Panels (TPanel, TAdvSmoothPanel)
- TCoolBar
- TMenuItem (not direct child of TMainMenu)
This is my code:
SetCursorPos(currentX , currentY);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Why doesn't it work on some components? Is there a workaround (because I would like to be able to click labels using mouse_event).
EDIT:
I tried checking if the clicking function was really called, so I put ShowMessage('clicked');
before the SetCursorPos and mouse_event...but strangely everything (minor edit: everything except MenuItems) works fine now (except for the fact that I have a Message popping out everytime I try to click something). Does anybody have an idea why this behaves that way?
Seems to work here;
procedure TForm1.Panel1Click(Sender: TObject);
begin
ShowMessage('Click');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Pt: TPoint;
begin
Pt := Panel1.ClientToScreen(Point(0, 0));
SetCursorPos(Pt.x, Pt.y);
// SetCursorPos(Panel1.ClientOrigin.x, Panel1.ClientOrigin.y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
end;
or, without SetCursorPos
;
procedure TForm1.Button1Click(Sender: TObject);
var
Pt: TPoint;
begin
Pt := Panel1.ClientToScreen(Point(0, 0));
Pt.x := Round(((Pt.x + 1) * 65535) / Screen.Width);
Pt.y := Round(((Pt.y + 1) * 65535) / Screen.Height);
mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE or MOUSEEVENTF_LEFTDOWN,
Pt.x, Pt.y, 0, 0);
mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE or MOUSEEVENTF_LEFTUP,
Pt.x, Pt.y, 0, 0);
end;
这篇关于为什么模拟鼠标点击(使用mouse_event)只对选定的组件起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!