# 如何锁定鼠标范围至目标窗体
最近更新dotool的时候,
有同学说需要一个能把鼠标限制在魔兽窗体里的东西,
小窗口的魔兽看起录像来是比较方便,
遂决定想想办法,
google + msdn发现有现成的api可以用。
// 这个方法可以取得目标窗体的矩形,其中rect为一个结构体
1
BOOL GetWindowRect(
2
HWND hWnd,
3
LPRECT lpRect
4
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 结构体RECT
1
typedef struct _RECT {
2
LONG left;
3
LONG top;
4
LONG right;
5
LONG bottom;
6
} RECT;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
通过目标窗体的句柄,
可以利用GetWindowRect来填充这个RECT,
从而利用下面的方法来限制住鼠标的操作范围。
// 这个方法用来限制鼠标操作范围
1
BOOL ClipCursor(
2
CONST RECT* lpRect
3
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下面是对于魔兽争霸这款游戏,
如何限制鼠标在小窗口范围内移动的简单sample:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)