问题描述:

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?

网友答案:

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.

网友答案:

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

相关阅读:
Top