- 真正的鼠标单击(手动)和从代码执行单击(通过c# 中的mouse_event )之间有区别吗?
- 同样,真正移动鼠标光标和设置Cursor.Position有区别吗?
如果有区别:
- 如何识别该事件的来源?
- 有一种方法可以模拟鼠标单击/光标移动,就好像它来自鼠标或键盘驱动程序一样?
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);
}
}