2011-09-06 108 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"); 
    } 
} 
相關問題