C#采取mouse_event函數完成模仿鼠標功效。本站提示廣大學習愛好者:(C#采取mouse_event函數完成模仿鼠標功效)文章只能為提供參考,不一定能成為您想要的結果。以下是C#采取mouse_event函數完成模仿鼠標功效正文
上面我經由過程代碼為年夜家分享下C#模仿鼠標,詳細內容以下:
想必有許多人在項目開辟中能夠碰見須要做模仿鼠標點擊的小功效,許多人會在百渡過後采取mouse_event這個函數,不外我其實不想評論辯論若何去應用mouse_event函數怎樣去應用,由於那沒有多年夜意義。
static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo) { int x = dx, y = dy; edit_position(dwFlags, dx, dy, ref x, ref y); IntPtr hWndFromPoint = WindowFromPoint(x, y); screen_to_client(hWndFromPoint, ref x, ref y); send_message(hWndFromPoint, dwFlags, cButtons, x, y); }
上述代碼你發明了甚麼?假如你發明解釋你曉得了本文究竟在寫甚麼東東 說不定你會有一些興致看下去,不外想到我現在混那末悲涼 在工地上做干活 不外也還好。
鼠標點擊目的時會向鼠標所點擊目的窗口送達新聞,依據鼠標的按鍵、狀況分歧會送達分歧的新聞,一個完全的“鼠標左鍵單擊”事宜進程為“WM_LBUTTONDOWN +
WM_LBUTTONUP”即鼠標“先左鍵按下 + 後左鍵抬起”,因為mouse_event可以模仿鼠標點擊進程而不是直接性一次完全的鼠標單擊進程,所以異樣存在“按下、抬起”
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);
mouse_event在沒有供給MOUSEEVENTF_MOVE量時間標不會挪動到絕對地位,“光標絕對地位=光標現行地位+新光標地位”假如供給量“MOUSEEVENTF_ABSOLUTE”相對地位,則會以“新光標地位”為准而不會添加“光標現行地位”
static void edit_position(int dwFlags, int dx, int dy, ref int x, ref int y) { Point pos = MousePosition; x = x + pos.X; y = y + pos.Y; if ((dwFlags | MOUSEEVENTF_ABSOLUTE) == dwFlags) SetCursorPos(dx, dy); if ((dwFlags | MOUSEEVENTF_MOVE) == dwFlags) SetCursorPos(x, y); }
edit_position函數重要用於對MOUSEEVENTF_MOVE於MOUSEEVENTF_ABSOLUTE
絕對/相對光標地位修正的一個支撐
static void send_message(IntPtr hWnd, int dwFlags, int cButtons, int x, int y) { if ((dwFlags | MOUSEEVENTF_LEFTDOWN) == dwFlags) SendMessage(hWnd, WM_LBUTTONDOWN, cButtons, MakeDWord(x, y)); if ((dwFlags | MOUSEEVENTF_LEFTUP) == dwFlags) SendMessage(hWnd, WM_LBUTTONUP, cButtons, MakeDWord(x, y)); if ((dwFlags | MOUSEEVENTF_RIGHTDOWN) == dwFlags) SendMessage(hWnd, WM_RBUTTONDOWN, cButtons, MakeDWord(x, y)); if ((dwFlags | MOUSEEVENTF_RIGHTUP) == dwFlags) SendMessage(hWnd, WM_RBUTTONUP, cButtons, MakeDWord(x, y)); if ((dwFlags | MOUSEEVENTF_MIDDLEDOWN) == dwFlags) SendMessage(hWnd, WM_MBUTTONDOWN, cButtons, MakeDWord(x, y)); if ((dwFlags | MOUSEEVENTF_MIDDLEUP) == dwFlags) SendMessage(hWnd, WM_MBUTTONUP, cButtons, MakeDWord(x, y)); }
send_message函數重要用於模仿鼠標點擊的進程,下面我提到“先左鍵按下 + 後左鍵抬起”在下面的代碼中你會看的清晰的不得了,假如相反你可以去測驗考試一番會有甚麼效果與其說
不如你們本身做更要來的快些。
static int MakeDWord(int low, int high) { return low + (high * Abs(~ushort.MaxValue)); } static int Abs(int value) { return ((value >> 31) ^ value) - (value >> 31); } MakeDWord / 歸並整數,函數重要是把兩個short歸並為一個int,分為low、high兩部門 static bool screen_to_client(IntPtr hwnd, ref int x, ref int y) { bool bRetVal = false; Point lpptPos = new Point(x, y); if ((bRetVal = ScreenToClient(hwnd, ref lpptPos))) { x = lpptPos.X; y = lpptPos.Y; } return bRetVal; } screen_to_client函數故名思意,它重要用於把屏幕上的坐標轉換到窗口客戶上對應坐標 public const int WM_LBUTTONDOWN = 513; // 鼠標左鍵按下 public const int WM_LBUTTONUP = 514; // 鼠標左鍵抬起 public const int WM_RBUTTONDOWN = 516; // 鼠標右鍵按下 public const int WM_RBUTTONUP = 517; // 鼠標右鍵抬起 public const int WM_MBUTTONDOWN = 519; // 鼠標中鍵按下 public const int WM_MBUTTONUP = 520; // 鼠標中鍵抬起 public const int MOUSEEVENTF_MOVE = 0x0001; // 挪動鼠標 public const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠標左鍵按下 public const int MOUSEEVENTF_LEFTUP = 0x0004; // 鼠標左鍵抬起 public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠標右鍵按下 public const int MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠標右鍵抬起 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // 鼠標中鍵按下 public const int MOUSEEVENTF_MIDDLEUP = 0x0040; // 鼠標中鍵抬起 public const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 相對坐標 [DllImport("user32.dll", SetLastError = true)] public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr WindowFromPoint(int xPoint, int yPoint); [DllImport("user32.dll", SetLastError = true)] public static extern int SetCursorPos(int x, int y); [DllImport("user32.dll", SetLastError = true)] public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt); // [DllImport("user32", SetLastError = true)] // public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
鼠標右鍵單擊(靜默):
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);
鼠標左鍵雙擊(靜默):
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);
鼠標挪動(絕對地位):
mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);
鼠標挪動(相對地位):
mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0);
以上內容比擬多請賣力進修,願望可以或許贊助到年夜家。