星火直播PC
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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// <param name="lpPoint">传址参数,坐标point类型</param>
  22. /// <returns>获取成功返回真</returns>
  23. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  24. public static extern bool GetCursorPos(out POINT pt);
  25. /// <summary>
  26. /// 鼠标点击左键
  27. /// </summary>
  28. /// <param name="dx">坐标x</param>
  29. /// <param name="dy">坐标Y</param>
  30. /// <param name="data"></param>
  31. public static void MouseLefClickEvent(int dx, int dy, int data)
  32. {
  33. GetCursorPos(out POINT pointRecord);
  34. mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0);
  35. mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0);
  36. }
  37. public static void MouseText(int dx, int dy)
  38. {
  39. mouse_event((int)MouseEventFlag.LeftDown, dx, dy, 0, 0);
  40. mouse_event((int)MouseEventFlag.LeftUp, dx, dy, 0, 0);
  41. }
  42. /// <summary>
  43. /// 鼠标点击中中键
  44. /// </summary>
  45. /// <param name="data"></param>
  46. public static void MouseMiddleClickEvent(int data)
  47. {
  48. GetCursorPos(out POINT pointRecord);
  49. mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0);
  50. mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0);
  51. }
  52. public struct POINT
  53. {
  54. public int X;
  55. public int Y;
  56. public POINT(int x, int y)
  57. {
  58. this.X = x;
  59. this.Y = y;
  60. }
  61. }
  62. enum MouseEventFlag : uint
  63. {
  64. //移动鼠标
  65. Move = 0x0001,
  66. //模拟鼠标左键按下
  67. LeftDown = 0x0002,
  68. //模拟鼠标左键抬起
  69. LeftUp = 0x0004,
  70. //模拟鼠标右键按下
  71. RightDown = 0x0008,
  72. //模拟鼠标右键抬起
  73. RightUp = 0x0010,
  74. //模拟鼠标中键按下
  75. MiddleDown = 0x0020,
  76. //模拟鼠标中键抬起
  77. MiddleUp = 0x0040,
  78. XDown = 0x0080,
  79. XUp = 0x0100,
  80. Wheel = 0x0800,
  81. VirtualDesk = 0x4000,
  82. //标示是否采用绝对坐标
  83. Absolute = 0x8000
  84. }
  85. }
  86. }