更改 MFC 创建的窗口的样式Changing the Styles of a Window Created by MFC
11/04/2016
本文内容
在其功能版本中 WinMain ,MFC 注册多个标准窗口类。In its version of the WinMain function, MFC registers several standard window classes for you. 由于你通常不会编辑 MFC WinMain ,因此该函数使你无机会更改 mfc 默认窗口样式。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.
更改新 MFC 应用程序中的样式Changing Styles in a New MFC Application
如果使用 Visual C++ 2.0 或更高版本,则在创建应用程序时,可以在应用程序向导中更改默认的窗口样式。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. 在应用程序向导的 "用户界面功能" 页中,您可以更改主框架窗口和 MDI 子窗口的样式。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. 对于 MDI 子窗口,可以指定窗口是否支持拆分窗格。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.
若要更改使用应用程序向导创建的框架应用程序所使用的默认窗口属性,请重写窗口的 PreCreateWindow 虚拟成员函数。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 允许应用程序访问通常由 CDocTemplate 类在内部管理的创建过程。PreCreateWindow allows an application to access the creation process normally managed internally by the CDocTemplate class. 框架 PreCreateWindow 只需在创建窗口之前调用。The framework calls PreCreateWindow just prior to creating the window. 通过修改传递到的 CREATESTRUCT 结构 PreCreateWindow ,你的应用程序可以更改用于创建窗口的属性。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;
CTRLBARS示例应用程序演示了这种更改窗口特性的方法。The CTRLBARS sample application demonstrates this technique for changing window attributes. 根据应用程序在中所做的更改 PreCreateWindow ,可能需要调用函数的基类实现。Depending on what your application changes in PreCreateWindow, it may be necessary to call the base class implementation of the function.
以下讨论涉及到 SDI 事例和 MDI 情况。The following discussion covers the SDI case and the MDI case.
SDI CaseThe SDI Case
在单文档界面 (SDI) 应用程序中,框架中的默认窗口样式是 WS_OVERLAPPEDWINDOW 和 FWS_ADDTOTITLE 样式的组合。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 是一种特定于 MFC 的样式,指示框架将文档标题添加到窗口的标题。FWS_ADDTOTITLE is an MFC-specific style that instructs the framework to add the document title to the window's caption. 若要更改 SDI 应用程序中的窗口特性,请重写 PreCreateWindow 派生自 CFrameWnd 应用程序向导) 命名 (的类中的函数 CMainFrame 。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.
MDI CaseThe MDI Case
若要在多文档界面中更改子窗口的窗口样式,请 (MDI) 应用程序。A little more work is required to change the window style of a child window in a multiple document interface (MDI) application. 默认情况下,使用应用程序向导创建的 MDI 应用程序使用 MFC 中定义的默认 CMDIChildWnd 类。By default, an MDI application created with the Application Wizard uses the default CMDIChildWnd class defined in MFC. 若要更改 MDI 子窗口的窗口样式,必须从派生新类,并将 CMDIChildWnd 项目中对的所有引用替换为 CMDIChildWnd 对新类的引用。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. 最可能的情况是,对 CMDIChildWnd 应用程序中的唯一引用位于应用程序的 InitInstance 成员函数中。Most likely, the only reference to CMDIChildWnd in the application is located in your application's InitInstance member function.
MDI 应用程序中使用的默认窗口样式是 WS_CHILD、 WS_OVERLAPPEDWINDOW 和 FWS_ADDTOTITLE 样式的组合。The default window style used in an MDI application is a combination of the WS_CHILD, WS_OVERLAPPEDWINDOW, and FWS_ADDTOTITLE styles. 若要更改 MDI 应用程序的子窗口的窗口特性,请重写派生自的类中的 PreCreateWindow 函数 CMDIChildWnd 。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);
}
此代码将创建不带 "最大化" 按钮的 MDI 子窗口。This code creates MDI child windows without a Maximize button.
要了解有关的详细信息What do you want to know more about
请参阅See also