Share via


Changing the Styles of a Window Created by MFC

OverviewHow Do ITutorial

In its version of the WinMain function, MFC registers several standard window classes for you. Because you don’t normally edit MFC’s WinMain, that function gives you no opportunity to change the MFC default window styles. This article explains how you can change the styles of such a preregistered window class in an existing application.

Changing Styles in a New MFC Application

If you’re using Visual C++ 2.0 or later, you can change the default window styles in AppWizard when you create your application. In AppWizard’s Advanced Options dialog box, choose the Main Frame tab (to change styles for your main frame window) or the MDI Child Frame tab (to change styles for MDI child windows). For either window type, you can specify its frame thickness (thick or thin) and any of the following:

  • Whether the window has Minimize or Maximize controls.

  • Whether the window appears initially minimized, maximized, or neither.

For main frame windows, you can also specify whether the window has a System Menu. For MDI child windows, you can specify whether the window supports splitter panes.

Changing Styles in an Existing Application

If you’re using a version of Visual C++ prior to version 2.0 or changing window attributes in an existing application, follow the instructions in the rest of this article instead.

To change the default window attributes used by a framework application created with AppWizard, override the window’s virtual member function. PreCreateWindow allows an application to access the creation process normally managed internally by the class. The framework calls PreCreateWindow just prior to creating the window. By modifying the parameter to PreCreateWindow, your application can change the attributes used to create the window.

The sample application demonstrates this technique for changing window attributes. Depending on what your application changes in PreCreateWindow, it may be necessary to call the base class implementation of the function.

The following discussion covers the SDI case and the MDI case.

The SDI Case

In a single document interface (SDI) application, the default window style in the framework is a combination of the WS_OVERLAPPEDWINDOW and FWS_ADDTOTITLE styles. FWS_ADDTOTITLE is an MFC-specific style that instructs the framework to add the document title to the window’s caption. To change the window attributes in an SDI application, override the PreCreateWindow function in your class derived from CFrameWnd (which AppWizard names CMainFrame). For example:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    // Create a window without min/max buttons or sizable border
    cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;

    // Size the window to 1/3 screen size and center it
    cs.cy = ::GetSystemMetrics(SM_CYSCREEN) / 3;
    cs.cx = ::GetSystemMetrics(SM_CXSCREEN) / 3;
    cs.y = ((cs.cy * 3) - cs.cy) / 2;
    cs.x = ((cs.cx * 3) - cs.cx) / 2;

    // Call the base-class version
    return CFrameWnd::PreCreateWindow(cs);
}

This code creates a main frame window without Minimize and Maximize buttons and without a sizable border. The window is initially centered on the screen.

The MDI Case

A little more work is required to change the window style of a child window in a multiple document interface (MDI) application. By default, an MDI application created with AppWizard uses the default class defined in MFC. To change the window style of an MDI child window, you must derive a new class from CMDIChildWnd and replace all references to CMDIChildWnd in your project with references to the new class. Most likely, the only reference to CMDIChildWnd in the application is located in your application's InitInstance member function.

The default window style used in an MDI application is a combination of the WS_CHILD, WS_OVERLAPPEDWINDOW, and FWS_ADDTOTITLE styles. To change the window attributes of an MDI application’s child windows, override the function in your class derived from CMDIChildWnd. For example:

BOOL CMyChildWnd::PreCreateWindow(CREATESTRUCT& cs)
{
    // Create a child window without the maximize button
    cs.style &= ~WS_MAXIMIZEBOX;

    // Call the base-class version
    return CMDIChildWnd::PreCreateWindow(cs);
}

This code creates MDI child windows without a Maximize button.

What do you want to know more about?