国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#采用mouse_event函數(shù)實(shí)現(xiàn)模擬鼠標(biāo)功能

2019-10-29 21:40:25
字體:
供稿:網(wǎng)友
這篇文章主要介紹了C#模擬鼠標(biāo)點(diǎn)擊小功能,通過代碼向大家做分析,需要的朋友可以參考下
 

下面我通過代碼為大家分享下C#模擬鼠標(biāo),具體內(nèi)容如下:

想必有很多人在項(xiàng)目開發(fā)中可能遇見需要做模擬鼠標(biāo)點(diǎn)擊的小功能,很多人會在百度過后采用mouse_event這個函數(shù),不過我并不想討論如何去使用mouse_event函數(shù)怎么去使用,因?yàn)槟菦]有多大意義。

?

 

  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. }  


上述代碼你發(fā)現(xiàn)了什么?如果你發(fā)現(xiàn)說明你知道了本文到底在寫什么東東 說不定你會有一些興趣看下去,不過想到我如今混那么凄慘 在工地上做干活 不過也還好。

鼠標(biāo)點(diǎn)擊目標(biāo)時會向鼠標(biāo)所點(diǎn)擊目標(biāo)窗口投遞消息,根據(jù)鼠標(biāo)的按鍵、狀態(tài)不同會投遞不同的消息,一個完整的“鼠標(biāo)左鍵單擊”事件過程為“WM_LBUTTONDOWN +

WM_LBUTTONUP”即鼠標(biāo)“先左鍵按下 + 后左鍵抬起”,由于mouse_event可以模擬鼠標(biāo)點(diǎn)擊過程而不是直接性一次完整的鼠標(biāo)單擊過程,所以同樣存在“按下、抬起”

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);  
mouse_event在沒有提供MOUSEEVENTF_MOVE量時光標(biāo)不會移動到相對位置,“光標(biāo)相對位置=光標(biāo)現(xiàn)行位置+新光標(biāo)位置”如果提供量“MOUSEEVENTF_ABSOLUTE”絕對位置,則會以“新光標(biāo)位置”為準(zhǔn)而不會添加“光標(biāo)現(xiàn)行位置”
 

  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函數(shù)主要用于對MOUSEEVENTF_MOVE于MOUSEEVENTF_ABSOLUTE

相對/絕對光標(biāo)位置修改的一個支持
 

  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函數(shù)主要用于模擬鼠標(biāo)點(diǎn)擊的過程,上面我提到“先左鍵按下 + 后左鍵抬起”在上面的代碼中你會看的清楚的不得了,如果相反你可以去嘗試一番會有什么后果與其說

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

  1. static int MakeDWord(int low, int high)  
  2. {  
  3.  return low + (high * Abs(~ushort.MaxValue));  
  4. }  
  5. static int Abs(int value)  
  6. {  
  7.  return ((value >> 31) ^ value) - (value >> 31);  
  8. }  
  9. MakeDWord / 合并整數(shù),函數(shù)主要是把兩個short合并為一個int,分為low、high兩部分 
  10.   
  11.   
  12. static bool screen_to_client(IntPtr hwnd, ref int x, ref int y)  
  13. {  
  14.  bool bRetVal = false;  
  15.  Point lpptPos = new Point(x, y);  
  16.  if ((bRetVal = ScreenToClient(hwnd, ref lpptPos)))  
  17.  {  
  18.   x = lpptPos.X;  
  19.   y = lpptPos.Y;  
  20.  }  
  21.  return bRetVal;  
  22. }  
  23. screen_to_client函數(shù)故名思意,它主要用于把屏幕上的坐標(biāo)轉(zhuǎn)換到窗口客戶上對應(yīng)坐標(biāo)  
  24. public const int WM_LBUTTONDOWN = 513; // 鼠標(biāo)左鍵按下  
  25. public const int WM_LBUTTONUP = 514; // 鼠標(biāo)左鍵抬起  
  26. public const int WM_RBUTTONDOWN = 516; // 鼠標(biāo)右鍵按下  
  27. public const int WM_RBUTTONUP = 517; // 鼠標(biāo)右鍵抬起  
  28. public const int WM_MBUTTONDOWN = 519; // 鼠標(biāo)中鍵按下  
  29. public const int WM_MBUTTONUP = 520; // 鼠標(biāo)中鍵抬起  
  30. public const int MOUSEEVENTF_MOVE = 0x0001; // 移動鼠標(biāo)    
  31. public const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠標(biāo)左鍵按下   
  32. public const int MOUSEEVENTF_LEFTUP = 0x0004; // 鼠標(biāo)左鍵抬起   
  33. public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠標(biāo)右鍵按下   
  34. public const int MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠標(biāo)右鍵抬起    
  35. public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // 鼠標(biāo)中鍵按下  
  36. public const int MOUSEEVENTF_MIDDLEUP = 0x0040; // 鼠標(biāo)中鍵抬起    
  37. public const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 絕對坐標(biāo)  
  38.  
  39.  
  40. [DllImport("user32.dll", SetLastError = true)]  
  41. public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);  
  42. [DllImport("user32.dll", SetLastError = true)]  
  43. public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);  
  44. [DllImport("user32.dll", SetLastError = true)]  
  45. public static extern int SetCursorPos(int x, int y);  
  46. [DllImport("user32.dll", SetLastError = true)]  
  47. public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt);  
  48. // [DllImport("user32", SetLastError = true)]  
  49. // public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); 

鼠標(biāo)右鍵單擊(靜默):

 

復(fù)制代碼代碼如下:

mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);  

 

鼠標(biāo)左鍵雙擊(靜默):

復(fù)制代碼代碼如下:

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);  

 

鼠標(biāo)移動(相對位置):

 

復(fù)制代碼代碼如下:

mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);  

 

鼠標(biāo)移動(絕對位置):

 

復(fù)制代碼代碼如下:

mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0);  

 

以上內(nèi)容比較多請認(rèn)真學(xué)習(xí),希望能夠幫助到大家。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通城县| 突泉县| 陕西省| 宾阳县| 建阳市| 东源县| 永修县| 犍为县| 杂多县| 望江县| 苍山县| 昌乐县| 南昌县| 沙洋县| 蒙城县| 洪湖市| 旬邑县| 宁远县| 东至县| 青海省| 漯河市| 石景山区| 铁力市| 比如县| 保德县| 三台县| 高淳县| 永善县| 历史| 德安县| 田东县| 旬阳县| 陇西县| 敦煌市| 德昌县| 陆川县| 滨海县| 沙田区| 沁水县| 柞水县| 宁波市|