C++에서 마우스 위치 가져오기

Sheeraz Gul 2023년10월12일
C++에서 마우스 위치 가져오기

이 자습서에서는 C++에서 마우스 위치를 가져오는 방법을 설명합니다.

C++에서 마우스 위치 가져오기

C++는 마우스 커서의 x 및 y 위치를 가져오는 GetCursorPos 메서드를 제공합니다. 이 방법은 사용하기 매우 쉽습니다. Point를 선언한 다음 메서드에 전달해야 하며 메서드는 마우스 위치의 x 및 y 지점을 반환합니다.

C++에서 마우스 위치를 가져오기 위해 GetCursorPos() 메서드를 사용하는 예제를 시도해 봅시다.

#include <windows.h>

#include <iostream>

using namespace std;

int main() {
  POINT MousePoint;
  if (GetCursorPos(&MousePoint)) {
    cout << MousePoint.x << "," << MousePoint.y << "\n";
  }
  return 0;
}

위의 코드는 전체 화면을 분석하여 마우스 커서의 x 및 y 지점을 얻으려고 시도합니다. 출력을 참조하십시오.

424,266

게임이나 애플리케이션을 개발할 때 일반적으로 마우스 위치를 얻는 것이 필요하며 GameMaker:Studio는 주어진 화면 외부에서 마우스 좌표에 액세스하는 것을 허용하지 않습니다. 이 경우 아래 코드를 사용하여 마우스 좌표를 얻을 수 있습니다.

#include <WinUser.h>
#include <Windows.h>
#define EXPORT extern "C" _declspec(dllexport)

EXPORT double GetMouse(double mode) {
  POINT MousePoint;
  GetCursorPos(&MousePoint);
  if (mode == 0) {
    double Mouse_X = MousePoint.x;
    return Mouse_X;
  } else {
    double Mouse_Y = MousePoint.y;
    return Mouse_Y;
  }
}

위의 코드는 Windows의 자식 헤더인 WinUser.h를 사용하며 화면 안팎의 마우스 좌표를 가져오는 데 도움이 됩니다. 출력을 참조하십시오.

630,368

getcursorpos 데모

작가: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook