2011-09-06 34 views
2

Cursorクラスでカーソルを移動し、mouse_eventをクリックしてカーソルを元の位置に移動する以外は、解決策が見つかりませんでした。私はSendInputの機能を使って遊んでいますが、良い解決策はまだありません。何かアドバイス?カーソルを移動せずにマウスクリックを実行

+0

.NET C#、VB?、ASP.Netのフレーバー –

+0

タグを編集しました。思い出していただきありがとうございます。 – onatm

+0

あなたはどのタイプのオブジェクトをクリックしようとしていますか? –

答えて

3

Hoochが提案したアプローチの例を以下に示します。

2つのボタンを含むフォームを作成しました。最初のボタンをクリックすると、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"); 
    } 
} 
関連する問題