Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to handle a windows message, such as WM_SIZING, WM_SIZE, etc. Mostly those related to or is called when the width or height is changed. I have tried to set the value using MoveWindow and SetWindowPos but nothing is happening, instead of the windows flickers and return to default. How do I force the width or height to be fixed but not both is my challenge.

What I have tried:

I tried this

((MINMAXINFO*)message.lParam())->ptMinTrackSize.x = m_fixedSize.width();

((MINMAXINFO*)message.lParam())->ptMinTrackSize.y = m_fixedSize.height();
Posted
Updated 1-Sep-18 22:35pm

1 solution

The easiest way is to handle the WM_SIZE message, which is called after the window receives its new size, but before it is repainted. You simply compare the new width to your desired width. If it is identical, do nothing. If it differs, call the SetWindowPos() API with the new height (received in the WM_SIZE message) and your desired width.

For example, the following uses the Message Crackers in <windowsx.h> to parse the message:

C++
static void onSize(HWND hwnd, UINT /*state*/, int cx, int cy)
{
    /* code to prevent the window from exceeding a "full text page" */
    BOOL resize = FALSE;

    if (cx > wintext_max_width)
    {
        resize = TRUE;
        cx = wintext_max_width;
    }
    if (cy > wintext_max_height)
    {
        resize = TRUE;
        cy = wintext_max_height;
    }
    if (resize)
        SetWindowPos(hwnd,
                     GetNextWindow(hwnd, GW_HWNDPREV),
                     0, 0,
                     cx, cy,
                     SWP_NOMOVE);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        /* other messages go here */
        case WM_SIZE:
            return HANDLE_WM_SIZE(hwnd, wParam, lParam, onSize);

        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 2-Sep-18 4:25am    
This is the first time I have seen anyone (apart from me) using the message cracker macros. I actually gave up using them recently, for reasons I cannot recall.
Daniel Pfeffer 2-Sep-18 7:06am    
When you want to write code that runs on more than one type of Windows (i.e. Win16, Win32, and Win64), they save a lot of effort parsing the messages. I am converting FRACTINT for Windows (a Win16 medium model program) to run as a native Win32/Win64 program, and these macros help a lot.
dexter4life 2-Sep-18 17:00pm    
I will try that now
dexter4life 2-Sep-18 17:02pm    
I don't understand this "GetNextWindow(hwnd, GW_HWNDPREV)", is it still okay to leave it as NULL?
dexter4life 2-Sep-18 18:25pm    
Not working as expected, the windows continues to flicker when I try to resize it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900