How to prevent window resizing temporarily?
I have a window which can be resized, but there are some situations when resizing is not possible because of the appli开发者_运维百科cation state. Is there a way to prevent resizing the window temporarily?
I want to disable resizing by all means available to the users, which include window menu, dragging edges by mouse, user initiated window tiling performed by OS - and perhaps some other I am not aware of?
To retain the look of the window border and still prevent re-size (and cursor change), catch WM_NCHITTEST
, pass it to DefWindowProc, if the returned code is one of the size constants, change the real return to something else, HTCLIENT for example
One way is to use GetWindowLong() with GWL_STYLE
flag to get the window style and
reset/remove any styles you need, ie the WS_THICKFRAME
style so that the window can't be resized.
You apply the new style with SetWindowLong.
Another possibility is to handle the WM_GETMINMAXINFO message and set the MINMAXINFO struct so that both min and max size of the window is the current size. Then the user can't resize the window either.
Following code in the window procedure seems to handle the case of user dragging the window edge/corner:
case WM_SIZING:
RECT &rc = *(LPRECT) lParam;
RECT windowRect;
GetWindowRect(hwnd, &windowRect);
rc = windowRect;
return 0;
I did not find anything yet to prevent the system from resizing the window when tiling/cascading windows. I hoped following might do the trick, but it seems it does not:
case WM_SIZE:
return TRUE;
I guess I can find similar measure for other cases, but at least I would need to know the exhaustive list of messages which can result in a window changing its size.
Also, while this really prevents the window from resizing, I would rather prevent the user from even initiating the resize, than apparently letting him to resize and then refusing to do so.
精彩评论