Adjusting window sizes

Posted on 1998-08-10
Last Modified: 2013-11-20
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!

Question by:RJV
  • 4
  • 2

Accepted Solution

psdavis earned 150 total points
ID: 1320499
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.


Expert Comment

ID: 1320500
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.

Author Comment

ID: 1320501

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?


PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Expert Comment

ID: 1320502
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.


Expert Comment

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


Author Comment

ID: 1320504
Thanks, Phillip!


Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 122
VBA "SendKeys" Syntax for Multiple Keystrokes 7 108
matchUp  challenge 6 64
wordmultiple challenge 12 133
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question