7-2-5 Windows鼠标相关函数

Windows鼠标相关函数

1.设置鼠标光标位置(相对于屏幕)

BOOL SetCursorPos(
[in] int X,
[in] int Y
);

由于转换需要的是屏幕坐标,而获取的是窗口坐标,因此需要一个转换函数。

BOOL ClientToScreen(
[in] HWND hWnd,
[in, out] LPPOINT lpPoint
);
使用案例
switch(message)
{
case WM_MOUSEMOVE: //鼠标移动消息
g_iX = LOWORD(lParam); //取得鼠标X坐标
g_iY = HIWORD(lParam); //取得鼠标Y坐标
Point temp{g_iX,g_iY};
ClientToScreen(hwnd, &temp);
SetCursorPos(temp.x,temp.y);
break;
}

2.显示与隐藏光标

int ShowCursor(
[in] BOOL bShow
);

//example
ShowCursor(TRUE); //显示光标
ShowCursor(FALSE); //隐藏光标

3.获取窗口外鼠标信息

正常情况下,鼠标指针位于哪个窗口区域内,鼠标消息就自动发给哪个窗口。如果调用了SetCapture,之后无论鼠标的位置在哪,鼠标消息都发给指定的这个窗口,直到调用ReleaseCapture或者调用SetCapture设置另一个窗口为止。

//catch
HWND SetCapture(
[in] HWND hWnd
);

//release
BOOL ReleaseCapture();

4.限制光标移动区域

//function
BOOL ClipCursor(
[in, optional] const RECT *lpRect
);

//struct
typedef struct tagRECT
{
LONG left; //左上点x
LONG top; //左上点y
LONG right; //右下点x
LONG bottom; //右下点y
} RECT

5.取得窗口外部区域及内部区域的函数