共用方式為


Changing the Styles of a Window Created by MFC

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 the Application Wizard when you create your application. In the Application Wizard's User Interface Features page, you can change styles for your main frame window and 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 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 the Application Wizard, override the window's PreCreateWindow virtual member function. PreCreateWindow allows an application to access the creation process normally managed internally by the CDocTemplate class. The framework calls PreCreateWindow just prior to creating the window. By modifying the CREATESTRUCT structure passed to PreCreateWindow, your application can change the attributes used to create the window. For example, to ensure that a window does not use a caption, use the following bitwise operation:

// cs has been declared as CREATESTRUCT& cs;
cs.style &= ~WS_CAPTION;

The CTRLBARS 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 the Application Wizard names CMainFrame). For example:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    // Call the base-class version 
   if( !CFrameWnd::PreCreateWindow(cs) )
      return FALSE;

    // 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;

   return TRUE;
}

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 the Application Wizard uses the default CMDIChildWnd 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 PreCreateWindow function in your class derived from CMDIChildWnd. For example:

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

   return CMDIChildWnd::PreCreateWindow(cs);
}

This code creates MDI child windows without a Maximize button.

What do you want to know more about?

See Also

Concepts

Frame-Window Styles (C++)