注释感觉已经很清楚了,有不懂的欢迎评论
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Runtime.InteropServices;
8 using System.Text;
9 using System.Threading;
10 using System.Threading.Tasks;
11 using System.Windows.Forms;
12
13 namespace WindowsFormsApp1
14 {
15 public partial class Form1 : Form
16 {
17 public Form1()
18 {
19 InitializeComponent();
20 this.Hide();
21 }
22
23
24
25
26 [DllImport("user32.dll")]
27 private static extern int SetCursorPos(int x, int y);
28
29
30
31 public void MoveMouseToPoint(Point p)
32 {
33 SetCursorPos(p.X, p.Y);
34 }
35
36
37
38 public void SetMouseRectangle(Rectangle rectangle)
39 {
40 System.Windows.Forms.Cursor.Clip = rectangle;
41 }
42
43
44
45 public void SetMouseAtCenterScreen()
46 {
47
48 int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
49 int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
50
51 loginx = winWidth / 3 * 2;
52 loginy = winHeight / 4 + 5;
53 Point centerP = new Point(loginx, loginy);
54
55 MoveMouseToPoint(centerP);
56 }
57
58 [DllImport("User32")]
59
60
61 public extern static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
62 public const int MOUSEEVENTF_LEFTDOWN = 0x2;
63 public const int MOUSEEVENTF_LEFTUP = 0x4;
64 public enum MouseEventFlags
65 {
66 Move = 0x0001,
67 LeftDown = 0x0002,
68 LeftUp = 0x0004,
69 RightDown = 0x0008,
70 RightUp = 0x0010,
71 MiddleDown = 0x0020,
72 MiddleUp = 0x0040,
73 Wheel = 0x0800,
74 Absolute = 0x8000
75 }
76
77 public static int loginx, loginy;
78 private void Form1_Load(object sender, EventArgs e)
79 {
80
81
82
83 SetMouseAtCenterScreen();
84
85
86
87
88
89
90 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0);
91
92 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0);
93
94 }
95 }
96 }