Navigation Bar

12 March 2012

[ MFC ] - Using the PreCreateWindow override, hide the maximize & minimize mainframe buttons


1. Introduction


In this article, we will create Single Document Interface (SDI) application for a start. Then we will have a look at the CREATESTRUCT structure to modify the style of the displayed window.


2. About the Sample


Look at the Screenshot below:



The screenshot shows a portion of the MFC generated SDI Application. In this sample, we modified the default SDI by changing the CREATESTRUCT that is passed to the PreCreateWindow function. The following modifications are applied to the sample:

1) Minimize and Maximize boxes at the upper right corner of the window (Not shown in screen shot) are removed.
2) The entire SDI application is displayed as fixed size dialog. That means the dialog can not be resized.


3. Create MFC SDI Application


Creating the MFC SDI Sample is shown in the below video. The MFC Framework for SDI application with document and view support will create four classes for you. These classes are explored in detail in a separate article on Document view architecture.


When you run the Application, you will see default SDI as shown in the below screenshot:


The Untitled says that the document is not yet given a valid title. The dialog is resizable and at the upper right-hand side, you can see Minimize, Maximize, and close button.

4. The PreCreateWindow Override

The CMainFrame class already overrode the PreCreateWindow for you. All you need to do is navigate to the function and start writing the code. The function signature is shown below:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

This function is called by the MFC framework before creating the window. So it is a perfect place to intercept the window creation and make the modification to the default window style. Look the parameter. The function already supplied with the created structure and it expects that the client (We are a client to the MFS Framework as we are making use of their classes. In this case the CMainFrame) will inform the changes through the CREATESTRUCT parameter cs. Also, note that the parameter is received by reference so the changes we make on this structure are reflected back to the caller. The caller will then draw the window with the changes we apply. Have a look at the video to Navigate to the PreCreateWindow function (If you are first time user of MFC):

Video 1: Watch Here

The PreCreateWindow function modified as shown below:

// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs
// Sample 01: Hide the document title
cs.style = cs.style & (~ FWS_ADDTOTITLE );
//Sample 02: To remove Maximize and Minimize box of the window
cs.style = cs.style & ( ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX ));
//Sample 03: Avoid resizing the document. Make it fixed size
cs.style = cs.style & ~WS_THICKFRAME;

In the above set of statements, we revoked some of the already added styles to the style member of CREATESTRUCT. Say, for Example, the style member already had the WS_THICKFRAME applied to it. To remove that style we used the bitwise negation on the WS_THICKFRAME constant. Then we applied that negated (Bit inverse) content style by performing the bitwise AND (&).

First, we removed the document title (Marked in yellow in the above picture) from the window caption area. Then we revoked the Minimize and Maximize box from the window. Finally, the window is made fixed size.

The below video shows final Sample after making the above-said changes in the PreCreateWindow function.
Video 2: Watch Here
Source Code : Download