Skip to content

Fixed MFC SDI Window Using CREATESTRUCT & PreCreateWindow

1. CREATESTRUCT Structure

In this Example, we will look at wiping out the minimize and resize button from the MFC Single Document Interface (SDI) Application using PreCreateWindow Override and CREATESTRUCT. Of course, we can do that with the MFC Wizard itself. However, that option is restricted to design time. Moreover, knowing how to carry out this in run-time is useful as the Application can behave based on the INI file or configuration file settings. Look at the picture below where only close button is available. So, the user cannot minimize it. Also, they cannot resize it by dragging the borders.

Fixed Size MFC SDI Application
Fixed Size MFC SDI Application

Before we go on, First, we will create an SDI Application with maximize and minimize buttons. You can watch the below video which shows making a skeleton of Visual C++ SDI Application for this Example.

The CMainFrame class of MFC Framework is liable for creating and managing Application’s Main Window which houses the Toolbar, Status Bar and Menu, etc. We can change its style by altering the CREATESTRUCT structure while the Application Window is loading. MFC Framework calls the PreCreateWindow function before presenting the Window to the user. One can override this function to change the CREATESTRUCT which is passed as a reference argument to it.

2. PreCreateWindow Override

When we created the SDI Application, the MFC App wizard created a PrecreateWindow override for us. The default implementation of the function is below:

The below video shows how to navigate to the PreCreateWindow Function:

In the above code snippet, we can see that MFC Framework supplies CREATESTRUCT as a parameter to the PreCreateWindow override. One can change the style of the SDI Application Window by adjusting CREATESTRUCT structure.

Note that the MFC Framework calls PreCreateWindow function before displaying the Main SDI Window to the user. Also it fills the CREATESTRUCT with default options (Or One changed in the Wizard, Refer Video 1) and supplies that to the PreCreateWindow function. Remember, while creating the Application through Wizard we opted for Minimize and Maximize buttons and MFC fills CREATESTRUCT with this information. We have the final chance to override those display styles of the Main Frame Window. Note that MFC passes the CREATESTRUCT as a reference and hence the change we make on it will reflect to the caller. The MFC Framework caller will make use of this changed structure and draws the Mainframe Window.

3. Controlling CMainFrame Window Style

We are going to set four styles to the CREATESTRUCT. They are:

  1. Set MainFrame window of the SDI application as Fixed Size Window so that user cannot resize it.
  2. Hide the Minimize button from the MainFrame window.
  3. Hide the Maximize button to avoid Maximizing the window.
  4. Do no show the default title of the MFC SDI MainFrame Window.
  5. Set the size of the Window to 500 x 250 Pixels.

The below code will do all the above said adjustments:

Note, that the CREATESTRUCT is already populated with the default settings by the Application Wizard. To remove a particular setting, we are using the “~ Bit-wise Operator”. For Example, ~WS_MINIMIZEBOX will remove the bit for Minimize box and the Bit-wise AND (&) with the CREATESTRUCT cs will remove that style from it. The same way we remove other settings as described below:

  • WS_MAXIMIZEBOX controls the Maximize button
  • WS_MINIMIZEBOX controls the Minimize button
  • WS_THICKFRAME controls the SDI is sizable or not
  • FWS_ADDTOTITLE controls the display of default title
  • And Finally, cx and cy are used to set the Width and Height of the MainFrame Window.

The below video shows how the modified code works with MainFrame Window:

4. Summary

Changing window style at run-time is useful when we want to adjust the window style on the Fly or to act upon the application settings. In this example, we used CREATESTRUCT to set the SDI Application window size. One can change the size using the Win32 API functions as well.

There are other useful window styles like WS_CAPTION, WS_SYSMENU, etc which we can control through the CREATESTRUCT structure. Refer MSDN to know more Window Style Constants.

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: