1
votes

I want to prevent a window from being moved between monitors by checking for the message WM_MOVE and using the SetWindowPos function to keep the window within the bounds of the display. When I try to do this, the window briefly flashes where the mouse is and snaps to a small area on the bottom of the screen. I'm not sure why this is happening, since the code is just like any other collision detection:

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 is basically a pointer to a Rect that contains the coordinates of the display, and uNormalWidth/uNormalHeight are the width and height of the window in windowed mode.

1
You might have better results if you intercept WM_WINDOWPOSCHANGING. - Jonathan Potter
What are the values of uNormalWidth and uNormalHeight? - PMF

1 Answers

1
votes

WM_MOVE

lParam The x and y coordinates of the upper-left corner of the client area of the window. The low-order word contains the x-coordinate while the high-order word contains the y coordinate.

Each time you enter WM_MOUSE, you Move your windows +8 pixels to the right and +30 pixels to the bottom (no menu in my code). That's the Width of the left sizing border and the height of the top sizing border + Title bar.

That move triggers a recursive chain of WM_MOVE processing, eventually ending up with some coords.

What you could do:

1. Follow Jonathan' advice. WM_MOVE is not the message you are looking for, it's WM_WINDOWPOSCHANGING.

2. Use the NON-client coordinates:

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()) {

[...]