星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MouseEventCommon.cs 3.0KB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Runtime.InteropServices;
  2. namespace Common.system
  3. {
  4. public class MouseEventCommon
  5. {
  6. //[DllImport("user32.dll")]
  7. //static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
  8. [DllImport("user32")]
  9. private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
  10. /// <summary>
  11. /// 设置鼠标的坐标
  12. /// </summary>
  13. /// <param name="x">横坐标</param>
  14. /// <param name="y">纵坐标</param>
  15. /// <returns></returns>
  16. [DllImport("user32.dll")]
  17. public static extern int SetCursorPos(int x, int y);
  18. /// <summary>
  19. /// 获取鼠标的坐标
  20. /// </summary>
  21. /// <returns>获取成功返回真</returns>
  22. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  23. public static extern bool GetCursorPos(out Point pt);
  24. /// <summary>
  25. /// 鼠标点击左键
  26. /// </summary>
  27. /// <param name="dx">坐标x</param>
  28. /// <param name="dy">坐标Y</param>
  29. /// <param name="data"></param>
  30. public static void MouseLefClickEvent(int dx, int dy, int data)
  31. {
  32. GetCursorPos(out Point pointRecord);
  33. mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0);
  34. mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0);
  35. }
  36. public static void MouseText(int dx, int dy)
  37. {
  38. mouse_event((int)MouseEventFlag.LeftDown, dx, dy, 0, 0);
  39. mouse_event((int)MouseEventFlag.LeftUp, dx, dy, 0, 0);
  40. }
  41. /// <summary>
  42. /// 鼠标点击中中键
  43. /// </summary>
  44. /// <param name="data"></param>
  45. public static void MouseMiddleClickEvent(int data)
  46. {
  47. GetCursorPos(out Point pointRecord);
  48. mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0);
  49. mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0);
  50. }
  51. public struct Point
  52. {
  53. public int X;
  54. public int Y;
  55. public Point(int x, int y)
  56. {
  57. this.X = x;
  58. this.Y = y;
  59. }
  60. }
  61. enum MouseEventFlag : uint
  62. {
  63. //移动鼠标
  64. Move = 0x0001,
  65. //模拟鼠标左键按下
  66. LeftDown = 0x0002,
  67. //模拟鼠标左键抬起
  68. LeftUp = 0x0004,
  69. //模拟鼠标右键按下
  70. RightDown = 0x0008,
  71. //模拟鼠标右键抬起
  72. RightUp = 0x0010,
  73. //模拟鼠标中键按下
  74. MiddleDown = 0x0020,
  75. //模拟鼠标中键抬起
  76. MiddleUp = 0x0040,
  77. XDown = 0x0080,
  78. XUp = 0x0100,
  79. Wheel = 0x0800,
  80. VirtualDesk = 0x4000,
  81. //标示是否采用绝对坐标
  82. Absolute = 0x8000
  83. }
  84. }
  85. }