星火微课系统客户端
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.2KB

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