繁体   English   中英

mouse_event与真实鼠标事件

[英]mouse_event vs real mouse event

  1. 真正的鼠标单击 (手动)和执行代码单击 (在C#中通过mouse_event )是否有区别?
  2. 同样,实际移动鼠标光标和设置Cursor.Position之间有区别吗?

如果有区别:

  1. 如何识别该事件的来源?
  2. 有没有一种模拟鼠标单击 / 光标移动的方法 ,就像它来自鼠标或键盘驱动程序一样

Edit1:为@Marco Forberg添加了代码示例。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    Button button;

    private void Form1_Load(object sender, EventArgs e)
    {

        button = new Button();
        button.Text = "Click";
        button.Location = new Point(50, 50);
        button.Size = new System.Drawing.Size(100, 20);
        button.Click += button_Click;
        Controls.Add(button);

        Button simulate = new Button();
        simulate.Text = "Simulate";
        simulate.Location = new Point(50, 100);
        simulate.Size = new System.Drawing.Size(100, 20);
        simulate.Click += simulate_Click;
        Controls.Add(simulate);

    }

    void button_Click(object sender, EventArgs e)
    {
        Console.WriteLine(sender);
    }

    void simulate_Click(object sender, EventArgs e)
    {
        Point location = button.PointToScreen(Point.Empty);
        Cursor.Position = new Point(location.X + (button.Width / 2), location.Y + (button.Height / 2));

        mouse_event(0x02 | 0x04, 0, 0, 0, 0);
    }
}

如果创建适当的事件参数,则没有任何区别。 从“机器”中找出事件的唯一方法是分析时刻。

@ Garath, MSLLHOOKSTRUCT包含一个LLMHF_INJECTED标志,当事件来自对mouse_event函数的调用时,将设置该标志。

您可以使用SetWindowsHookEx检索此信息,如我在此处所述

实际单击控件和对鼠标事件处理程序的编程调用之间的区别应该是sender参数。

对于真正的鼠标单击,您会收到作为发送者被单击的控件。 以编程方式调用事件处理程序时,您应该提供明智的发送者

我也在尝试在C#中执行此操作,但是在注入的C#dll中托管了clr。 我的一个朋友建议阅读这篇文章http://pastebin.com/rj4YcW4C

我尝试过mouse_event,PostMessage,SendMessage,SendInput和Cursor.Position。 所有人都忽略了,但我相信这篇文章有我们都在寻求的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM