Window Class Registration(窗口类的注册)

This note describes the MFC routines that register the special WNDCLASSes needed by Microsoft Windows. Specific WNDCLASS attributes used by MFC and Windows are discussed.
此章节讲述了一些 MFC 中的常规内容,涉及注册 Microsoft Windows 需要的特殊的 WNDCLASS 。被 MFC Windows 使用的特殊的 WINCLASS 属性也会在此讨论。
The Problem
The attributes of a CWnd object, like an HWND in Windows, are stored in two places: the window object and the WNDCLASS. A WNDCLASS is different than a C++ class. The name of the WNDCLASS is passed to general window creation functions such as CWnd::Create and CFrameWnd::Create in the lpszClassName parameter.
问题:
一个 CWnd 对象的属性,如同一个 Windows 里的 HWND 句柄,被保存在两个地方:窗口对象和 WNDCLASS 中。一个 WNDCLASS 不同于一个 C++ 类。 WNDCLASS 的名称被传递到一般的窗口创建函数比如 CWnd::Create CframeWnd::Create 函数的 lpszClassName 参数中。
This WNDCLASS must be registered via one of four means:
  • Implicitly by MFC provided WNDCLASSes
  • Implicitly by subclassing a Windows control (or some other control)
  • Explicitly by calling the MFC AfxRegisterWndClass or AfxRegisterClass
  • Explicitly by calling the Windows routine RegisterClass
这种 WNDCLASS 必须经由以下四种方法之一来注册:
l          隐式地由 MFC 提供的一些 WNDCLASS 进行注册
l          隐式地子分类一个 Windows 控件(或者其他控件)以进行注册
l          显式地调用 MFC 中的 AfxRegisterWndClass AfxRegisterClass 进行注册
l          显式地调用 Windows RegisterClass
WNDCLASSes and MFC
The WNDCLASS structure consists of various fields that describe a window class. Following are the fields and how they are used in an MFC application.
WNDCLASS MFC
WNDCLASS 结构体由各种描述一个窗口类的不同域组成。以下是这些域的说明以及它们是怎样在一个 MFC 的应用程序中被使用的。

Style
Style of window: see below
LpfnWndProc
window proc, must be AfxWndProc
窗口处理过程,必须为AfxWndProc
CbClsExtra
not used (should be zero)
未被使用(必须为0)
CbWndExtra
not used (should be zero)
未被使用(必须为0)
HInstance
automatically filled with AfxGetInstanceHandle
自动地被填入AfxGetInstanceHandle
HIcon
icon for frame windows, see below
为框架窗口而准备的图标,见下
HCursor
cursor for when mouse is over window, see below
当鼠标越过窗口时的指针形状(光标),见下
HbrBackground
background color, see below
背景色,见下
LpszMenuName
not used (should be NULL)
未使用(必须为空)
LpszClassName
class name, see below
窗口类的名称,见下

 

Provided WNDCLASSes

In previous versions of MFC (prior to MFC 4.0), there were a number of predefined Window classes provided. These Window classes are no longer provided by default because of technical problems related to versioning (multiple versions of MFC loaded in one address space) as well as concerns relating to the fact that both MFC applications and OLE Controls may use the MFC DLLs.

为我们提供的WNDCLASSes

MFC早期的版本中(MFC4.0之前),它提供了一些预先确定的窗口类。这些窗口类现在不再被提供,因为涉及到版本方面的一些技术上的问题(在一个地址空间载入多种MFC版本),同时也因为这样一个事实:MFC应用程序和OLE(Object Linking and Embedded对象链接与嵌入)控件都可能会使用MFC DLLdynamic link libraries动态链接库)。

The following reference is provided to help migrate code that uses these previously provided WNDCLASSes. Applications should use AfxRegisterWndClass (with the appropriate parameters) in place of these classes.

以下涉及的内容是用来帮你移植使用这些之前提供的WNDCLASSes的代码。应用程序应该使用AfxRegisterWndClass(包含适当的参数)来代替这些类。

The following shows the classes and their attributes:

下面列出了这些类和它们的属性:

  • "AfxWnd" is used for all child windows created with CWnd::Create.
    • class style : CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW
    • no icon
    • arrow cursor
    • no background color

l         AfxWnd使用于所有由CWnd::Create创建的子窗口。

l         类风格:CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW

l         无图标

l         箭头光标

l         无背景色

  • "AfxFrameOrView" is used for frame windows and views (including stand-alone CFrameWnds and CMDIChildWnds).
    • class style : CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    • icon AFX_IDI_STD_FRAME
    • arrow cursor
    • COLOR_WINDOW background color

l         AfxFrameOrView使用于框架窗口和视类窗口(包含单机(stand-alone?)的CframeWndsCMDIChildWnds

l         类风格:CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW

l         图标:AFX_IDI_STD_FRAME

l         箭头光标

l         背景色:COLOR_WINDOW

  • "AfxMDIFrame" is used for the MDI frame window (that is, the parent) created with CMDIFrameWnd::Create.
    • class style : CS_DBLCLKS [ reduces flash during resizing ]
    • icon AFX_IDI_STD_MDIFRAME
    • arrow cursor
    • no background color

l         AfxMDIFrame使用于由CMDIFrameWnd::Create 创建的MDIMulti-Document Interface多文档界面)框架窗口(也就是父窗口)

l         类风格:CS_DBLCLKS(调整窗口大小时减少了闪烁)

l         图标:AFX_IDI_STD_MDIFRAME

l         箭头光标

l         无背景色

  • "AfxControlBar" is used for the standard control bar implementation.
    • class style : 0 [ reduces flash during resizing, no double clicks ]
    • no icon
    • AFX_IDI_STD_FRAME gray background color (COLOR_BTNFACE)

l         AfxControlBar被用于实现标准的控件条。

l         类风格:0(调整大小时减少闪烁,无鼠标双击行为)

l         无图标

l         箭头光标

l         灰色的背景色COLOR_BTNFACE

 

 

 

If the application provides a resource with the specified resource ID (for example, AFX_IDI_STD_FRAME) ID, MFC will use that resource. Otherwise the default resource is used. For the icon, the standard application icon is used, and for the cursor, the standard arrow cursor is used.

如果应用程序提供了一个以明确的资源ID标识的资源,那么MFC就会使用这个资源。否则使用缺省资源。对图标来说,标准的应用程序图标AFX_IDI_STD_FRAME被使用,对于光标来说,标准的箭头光标AFX_IDI_STD_FRAME被使用。

There are two icons that support MDI applications with single document types (one icon for the main application, the other icon for iconic document/MDIChild windows). For multiple document types with different icons, you must register additional WNDCLASSes or use the CFrameWnd::LoadFrame function.

有两种图标支持单一类型文档的MDI应用程序(一个图标使用在主应用程序中,另一个图标使用于文档/MDI子窗口)。对于有着不同的图标的多种文档类型,你必须注册额外的WNDCLASS或者使用CFrameWnd::LoadFrame

CFrameWnd::LoadFrame will automatically register a WNDCLASS using the standard "AfxFrameOrView" attributes but using the icon ID you specify as the first parameter to LoadFrame.

CFrameWnd::LoadFrame会自动为你注册一个WNDCLASS,此WNDCLASS使用标准的"AfxFrameOrView"属性,但会使用由你指定的图标ID,并把它作为作为第一个参数。

The values for background color and cursor for the MDIFrameWnd are not used since the client area of the MDIFrameWnd is completely covered by the "MDICLIENT" window. Microsoft does not encourage subclassing the "MDICLIENT" window so use the standard colors and cursor types when possible.

MDIFrameWnd的背景色和光标则没有被使用,因为MDIFrameWnd的客户区完全被"MDICLIENT"窗口覆盖。Microsoft不鼓励子分类"MDICLIENT"窗口,所以如果有可能的话,使用标准的颜色和光标类型。

Subclassing Controls

If you subclass or superclass a Windows control (for example, CButton) then your class automatically gets the WNDCLASS attributes provided in the Windows implementation of that control.

子分类控件

如果你子分类或者超分类一个Windows控件(比如,CButton),那么你的类便自动地得到了在那个控件的Windows实现里提供的WNDCLASS属性。

 

The AfxRegisterWndClass Function

MFC provides a helper routine for registering a window class. Given a set of attributes (window class style, cursor, background brush, and icon), a synthetic name is generated, and the resulting window class is registered. For example,

MFC提供了注册一个窗口类的帮助例程。给定一个属性集(窗口类风格,光标,背景画刷和图标),一个合成的名字就产生了,同时这个窗口类被注册了。例如:

const char* AfxRegisterWndClass(UINT nClassStyle, HCURSOR hCursor,    HBRUSH hbrBackground, HICON hIcon);

This function returns a temporary string of the generated registered window class name. See the Class Library Reference for more details.

此函数返回了一个临时的字符串,它是被注册窗口类的名字。查看Class Library Reference以了解更多细节。

The string returned is a temporary pointer to a static string buffer which is valid until the next call to AfxRegisterWndClass. If you want to keep this string around, store it in a CString variable. For example,

返回的字符串是一个临时的指针,它指向一个静态的字符串缓冲区。在下一次AfxRegisterWndClass被调用之前,它都是有效的。如果你想保存这个字符串,把它放在一个CString变量中。例如:

CString strWndClass = AfxRegisterWndClass(CS_DBLCLK, ...);

...

CWnd* pWnd = new CWnd;

pWnd->Create(strWndClass, ...);

...

AfxRegisterWndClass will throw a CResourceException if the window class failed to register (either because of bad parameters, or out of Windows memory).

如果窗口类注册失败,AfxRegisterWndClass会抛出CresourceException异常原因可能是坏参数或者是Windows内存不够

The RegisterClass and AfxRegisterClass Functions

If you want to do anything more sophisticated than what AfxRegisterWndClass provides, you may call the Windows API RegisterClass or the MFC function AfxRegisterClass. The CWnd, CFrameWnd and CMDIChildWnd Create functions take a lpszClassName string name for the window class as the first parameter. Any window class name can be used, regardless of how it was registered.

函数RegisterClass AfxRegisterClass

如果你想实现比AfxRegisterWndClass提供的更复杂的功能,你可以调用Windows API(Application Programming Interface应用编程接口) RegisterClass或者MFC函数AfxRegisterClassCWnd, CFrameWnd CMDIChildWnd Create函数都把窗口类的名称lpszClassName作为它们的第一个参数。任何窗口类的名称都能够使用,而不管它是怎样被注册的。

It is important to use AfxRegisterClass (or AfxRegisterWndClass) in a DLL on Win32. Win32 does not automatically unregister classes registered by a DLL, so this must be done explicitly when the DLL is terminated. By using AfxRegisterClass instead of RegisterClass this is done automatically for you. AfxRegisterClass maintains a list of unique classes registered by your DLL and will automatically unregister then when the DLL terminates. When using RegisterClass in a DLL, you must insure that all classes are unregistered when the DLL is terminated (in your DllMain function). Failure to do so may cause RegisterClass to fail unexpectedly when your DLL is used by another client application.

Win32平台下的一个DLL里使用AfxRegisterClass ( AfxRegisterWndClass)是很重要的。Win32不会自动地撤消注册那些被DLL注册的窗口类,所以当DLL终止时,必须明确地进行这种撤消操作。使用AfxRegisterClass 来代替RegisterClass会自动为你做这些操作。AfxRegisterClass维护一个由你的DLL注册的各种窗口类的列表,并且在DLL终止时自动撤消注册。而当你使用在DLLRegisterClass时,你必须确保当DLL终止时所有的窗口类都被撤消注册(在你的DllMain函数中)。如果撤消操作失败,当你的DLL被用于另一个客户应用程序时,可能会导致RegisterClass意外地失败。

 

你可能感兴趣的:(Window Class Registration(窗口类的注册))