1

Is there any way to enforce a minimum size (width and height) on a window?

I've seen applications that don't allow the resizing of their windows below a point, and do it without flickering or anything. Also if the resolution was below those minimums, they wouldn't allow maximizations, and would instead restore themselves to filling the screen but never maximized.

How can this be achieved?

Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72

2 Answers2

1

I haven't done this myself, but something like this should work:

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_SIZING:
        {
            RECT r = *((RECT *)lParam);

            if (r.right - r.left > MAX_WIDTH)
                ((RECT *)lParam)->right = r.left + MAX_WIDTH;

            if (r.bottom - r.top > MAX_HEIGHT)
                ((RECT *)lParam)->bottom = r.top + MAX_HEIGHT;

            break;
        }
    }

    return DefWindowProc (hwnd, msg, wParam, lParam);
}

Basically, if the rectangle the window is going to be exceeds the maximum width or height, it adjusts it and sends it on, so it shouldn't cause flickering. Again, I've never tried this, so I'm not completely sure.

As Luke points out, you can save a lot of work by handling WM_GETMINMAXINFO to set the defaults instead of reinventing the wheel.

chris
  • 60,560
  • 13
  • 143
  • 205
  • @GRIGORE-TURBODISEL, if you post your adjustments, I can update my answer to help those that come looking for a native winapi solution in the future. – chris May 20 '12 at 02:40
  • Did a couple of `GetSystemMetrics()` to ensure there's enough space on the screen to enforce those minimums. There's also a `lpfnWndProc` cause I spent some time searching for it, and I'm trying to see what happens when maximizing the window. Sorry if I sound like a total noob but I'm in my very first days of desktop programming. – Silviu-Marian May 20 '12 at 02:48
  • 8
    Just FYI the "proper" technique to handle this is the [WM_GETMINMAXINFO](http://msdn.microsoft.com/en-us/library/windows/desktop/ms632626%28v=vs.85%29.aspx) message. – Luke May 20 '12 at 03:09
  • @Luke, thanks. I've seen the message, but I've never really looked at it. It's perfectly clear when you read the article. – chris May 20 '12 at 03:16
0

You can use GetWindowRect to check the size of the window and if it bigger or smaller than you want you can just use a SetWindowLong and add the DS_FIXEDSYS so you will block the sizing of the window