WinForm中鼠标自动移动到窗体中间,并模拟左键点击
private void btnGetusernamebymobile_Click(object sender, EventArgs e) { int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height; int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width; Point centerP = new Point(winWidth / 2, winHeight / 2); Cursor.Position = centerP; mouse_event(MouseEventFlag.LeftDown, centerP.X, centerP.Y, 0, UIntPtr.Zero); mouse_event(MouseEventFlag.LeftUp, centerP.X, centerP.Y, 0, UIntPtr.Zero); } [Flags] enum MouseEventFlag : uint { Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, MiddleDown = 0x0020, MiddleUp = 0x0040, XDown = 0x0080, XUp = 0x0100, Wheel = 0x0800, VirtualDesk = 0x4000, Absolute = 0x8000 } [DllImport("user32.dll")] static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); }