2011-09-06 59 views
2

除了将光标移动到Cursor类以外,我还找不到任何解决方案,点击mouse_event然后将光标移动到其旧位置。我现在正在玩SendInput的功能,但仍然没有好的解决方案。有什么建议?在没有移动光标的情况下执行鼠标点击

+0

什么味道的.Net C#,VB ?, ASP.Net –

+0

我编辑了标签。感谢您提醒。 – onatm

+0

你想要点击什么类型的对象? –

回答

3

下面是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"); 
    } 
} 
相关问题