Adjusting window sizes

How can one adjust the window sizes with code such as one does with the cursor/mouse, be it of the main (mainframe) window or of internal windows such as of the MDI type? The idea is to be able to:

  - Accompany the adjustments of the main window with
    child windows, when the user adjusts the main one

  - Be able to stop any window resizing altogether

Thanks in advance for your input!

Who is Participating?

Improve company productivity with a Business Account.Sign Up

psdavisConnect With a Mentor Commented:
Override your OnSize function.  Everytime your main window changes, you will get the WM_SIZE message.  If you want to stop the sizing, you can prohibit this function from calling it's base class.


afx_msg void OnSize( UINT nType, int cx, int cy );


nType   Specifies the type of resizing requested. This parameter can be one of the following values:

SIZE_MAXIMIZED   Window has been maximized.

SIZE_MINIMIZED   Window has been minimized.

SIZE_RESTORED   Window has been resized, but neither SIZE_MINIMIZED nor SIZE_MAXIMIZED applies.

SIZE_MAXHIDE   Message is sent to all pop-up windows when some other window is maximized.

SIZE_MAXSHOW   Message is sent to all pop-up windows when some other window has been restored to its former size.

cx   Specifies the new width of the client area.

cy   Specifies the new height of the client area.


The framework calls this member function after the window's size has changed.

If the SetScrollPos or MoveWindow member function is called for a child window from OnSize, the bRedraw parameter of SetScrollPos or MoveWindow should be nonzero to cause the CWnd to be repainted.

Here's some more information for you.  The WM_GETMINMAXINFO message will allow you to set the sizes of the windows automatically for you...

Adding the WS_THICKFRAME windows style allows a window to be re-sized. By removing this from the Mainframe window's styles, you can prevent this window from being resized. Further, the MINMAXINFO structure stores the maximum and minimum tracking size of a window. The WM_GETMINMAXINFO message is sent to a window when the size or position of that window is about to change. An application can over-ride the handler for this message to set a window's default minimum or maximum tracking size.


You can prevent the Mainframe window of an AppWizard-generated SDI application from being resized by following these steps:

Modify the IDR_MAINFRAME menu to add a top-level menu item (F&reeze!) with an ID of ID_FREEZE.
Add a public Boolean data member (say, BOOL freezeState) to the CMainFrame class to keep track of the current state of the application. Initialize freezeState to FALSE in the constructor of the CMainFrame class.
Provide a command handler for the 'Freeze' menu item as follows:
void CMainFrame::OnFreeze() {

      char* lpszFreeze = "F&reeze!";
      char* lpszUnFreeze = "&UnFreeze!";
      CMenu* pmenu = GetMenu();
      if (!freezeState)

          freezeState = TRUE;
          pmenu->ModifyMenu(ID_FREEZE, MF_STRING, ID_FREEZE,
          DWORD style = ::GetWindowLong(this->m_hWnd, GWL_STYLE);
// Remove the thick frame style and the Minimize, Maximize buttons

        ::SetWindowLong(this->m_hWnd, GWL_STYLE, style);
} else {
       freezeState = FALSE;
       pmenu->ModifyMenu(ID_FREEZE, MF_STRING, ID_FREEZE,
       DWORD style = ::GetWindowLong(this->m_hWnd, GWL_STYLE);
// Add the thick frame style and the Minimize, Maximize buttons

      ::SetWindowLong(this->m_hWnd, GWL_STYLE, style);
Provide a handler for the WM_GETMINMAXINFO message as follows:
void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) {

      if (freezeState)
          RECT rc;

          lpMMI->ptMaxSize.x = rc.right - rc.left;
          lpMMI->ptMaxSize.y = rc.bottom -;
          lpMMI->ptMaxPosition.x = rc.left;
          lpMMI->ptMaxPosition.y =;
          lpMMI->ptMinTrackSize.x = rc.right - rc.left;
          lpMMI->ptMinTrackSize.y = rc.bottom -;
          lpMMI->ptMaxTrackSize.x = rc.right - rc.left;
          lpMMI->ptMaxTrackSize.y = rc.bottom -;

If the mainframe is maximized, disable the Freeze menu option because there is no point in making the window non-resizable because a maximized window cannot be resized anyway:
void CMainFrame::OnSize(UINT nType, int cx, int cy) {

      CFrameWnd::OnSize(nType, cx, cy);
      CMenu* pmenu = GetMenu();

      if (nType == SIZE_MAXIMIZED)
          pmenu->EnableMenuItem(ID_FREEZE, MF_DISABLED|MF_GRAYED);
          pmenu->EnableMenuItem(ID_FREEZE, MF_ENABLED);


NOTE: The steps to prevent the Mainframe window of an MDI application from being resized should be identical to the steps listed in this article. However, because there is more than one menu resource in an MDI application, you may want to design your application to add the 'Freeze' menu item to more than one menu resource.
RJVAuthor Commented:

Please correct me if I'm wrong, but aren't your suggestions dependent on events? Namely, if the user does the changing. How, however, does one do one's own changing, to simulate that of the user?


The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

AHhhh.....  It would help if I would read the question. :)

You could do several SetWindowPos commands in a row.  (e.g. Decreasing the x and increasing the cx simultaneously)  After every SetWindowPos call, the window should redraw.

BOOL SetWindowPos( const CWnd* pWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags );

Return Value

Nonzero if the function is successful; otherwise 0.


pWndInsertAfter   Identifies the CWnd object that will precede this CWnd object in the Z-order. This parameter can be a pointer to a CWnd or a Pointer to one of the following values:

wndBottom   Places the window at the bottom of the Z-order. If this CWnd is a topmost window, the window loses its topmost status; the system places the window at the bottom of all other windows.

wndTop   Places the window at the top of the Z-order.

wndTopMost   Places the window above all nontopmost windows. The window maintains its topmost position even when it is deactivated.

wndNoTopMost   Repositions the window to the top of all nontopmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a nontopmost window.

See the "Remarks" section for this function for rules about how this parameter is used.

x   Specifies the new position of the left side of the window.

y   Specifies the new position of the top of the window.

cx   Specifies the new width of the window.

cy   Specifies the new height of the window.

Also check out the OnSizing function, here you can stop the resizing of the window before it happens.

RJVAuthor Commented:
Thanks, Phillip!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.