C++模拟真人鼠标轨迹

一.API跨语言平台支持

`鼠标轨迹API `[https://winsdk.cn/]()底层实现采用 C/C++ 语言,利用其高性能和系统级访问能力,开发出高效的鼠标轨迹模拟算法。通过将算法封装为 DLL(动态链接库),可以方便地在不同的编程环境中调用,实现跨语言的兼容性。

通过 DLL 封装,开发者可以在 C++、Python、易语言、按键精灵等多种编程语言中使用鼠标轨迹模拟技术。这种封装方式提供了一种简便的接口,使得不同背景的开发者都能够轻松集成和使用这一技术。

二.鼠标轨迹 Demo 效果演示

  • 1.开始坐标为(100,100),结束坐标为(800,800),通过调用接口获得 4 条鼠标轨迹
  • 2.开始坐标为(1000,100),结束坐标为(800,800),通过调用接口获得 2 条鼠标轨迹

![](https://img2024.cnblogs.com/blog/641604/202409/641604-20240925113048924-1304306673.gif)

三.C++鼠标轨迹 Demo 下载

百度云盘下载
夸克云盘下载
123云盘下载

四.C++代码演示

1.头文件

  1. // Copyright (c) 2024
  2. //
  3. // 保留所有权利。
  4. //
  5. // 在满足以下条件的情况下,允许重新使用和分发:
  6. //
  7. // 1. 代码必须保留上述版权声明、作者声明和/或最初发布的网址。
  8. // 2. 不得将代码用于商业目的,除非与版权所有者签订了书面协议。
  9. // 3. 修改过的源版本必须清楚地标识出修改的部分,并且不得以任何方式表明版权所有者支持或认可这些修改。
  10. //
  11. #ifndef _SN_SDK_H__
  12. #define _SN_SDK_H__
  13. #include <windows.h>
  14. //返回参数
  15. typedef struct SN_RESULT {
  16. int code; //错误码,如果为 0 表示成功,否则表示错误号
  17. char message[4096]; //错误信息,如果为 "OK" 表示成功,否则返回错误信息
  18. }SN_RESULT;
  19. //坐标参数
  20. typedef struct SN_POINT
  21. {
  22. int x; //屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  23. int y; //屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  24. }SN_POINT;
  25. //轨迹参数
  26. typedef struct SN_POINT_PARAMS
  27. {
  28. struct SN_POINT point;//屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  29. int delayTime; //延时时间(单位:毫秒),仅供参考
  30. }SN_POINT_PARAMS;
  31. /*创建句柄
  32. *
  33. * 参数:
  34. * [in] szKey: 卡密(购买卡密:https://shop.4yuns.com/links/7C9F16B7)
  35. * [in] pOnnxFilePath:设置 onnx 模型文件路径,如果设置为 NULL,默认和 DLL文件同级目录
  36. * [out] pResult: 返回错误信息,参数pResult.code(错误码)如果为 0 表示成功,否则表示错误号;
  37. *
  38. * 返回值:成功返回句柄,失败返回NULL
  39. *
  40. */
  41. HANDLE WINAPI apiSNCreateHandle(char* szKey, char* pOnnxFilePath, SN_RESULT* pResult);
  42. /*获取鼠标移动轨迹
  43. *
  44. * 参数:
  45. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  46. * [in] startPoint: 开始坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  47. * [in] endPoint: 结束坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  48. * [out] points: 轨迹数组,如果数组中元素 point 出现(-1,-1),表示鼠标轨迹结束
  49. *
  50. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  51. *
  52. */
  53. SN_RESULT WINAPI apiSNMouseMove(HANDLE handle, SN_POINT startPoint, SN_POINT endPoint, SN_POINT_PARAMS* points);
  54. /*获取版本号
  55. *
  56. * 参数:
  57. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  58. * [out] szVersion: 版本号
  59. *
  60. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  61. *
  62. */
  63. SN_RESULT WINAPI apiSNGetVersion(HANDLE handle, char* szVersion);
  64. /*获取错误信息
  65. *
  66. * 参数:
  67. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  68. *
  69. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  70. *
  71. */
  72. SN_RESULT WINAPI apiSNGetError(HANDLE handle);
  73. /*释放句柄(内存)
  74. *
  75. * 参数:
  76. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  77. *
  78. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  79. *
  80. */
  81. SN_RESULT WINAPI apiSNDestroyHandle(HANDLE handle);
  82. #endif // !_SN_SDK_H__

2.CPP文件

  1. /****************************************************************************************/
  2. @SDK功能描述:鼠标轨迹
  3. /******************************************************************************************/
  4. //
  5. #include <iostream>
  6. #include "include/SNSDK.h"
  7. #pragma comment(lib,"./dll/x86/SNSDK.lib")
  8. int main()
  9. {
  10. SN_RESULT pResult = { 0 };
  11. char* pKey = (char*)"SNKJaURwbfuhcaQaCMJVhdByPZMVsZdqRUMjfWrxY1A5";
  12. //1.创建句柄
  13. HANDLE pHandle = apiSNCreateHandle(pKey,&pResult);
  14. if (!pHandle)
  15. {
  16. printf("error code:%d message:%s \n",pResult.code,pResult.message);
  17. return -__LINE__;
  18. }
  19. //2.获取轨迹
  20. SN_POINT startPoint = { 100,100 }; //开始坐标
  21. SN_POINT endPoint = { 800,800 }; //结束坐标
  22. SN_POINT_PARAMS track[4096] = { 0 };//轨迹
  23. pResult = apiSNMouseMove(pHandle, startPoint, endPoint, track);
  24. if (pResult.code != 0)
  25. {
  26. printf("error code:%d message:%s \n", pResult.code, pResult.message);
  27. return -__LINE__;
  28. }
  29. //3.鼠标根据轨迹移动,轨迹最后一个点(-1,-1)
  30. for (int i = 0; i < 4096; i++)
  31. {
  32. struct SN_POINT point = track[i].point;
  33. if (point.x <= 0 && point.y <= 0)//轨迹最后一个点(-1,-1)
  34. break;
  35. printf("x:%d y:%d delay_time:%d \n", point.x, point.y, track[i].delayTime);
  36. }
  37. //4.释放内存
  38. pResult = apiSNDestroyHandle(pHandle);
  39. return 1;
  40. }
  41. /*
  42. x:100 y:100 delay_time:0
  43. x:100 y:98 delay_time:10
  44. x:103 y:98 delay_time:15
  45. x:111 y:98 delay_time:16
  46. x:116 y:101 delay_time:15
  47. x:122 y:104 delay_time:2
  48. x:129 y:107 delay_time:13
  49. x:135 y:109 delay_time:2
  50. x:144 y:112 delay_time:14
  51. x:155 y:117 delay_time:2
  52. x:167 y:123 delay_time:14
  53. x:180 y:128 delay_time:2
  54. x:193 y:134 delay_time:13
  55. x:209 y:138 delay_time:2
  56. x:225 y:144 delay_time:13
  57. x:238 y:149 delay_time:5
  58. x:254 y:157 delay_time:10
  59. x:269 y:162 delay_time:5
  60. x:282 y:168 delay_time:11
  61. x:298 y:175 delay_time:5
  62. x:311 y:180 delay_time:10
  63. x:326 y:185 delay_time:6
  64. x:341 y:193 delay_time:9
  65. x:369 y:211 delay_time:15
  66. x:396 y:231 delay_time:16
  67. x:419 y:251 delay_time:16
  68. x:442 y:270 delay_time:16
  69. x:461 y:285 delay_time:17
  70. x:481 y:300 delay_time:15
  71. x:491 y:311 delay_time:15
  72. x:502 y:319 delay_time:2
  73. x:513 y:329 delay_time:14
  74. x:523 y:343 delay_time:2
  75. x:535 y:355 delay_time:14
  76. x:546 y:369 delay_time:0
  77. x:558 y:383 delay_time:15
  78. x:570 y:397 delay_time:2
  79. x:582 y:411 delay_time:13
  80. x:596 y:427 delay_time:2
  81. x:608 y:443 delay_time:14
  82. x:620 y:459 delay_time:5
  83. x:633 y:476 delay_time:10
  84. x:645 y:490 delay_time:5
  85. x:656 y:503 delay_time:11
  86. x:666 y:515 delay_time:5
  87. x:675 y:527 delay_time:11
  88. x:684 y:538 delay_time:5
  89. x:694 y:551 delay_time:11
  90. x:702 y:565 delay_time:5
  91. x:710 y:577 delay_time:11
  92. x:716 y:588 delay_time:5
  93. x:723 y:598 delay_time:11
  94. x:728 y:606 delay_time:5
  95. x:733 y:615 delay_time:11
  96. x:738 y:622 delay_time:5
  97. x:743 y:631 delay_time:11
  98. x:747 y:637 delay_time:5
  99. x:750 y:644 delay_time:11
  100. x:753 y:652 delay_time:5
  101. x:756 y:659 delay_time:10
  102. x:759 y:666 delay_time:5
  103. x:761 y:673 delay_time:11
  104. x:764 y:680 delay_time:5
  105. x:766 y:687 delay_time:11
  106. x:768 y:694 delay_time:5
  107. x:769 y:701 delay_time:10
  108. x:771 y:708 delay_time:5
  109. x:772 y:714 delay_time:11
  110. x:773 y:722 delay_time:5
  111. x:774 y:729 delay_time:10
  112. x:777 y:743 delay_time:16
  113. x:778 y:755 delay_time:15
  114. x:778 y:764 delay_time:16
  115. x:780 y:775 delay_time:16
  116. x:781 y:784 delay_time:16
  117. x:781 y:785 delay_time:15
  118. x:781 y:789 delay_time:2
  119. x:781 y:790 delay_time:13
  120. x:781 y:792 delay_time:2
  121. x:782 y:796 delay_time:14
  122. x:782 y:796 delay_time:2
  123. x:782 y:797 delay_time:14
  124. x:782 y:798 delay_time:15
  125. x:782 y:800 delay_time:311
  126. x:784 y:800 delay_time:16
  127. x:784 y:800 delay_time:5
  128. x:785 y:800 delay_time:10
  129. x:786 y:800 delay_time:5
  130. x:786 y:800 delay_time:11
  131. x:788 y:800 delay_time:6
  132. x:789 y:800 delay_time:9
  133. x:790 y:800 delay_time:5
  134. x:791 y:800 delay_time:10
  135. x:793 y:800 delay_time:16
  136. x:795 y:800 delay_time:16
  137. x:796 y:800 delay_time:15
  138. x:797 y:800 delay_time:15
  139. x:797 y:800 delay_time:2
  140. x:798 y:800 delay_time:15
  141. x:798 y:800 delay_time:30
  142. x:799 y:800 delay_time:15
  143. x:799 y:800 delay_time:15
  144. */

posted @ 2024-09-25 11:32  猿说编程  阅读(1)  评论(0编辑  收藏  举报