Forgot password?
 Create new account
View 132|Reply 1

[c++]获取两次点击的坐标之差

[Copy link]

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2022-5-24 10:17:25 |Read mode
Last edited by hbghlyj at 2022-5-24 21:24:00刚刚学.......啥都不懂.....写一个非常初级的小程序....记录一下

功能:
最小化控制台
鼠标点击第一次,记录坐标pt1
鼠标点击第二次,记录坐标pt2
输出坐标之差
最大化控制台

  1. #include<iostream>
  2. #include<windows.h>
  3. POINT pt1{};
  4. POINT pt2{};
  5. bool was_pressed = false;
  6. bool IsKeyPressed() {
  7.     bool is_pressed = (GetAsyncKeyState(VK_LBUTTON) & 0x8000) != 0;
  8.     if (was_pressed && !is_pressed) {
  9.         was_pressed = is_pressed;
  10.         return true;
  11.     }
  12.     was_pressed = is_pressed;
  13.     return false;
  14. }
  15. int main(){
  16.     ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
  17.     while (!IsKeyPressed()){}
  18.     GetCursorPos(&pt1);
  19.     while (!IsKeyPressed()){};
  20.     GetCursorPos(&pt2);
  21.     std::cout << '(' << (pt2.x-pt1.x) << ',' << (pt2.y-pt1.y) << ')' << std::endl;
  22.     ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
  23.     return 0;
  24. }
Copy the Code



编译的结果:
$type getcursor_win.zip (14.26 KB, Downloads: 26)
(这个zip里面只有一个exe文件.我起初提交exe附件时,提示“不支持此扩展名” ,于是改为压缩包)

遗憾:
1.最后需要手动复制. 不知道怎么实现自动复制结果...
2.这是一个控制台程序. 下次得学习一下怎么制作窗口程序,可以参考一下教程


参考资料:
cplusplus.com/doc/tutorial/ (语言基础)
ShowWindow (Microsoft doc)
GetCursorPos (Microsoft doc)
GetAsyncKeyState (a post on cplusplus forum)

后记:
用DOSKEY创建alias
Screenshot 2022-05-24 at 19-43-37 iciam07.pdf.png

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2022-9-16 17:31:15

Mathematica

Get the current mouse position:
  1. CurrentValue["MousePosition"]
Copy the Code

手机版Mobile version|Leisure Math Forum

2025-4-20 22:09 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list