views:

366

answers:

4

I have a window which can be resized, but there are some situations when resizing is not possible because of the application 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?

A: 

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.

Suma
This doesn't necessarily work - I've implemented this and instead my window grows a little each time WM_SIZING gets triggered. Also, your code is a little verbose, why not GetWindowRect straight into the lParam address?
jheriko
+2  A: 

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.

Nick D
Great. Removing WS_SIZEBOX from the style, setting the new style with SetWindowLong disabled the resizing completely. To be safe, after calling SetWindowLong I update the window using AdjustWindowRectEx / SetWindowPlacement / SetWindowPos / RedrawWindow.
Suma
This doesn't necessarily work - I have a window with only WS_CHILD, WS_CLIPCHILDREN and WS_CLIPSIBLINGS and it is still resizable.
jheriko
@jheriko, I haven't tested it on child windows. Perhaps the child window can still receive WM_SIZING and WM_SIZE messages.
Nick D
+1  A: 

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.

Stefan
+1  A: 

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

Anders
Best answer here - handles the small polish details as well as actually working.
jheriko