2

我最近刚刚学会了如何获得鼠标位置,但是如果我移动我的窗口,它就会出现问题。例如,我想在鼠标坐标(x = 100,y = 100)的位置绘制一个点,这样系统将在窗口中的该坐标处绘制,这就是问题所在,因为鼠标位置是根据位置读取的的屏幕而不是窗口。如果我能以某种方式根据窗口而不是屏幕来获取鼠标坐标,那将解决问题。

照片

#include<graphics.h>
#include<iostream>
#include<windows.h>
using namespace std;

int main()
{
    initwindow(800,600);
    POINT CursorPosition;

    while(1)
    {
        GetCursorPos(&CursorPosition);

        cout << CursorPosition.x << endl;
        cout << CursorPosition.y << endl;

        if(GetAsyncKeyState(VK_LBUTTON)) {
        bar(CursorPosition.x, CursorPosition.y, CursorPosition.x+50, 
        CursorPosition.y+50);
        }

        delay(5);
        Sleep(5);
        system("cls");
    }
}
4

1 回答 1

1

这是一个简单的解决方案,向您展示如何将光标位置转换为窗口位置。此概念证明使用 GetForeGroundWindow()、GetCursorPosition() 和 ScreenToClient()。按住鼠标右键并在控制台窗口周围移动鼠标以查看输出

#include <iostream>
#include <windows.h>

int main()
{
    while (1)
    {
        if (GetAsyncKeyState(VK_RBUTTON))
        {
            POINT pnt;
            GetCursorPos(&pnt);
            ScreenToClient(GetForegroundWindow(), &pnt);

            std::cout << "x: " << pnt.x << " y: " << pnt.y << std::endl;

            Sleep(300);
        }
    }

    return 0;
}
于 2020-06-29T16:33:39.643 回答