Microsoft KB Archive/99847

= How To Change Window Attributes in an MFC Application =

Article ID: 99847

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q99847

<div class="notice_section">

<div class="notice_section">

NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

<div class="summary_section">

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

<div class="moreinformation_section">

MORE INFORMATION
The CTRLBARS sample application, provided with the Microsoft Foundation Class Library, demonstrates this technique to change window attributes. Note that depending on what your application changes in PreCreateWindow, it may be necessary to call the base class implementation.

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 a framework-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 form CFrameWnd. 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;

return CFrameWnd::PreCreateWindow(cs); } 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 generated in AppWizard uses the default CMDIChildWnd class defined in the Microsoft Foundation Class Library. To change the window style of an MDI child window, your application 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 CMyChildWnd::PreCreateWindow(CREATESTRUCT& cs) {     // Create a child window without the maximize button cs.style &= ~WS_MAXIMIZEBOX; return CMDIChildWnd::PreCreateWindow(cs); } For more information about the PreCreateWindow function, please refer to the Microsoft Visual C++ &quot;Class Library Reference, Volume 1,&quot; or to the Visual C++ online help files and Visual C++ 4.0 Books Online.

Keywords: kbarchitecture kbhowto kbmdi kbuidesign kbwndwclass KB99847

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.