在不移动光标的情况下执行鼠标单击

除了用Cursor类移动光标,单击mouse_event然后将光标移动到旧位置,我找不到任何解决方案。 我现在正在使用SendInputfunction,但仍然没有机会获得一个好的解决方案。 任何建议?

您应该使用Win32 API。 使用user32.dll中的pInvoked SendMessage

pInvoked Function

然后阅读有关鼠标事件: msdn上的鼠标输入

然后阅读: 系统事件和鼠标混乱…….

还有很多信息: 信息

以下是Hooch建议的一个例子。

我创建了一个包含2个按钮的表单。 单击第一个按钮时,第二个按钮的位置将被解析(屏幕显示)。 然后检索此按钮的句柄。 最后,SendMessage(…)(PInvoke)函数用于在不移动鼠标的情况下发送单击事件。

 public partial class Form1 : Form { [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "WindowFromPoint", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr WindowFromPoint(Point point); private const int BM_CLICK = 0x00F5; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Specify the point you want to click var screenPoint = this.PointToScreen(new Point(button2.Left, button2.Top)); // Get a handle var handle = WindowFromPoint(screenPoint); // Send the click message if (handle != IntPtr.Zero) { SendMessage( handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Hi", "There"); } }