[英]Lock window to a specific display (winapi)
我想通过检查消息WM_MOVE并使用SetWindowPos函数将窗口保持在显示范围内来防止窗口在监视器之间移动。 当我尝试执行此操作时,窗口会短暂闪烁在鼠标所在的位置,并捕捉到屏幕底部的一小块区域。 我不确定为什么会这样,因为代码与任何其他碰撞检测一样:
case WM_MOVE:
{
int nX = (int)(short)LOWORD(lParam);
int nY = (int)(short)HIWORD(lParam);
if (nX < pDisplayDevice->getLeft()) {
nX = pDisplayDevice->getLeft();
} else if (nX + int(uNormalWidth) > pDisplayDevice->getRight()) {
nX = pDisplayDevice->getRight() - int(uNormalWidth);
}
if (nY < pDisplayDevice->getTop()) {
nY = pDisplayDevice->getTop();
} else if (nY + int(uNormalHeight) > pDisplayDevice->getBottom()) {
nY = pDisplayDevice->getBottom() - int(uNormalHeight);
}
SetWindowPos(hWnd, 0, nX, nY, uNormalWidth, uNormalHeight, 0);
}
break;
pDisplayDevice基本上是指向包含显示坐标的Rect的指针,而uNormalWidth / uNormalHeight是窗口模式下窗口的宽度和高度。
lParam窗口客户区左上角的x和y坐标。 低位单词包含x坐标,而高位单词包含y坐标。
每次输入WM_MOUSE时,都将窗口向右移动+8像素,向底部移动+30像素(我的代码中没有菜单)。 那就是左边的尺寸边框的宽度和顶端的尺寸边框+标题栏的高度。
此举触发了WM_MOVE处理的递归链,最终导致一些协调。
您可以做什么:
1.遵循乔纳森的建议。 WM_MOVE不是您要查找的消息,它是WM_WINDOWPOSCHANGING 。
2.使用非客户端坐标:
int nX = (int)(short)LOWORD(lParam);
int nY = (int)(short)HIWORD(lParam);
RECT rect;
GetWindowRect( hWnd, &rect );
nX = rect.left;
nY = rect.top;
if (nX < pDisplayDevice->getLeft()) {
[...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.