原文 https://blog.csdn.net/baidu_38494049/article/details/82930099
如果有小伙伴看不懂太多英語,還想知道GetCursorPos函數的用法,那么往下看吧。
msdn網址:https://docs.microsoft.com/zh-cn/windows/desktop/api/winuser/nf-winuser-getcursorpos
函數原型:
BOOL GetCursorPos(
LPPOINT lpPoint
);
參數:
lpPoint:一個指向POINT(struct)的指針,返回光標位置。
POINT這個struct里包含兩個變量:x和y。使用GetCursorPos獲取位置到一個POINT型變量之后,x就是鼠標指針的x坐標,y就是鼠標指針的y坐標。
返回值:
如果成功,返回非0;如果失敗,返回0。
頭文件:WinUser.h(在寫程序時#include <Windows.h>就可以使用這個函數了)
程序實例:
對於初學者來說,講太多術語不如直接上實例:
Win32 C++ 控制台應用程序。開發環境:VC/VS。
#include <iostream> #include <Windows.h>
using namespace std; void main(void) { SetConsoleTitleA("Get Cursor Postition"); POINT pt; BOOL bReturn = GetCursorPos(&pt); //獲取鼠標指針位置到pt
if (bReturn != 0) //如果函數執行成功
cout << "Cursor postition is: " << pt.x << "," << pt.y << endl; //顯示pt中的數據
else //如果函數沒有執行成功
cout << "Error!" << endl; //報錯
cout << "Press any key to exit ..."; system("pause > nul"); }
運行結果(在函數執行成功的情況下):