MyToolkit函式庫加入模擬滑鼠事件
1: #region 設定滑鼠位置
2: [DllImport("user32.dll")]
3: public static extern bool SetCursorPos(int x, int y);
4: #endregion
5: #region 模擬滑鼠事件
6: [DllImport("user32.dll")]
7: public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
8: public const int MOUSEEVENTF_LEFTDOWN = 0x02;
9: public const int MOUSEEVENTF_LEFTUP = 0x04;
10: #endregion
1: private void button1_Click(object sender, EventArgs e)
2: {
3: this.WindowState = FormWindowState.Minimized; // minimize Form1
4: Thread.Sleep(2000);
5: textBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString(); // get current cursor position
6: textBox2.Text = System.Windows.Forms.Cursor.Position.Y.ToString();
7: this.WindowState = FormWindowState.Normal;
8: }
1: private void button2_Click(object sender, EventArgs e)
2: {
3: int x = Int16.Parse( textBox1.Text );
4: int y = Int16.Parse( textBox2.Text);
5: MyToolkit.SystemTool.SetCursorPos(x, y);
6: Thread.Sleep(1000);
7: this.WindowState = FormWindowState.Minimized;
8: MyToolkit.SystemTool.mouse_event(MyToolkit.SystemTool.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
9: MyToolkit.SystemTool.mouse_event(MyToolkit.SystemTool.MOUSEEVENTF_LEFTUP, x, y, 0, 0);
10: this.WindowState = FormWindowState.Normal;
11: }
參考資料:
How can I simulate a mouse click at a certain position on the screen?
全站熱搜
留言列表