当前位置:   article > 正文

C#实现模拟鼠标点击事件(点击桌面的其他程序 )

c#实现模拟鼠标点击事件(点击桌面的其他程序 )
  1. 注释感觉已经很清楚了,有不懂的欢迎评论 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.ComponentModel;
  4. 4 using System.Data;
  5. 5 using System.Drawing;
  6. 6 using System.Linq;
  7. 7 using System.Runtime.InteropServices;
  8. 8 using System.Text;
  9. 9 using System.Threading;
  10. 10 using System.Threading.Tasks;
  11. 11 using System.Windows.Forms;
  12. 12
  13. 13 namespace WindowsFormsApp1
  14. 14 {
  15. 15 public partial class Form1 : Form
  16. 16 {
  17. 17 public Form1()
  18. 18 {
  19. 19 InitializeComponent();
  20. 20 this.Hide();
  21. 21 }
  22. 22 /// <summary>
  23. 23 /// 引用user32.dll动态链接库(windows api),
  24. 24 /// 使用库中定义 API:SetCursorPos
  25. 25 /// </summary>
  26. 26 [DllImport("user32.dll")]
  27. 27 private static extern int SetCursorPos(int x, int y);
  28. 28 /// <summary>
  29. 29 /// 移动鼠标到指定的坐标点
  30. 30 /// </summary>
  31. 31 public void MoveMouseToPoint(Point p)
  32. 32 {
  33. 33 SetCursorPos(p.X, p.Y);
  34. 34 }
  35. 35 /// <summary>
  36. 36 /// 设置鼠标的移动范围
  37. 37 /// </summary>
  38. 38 public void SetMouseRectangle(Rectangle rectangle)
  39. 39 {
  40. 40 System.Windows.Forms.Cursor.Clip = rectangle;
  41. 41 }
  42. 42 /// <summary>
  43. 43 /// 设置鼠标位于屏幕中心
  44. 44 /// </summary>
  45. 45 public void SetMouseAtCenterScreen()
  46. 46 {
  47. 47 //当前屏幕的宽高
  48. 48 int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
  49. 49 int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
  50. 50 //设置鼠标的x,y位置
  51. 51 loginx = winWidth / 3 * 2;
  52. 52 loginy = winHeight / 4 + 5;
  53. 53 Point centerP = new Point(loginx, loginy);
  54. 54 //移动鼠标
  55. 55 MoveMouseToPoint(centerP);
  56. 56 }
  57. 57 //点击事件
  58. 58 [DllImport("User32")]
  59. 59 //下面这一行对应着下面的点击事件
  60. 60 // public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
  61. 61 public extern static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
  62. 62 public const int MOUSEEVENTF_LEFTDOWN = 0x2;
  63. 63 public const int MOUSEEVENTF_LEFTUP = 0x4;
  64. 64 public enum MouseEventFlags
  65. 65 {
  66. 66 Move = 0x0001, //移动鼠标
  67. 67 LeftDown = 0x0002,//模拟鼠标左键按下
  68. 68 LeftUp = 0x0004,//模拟鼠标左键抬起
  69. 69 RightDown = 0x0008,//鼠标右键按下
  70. 70 RightUp = 0x0010,//鼠标右键抬起
  71. 71 MiddleDown = 0x0020,//鼠标中键按下
  72. 72 MiddleUp = 0x0040,//中键抬起
  73. 73 Wheel = 0x0800,
  74. 74 Absolute = 0x8000//标示是否采用绝对坐标
  75. 75 }
  76. 76 //鼠标将要到的x,y位置
  77. 77 public static int loginx, loginy;
  78. 78 private void Form1_Load(object sender, EventArgs e)
  79. 79 {
  80. 80
  81. 81
  82. 82 //移动鼠标
  83. 83 SetMouseAtCenterScreen();
  84. 84
  85. 85
  86. 86 //mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), loginx, loginy, 0, IntPtr.Zero);
  87. 87
  88. 88 //mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), loginx, loginy, 0, IntPtr.Zero);
  89. 89 //点击两次
  90. 90 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0);
  91. 91
  92. 92 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, loginx, loginy, 0, 0);
  93. 93
  94. 94 }
  95. 95 }
  96. 96 }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/309821
推荐阅读
相关标签
  

闽ICP备14008679号