如何手动触发鼠标单击? [英] How to manually trigger a mouse Click?

查看:179
本文介绍了如何手动触发鼠标单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码员们!

我正在尝试从Windows窗体应用程序模拟系统范围的鼠标事件.我尚未在代码中实现任何全局鼠标挂钩"或鼠标事件处理程序".

我所拥有的是某些标志,这些标志在设置时会触发系统范围的鼠标事件.就像我有一个标志

Hi Coders!

I''m trying to simulate a system-wide mouse event from my Windows Form Application. I haven''t implemented any "global mouse hook" or "mouse event handler" in my code.

What I''ve is certain flags which when set triggers a system-wide mouse event. Like I''ve a flag

bool moveCursor = false;


根据表单中的特定事件(例如单击按钮)和
进行设置 (X,Y)值由应用程序传递为:


which is set depending on a particular event in the Form(like click a button) and
(X,Y) values are passed by the application as:

if(moveCursor)
{ 
   //code to get X Y values
   Cursor.Position = new Point(cursorpos.X, cursorpos.Y);
}





现在,我还需要对右键/左/双击进行同样的操作,即使用一个标志,该标志在设置时会在光标的当前位置执行右键/左/双击.

我阅读了有关点击"和鼠标点击"命令的信息(此处: http://www.autohotkey.com/docs/命令/Click.htm [^ ]),但并没有完全理解.

感谢您的帮助!





Now I need to do the same with right/left/double-click as well,i.e., use a flag which when set performs a right/left/double click at current location of the cursor.

I read about "Click" and "MouseClick" commands(here:http://www.autohotkey.com/docs/commands/Click.htm[^]) but didn''t quite get it.

Thanks for Your help!

推荐答案

仅当您需要消耗所有可用事件时,全局钩子才有用.您可以使用P/Invoke和Windows挂钩来做到这一点.请参阅 http://msdn.microsoft.com/en-us/library/ms632589(v = vs.85).aspx [ http: //msdn.microsoft.com/zh-CN/library/ms646310(v=vs.85).aspx [


我仍然不知道如何准确模拟点击可以帮助您处理眨眼.可能您只是想快速重用以前创建的某些代码,而没有明确的下一步计划.看来您正在做一个应用程序,并且可以访问所有控件.我认为您根本不需要任何钩子,甚至不需要底层仿真.
很有可能,只需调用Control.OnMouseClick即可解决问题.如果您只需要触发事件,它将起作用.请参阅
http://msdn.microsoft.com/en-us/library /system.windows.forms.control.onmouseclick.aspx [ ^ ].

由于此方法受到保护,因此需要在派生控件中调用它(抱歉,我不知道您使用哪种控件类型).

您可以仅模拟点击,也可以模拟单独的MouseUpMouseDown.这取决于您处理哪个事件.像这样的东西:

A global hook only helps if you need to consume all available event. You can do it using P/Invoke and Windows Hooks. See http://msdn.microsoft.com/en-us/library/ms632589(v=vs.85).aspx[^].

If you want to write a system-global hook, you need to write a native DLL which installs the hook. I suggest you create C++ DLL or a mixed-mode DLL with C++/CLI.

However, the hooks would not help you if you want to trigger mouse inputs event. You need to simulate mouse input, use Windows API SendInput, see http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[^].

I must warn against using simulation of input in development of UI. The UI should not use such tricks: all functionality can be done using just .NET library. If you explained your ultimate goal and requirements, you could get a better advice in this direction.

Simulation of input should be used only if your really need "system" tricks in your application. Examples include running of keyboard and/or mouse macro, virtual keyboard and the like.



I still don''t understand how exactly simulation of the click can help to deal with blink. Probably you simply want to quickly re-use some code created previously without clear plan on what would be next. It looks like you''re doing a momolythic application and have access to all your control. I think you don''t need any hooks or even low-level simulation at all.
Most likely, a simple call to Control.OnMouseClick will do the trick. It will work if you just need to trigger the event. See http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmouseclick.aspx[^].

As this method is protected, you need to call it in derived control (sorry, I don''t know what control type do you use).

You can simulate just the click or with separate MouseUp and MouseDown. It depends on which event do you handle. Something like this:

class MyClickingControl : Panel { //or whatever it is: Form?

    //...

    void SimulateClicks(int count, Point where, int clickDelay, int downDelay) {
        MouseEventArgs eventArgs =
            new MouseEventArgs(MouseButtons.Left, 1, where.X, where.Y, 0);
        for (int index = 0; index < count; index++) {
            this.OnMouseClick(eventArgs);
            System.Threading.Thread.Sleep(clickDelay);
        } //loop click
        //another way, depending on what did you handle:
        for (int index = 0; index < count; index++) {
            this.OnMouseDown(eventArgs);
            System.Threading.Thread.Sleep(downDelay);
            this.OnMouseUp(eventArgs);
            System.Threading.Thread.Sleep(clickDelay);
        } //loop down-up
        //maybe even this way, depending on what did you handle:
        for (int index = 0; index < count; index++) {
            this.OnMouseDown(eventArgs);
            this.OnMouseClick(eventArgs);
            System.Threading.Thread.Sleep(downDelay);
            this.OnMouseUp(eventArgs);
            System.Threading.Thread.Sleep(clickDelay);
        }  //loop down-click-up
    } //SimulateClicks

} //class MyClickingControl


[END EDIT]

-SA


[END EDIT]

—SA


除了之前提供的商品外,此
In addition to the good provided earlier, this http://msdn.microsoft.com/en-us/library/ms171548.aspx[^] might help as an add-on.


这篇关于如何手动触发鼠标单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆