C# 模擬鼠標(mouse_event)


想必有很多人在項目開發中可能遇見需要做模擬鼠標點擊的小功能,很多人會在

百度過后采用mouse_event這個函數,不過我並不想討論如何去使用mouse_event

函數怎么去使用,因為那沒有多大意義。

 

[csharp]  view plaincopy
  1. static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo)  
  2. {  
  3.     int x = dx, y = dy;  
  4.     edit_position(dwFlags, dx, dy, ref x, ref y);   
  5.     IntPtr hWndFromPoint = WindowFromPoint(x, y);  
  6.     screen_to_client(hWndFromPoint, ref x, ref y);   
  7.     send_message(hWndFromPoint, dwFlags, cButtons, x, y);  
  8. }  

 

上述代碼你發現了什么?如果你發現說明你知道了本文到底在寫什么東東 說不定你

會有一些興趣看下去,不過想到我如今混那么凄慘 在工地上做干活 不過也還好。

鼠標點擊目標時會向鼠標所點擊目標窗口投遞消息,根據鼠標的按鍵、狀態不同會

投遞不同的消息,一個完整的“鼠標左鍵單擊”事件過程為“WM_LBUTTONDOWN + 

WM_LBUTTONUP”即鼠標“先左鍵按下 + 后左鍵抬起”,由於mouse_event可以模擬

鼠標點擊過程而不是直接性一次完整的鼠標單擊過程,所以同樣存在“按下、抬起”

 

[csharp]  view plaincopy
  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);  

mouse_event在沒有提供MOUSEEVENTF_MOVE量時光標不會移動到相對位置,

 

“光標相對位置=光標現行位置+新光標位置”如果提供量“MOUSEEVENTF_ABSOLUTE”

絕對位置,則會以“新光標位置”為准而不會添加“光標現行位置”

 

[csharp]  view plaincopy
  1. static void edit_position(int dwFlags, int dx, int dy, ref int x, ref int y)  
  2. {  
  3.     Point pos = MousePosition;  
  4.     x = x + pos.X;  
  5.     y = y + pos.Y;  
  6.     if ((dwFlags | MOUSEEVENTF_ABSOLUTE) == dwFlags)  
  7.         SetCursorPos(dx, dy);  
  8.     if ((dwFlags | MOUSEEVENTF_MOVE) == dwFlags)  
  9.         SetCursorPos(x, y);  
  10. }  

 

edit_position函數主要用於對MOUSEEVENTF_MOVE於MOUSEEVENTF_ABSOLUTE

相對/絕對光標位置修改的一個支持

 

[csharp]  view plaincopy
  1. static void send_message(IntPtr hWnd, int dwFlags, int cButtons, int x, int y)  
  2. {  
  3.     if ((dwFlags | MOUSEEVENTF_LEFTDOWN) == dwFlags)  
  4.         SendMessage(hWnd, WM_LBUTTONDOWN, cButtons, MakeDWord(x, y));  
  5.     if ((dwFlags | MOUSEEVENTF_LEFTUP) == dwFlags)  
  6.         SendMessage(hWnd, WM_LBUTTONUP, cButtons, MakeDWord(x, y));  
  7.     if ((dwFlags | MOUSEEVENTF_RIGHTDOWN) == dwFlags)  
  8.         SendMessage(hWnd, WM_RBUTTONDOWN, cButtons, MakeDWord(x, y));  
  9.     if ((dwFlags | MOUSEEVENTF_RIGHTUP) == dwFlags)  
  10.         SendMessage(hWnd, WM_RBUTTONUP, cButtons, MakeDWord(x, y));  
  11.     if ((dwFlags | MOUSEEVENTF_MIDDLEDOWN) == dwFlags)  
  12.         SendMessage(hWnd, WM_MBUTTONDOWN, cButtons, MakeDWord(x, y));  
  13.     if ((dwFlags | MOUSEEVENTF_MIDDLEUP) == dwFlags)  
  14.         SendMessage(hWnd, WM_MBUTTONUP, cButtons, MakeDWord(x, y));  
  15. }  

 

send_message函數主要用於模擬鼠標點擊的過程,上面我提到“先左鍵按下 + 后左鍵抬起”

在上面的代碼中你會看的清楚的不得了,如果相反你可以去嘗試一番會有什么后果 與其說

不如你們自己做更要來的快些。

 

[csharp]  view plaincopy
  1. static int MakeDWord(int low, int high)  
  2. {  
  3.     return low + (high * Abs(~ushort.MaxValue));  
  4. }  
  5.   
  6. static int Abs(int value)  
  7. {  
  8.     return ((value >> 31) ^ value) - (value >> 31);  
  9. }  

MakeDWord / 合並整數,函數主要是把兩個short合並為一個int,分為low、high兩部分

 

 

[csharp]  view plaincopy
  1. static bool screen_to_client(IntPtr hwnd, ref int x, ref int y)  
  2. {  
  3.     bool bRetVal = false;  
  4.     Point lpptPos = new Point(x, y);  
  5.     if ((bRetVal = ScreenToClient(hwnd, ref lpptPos)))  
  6.     {  
  7.         x = lpptPos.X;  
  8.         y = lpptPos.Y;  
  9.     }  
  10.     return bRetVal;  
  11. }  

screen_to_client函數故名思意,它主要用於把屏幕上的坐標轉換到窗口客戶上對應坐標

 

 

[csharp]  view plaincopy
  1. public const int WM_LBUTTONDOWN = 513; // 鼠標左鍵按下  
  2. public const int WM_LBUTTONUP = 514; // 鼠標左鍵抬起  
  3. public const int WM_RBUTTONDOWN = 516; // 鼠標右鍵按下  
  4. public const int WM_RBUTTONUP = 517; // 鼠標右鍵抬起  
  5. public const int WM_MBUTTONDOWN = 519; // 鼠標中鍵按下  
  6. public const int WM_MBUTTONUP = 520; // 鼠標中鍵抬起  
  7.   
  8. public const int MOUSEEVENTF_MOVE = 0x0001; // 移動鼠標         
  9. public const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠標左鍵按下        
  10. public const int MOUSEEVENTF_LEFTUP = 0x0004; // 鼠標左鍵抬起        
  11. public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠標右鍵按下       
  12. public const int MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠標右鍵抬起          
  13. public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // 鼠標中鍵按下    
  14. public const int MOUSEEVENTF_MIDDLEUP = 0x0040; // 鼠標中鍵抬起           
  15. public const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 絕對坐標   

 

 

[csharp]  view plaincopy
  1. [DllImport("user32.dll", SetLastError = true)]  
  2. public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);  
  3.   
  4. [DllImport("user32.dll", SetLastError = true)]  
  5. public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);  
  6.   
  7. [DllImport("user32.dll", SetLastError = true)]  
  8. public static extern int SetCursorPos(int x, int y);  
  9.   
  10. [DllImport("user32.dll", SetLastError = true)]  
  11. public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt);  
  12.   
  13. // [DllImport("user32", SetLastError = true)]  
  14. // public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);  

鼠標右鍵單擊(靜默):

 

 

[csharp]  view plaincopy
  1. mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);  

 

鼠標左鍵雙擊(靜默):

 

[csharp]  view plaincopy
  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);  

鼠標移動(相對位置):

 

 

[csharp]  view plaincopy
  1. mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);  

鼠標移動(絕對位置):

 

 

[csharp]  view plaincopy
  1. mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0);  

好了就這么多吧,現在渾身頭暈腦漲的 話說多了反倒不好 自己從代碼中領悟才是真


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM