|
|
1.1 root 1: Microsoft Foundation Classes Microsoft Corporation
2: Technical Notes
3:
4: #1 : WNDCLASSes and MFC
5:
6: This note describes the MFC routines for registering special
7: WNDCLASSes as needed by MS-Windows. Specific WNDCLASS attributes
8: used by MFC and Windows are discussed.
9:
10: =============================================================================
11:
12: The Problem
13: ===========
14:
15: The attributes of a CWnd object, like an HWND in Windows, are stored
16: in two places, the window object and the WNDCLASS. A WNDCLASS is different
17: than a C++ class. The MFC C++ class in Windows still requires that
18: the name of a WNDCLASS be passed to the creation function. This WNDCLASS
19: must be registered via one of four means:
20:
21: * implicitly by MFC
22: * implicitly by an MS-Windows control (or some other control)
23: * explicitly by calling the MFC routine AfxRegisterClass
24: * explicitly by calling the Windows routine RegisterClass
25:
26: -----------------------------------------------------------------------------
27: WNDCLASSes and MFC
28: ==================
29:
30: The WNDCLASS structure consists of various fields used to describe a
31: window class. Here are the fields and how they apply to an MFC app.
32:
33: style -- style of window, see below
34: lpfnWndProc -- window proc, must be 'AfxWndProc'
35: cbClsExtra -- not used
36: cbWndExtra -- not used
37: hInstance -- automatically filled with AfxGetInstanceHandle()
38: hIcon -- icon for frame windows, see below
39: hCursor -- cursor for when mouse is over window, see below
40: hbrBackground -- background color, see below
41: lpszMenuName -- not used
42: lpszClassName -- class name, see below
43:
44: -----------------------------------------------------------------------------
45: Provided WNDCLASSes
46: ===================
47:
48: MFC provides 3 standard window classes.
49:
50: "AfxWnd" is used for all child windows created with CWnd::Create().
51:
52: "AfxFrameWnd" is used for frame windows (both stand-alone CFrameWnds
53: and CMDIChildWnds).
54:
55: "AfxMDIFrameWnd" is used for the MDI frame window (i.e. the parent)
56: created with CMDIFrameWnd::Create().
57:
58:
59: The following table shows the registered attributes:
60:
61: Class name --> AfxWnd AfxFrameWnd AfxMDIFrameWnd
62:
63: Attribute
64: ---------
65: style 0 0 0
66: icon none AFX_IDI_STD_FRAME AFX_IDI_STD_MDIFRAME
67: cursor arrow arrow arrow
68: background color none COLOR_WINDOW n/a
69:
70: NOTE: if the application provides a resource with the specified 'AFX_ID*'
71: resource ID, MFC will use that resource. Otherwise the default resource
72: is used. For the icon, the standard app icon is used (a white box),
73: and for the cursor, the standard arrow cursor is used.
74:
75: NOTE: we provide two alternative icons for supporting MDI applications
76: with single document types (one icon for the main app, the other icon
77: for iconic document/MDIChild windows). If you desire an MDI application
78: with multiple document types with different icons, you must register
79: additional WNDCLASSes.
80:
81: NOTE: the values for background color and cursor for the MDIFrameWnd
82: are not used since the client area of the MDIFrameWnd is completely
83: covered with the "MDICLIENT" window. We do not support subclassing
84: of the "MDICLIENT" window and you should be sticking to the standard
85: colors and cursor types.
86:
87: -----------------------------------------------------------------------------
88: Subclassing controls:
89: =====================
90:
91: If you subclass or superclass a Windows control (eg: CButton) then your class
92: automatically gets the WNDCLASS attributes as provided in the Windows
93: implementation of that control.
94:
95: -----------------------------------------------------------------------------
96: The AfxRegisterWndClass() Function
97: ==================================
98:
99: MFC provides a helper routine for registering a window class.
100: Given a set of attributes for: window class style, cursor,
101: background brush and icon, a synthetic name is generated, and
102: the resulting window class registered.
103:
104: const char* AfxRegisterWndClass(UINT nClassStyle,
105: HCURSOR hCursor, HBRUSH hbrBackground, HICON hIcon);
106:
107: This function takes the arguments (all except the first one default
108: to zero) and returns a temporary string of the registered window
109: class.
110:
111: AfxRegisterWndClass will throw a CResourceException if the window
112: class failed to register (either because of bad parameters, or out of
113: Windows memory).
114:
115: NOTE: the string returned is a temporary pointer to a static string buffer
116: which is valid until the next call to AfxRegisterWndClass. If you want
117: to keep this string around, store it in a CString variable.
118:
119: CString wndClass = AfxRegisterWndClass(CS_DBLCLK, defaultCursor);
120:
121: ...
122: CWnd wnd;
123: wnd.Create(wndClass, ...);
124: ...
125:
126: -----------------------------------------------------------------------------
127: Last resort: use RegisterClass
128: ==============================
129:
130: If you want to do anything more sophisticated than what
131: AfxRegisterWndClass provides, you can always call the Windows API
132: 'RegisterClass'.
133:
134: Most of the MFC window creation routines take a string name for a
135: window class as the first parameter, and any window class name
136: can be used, regardless of how it was registered.
137:
138: -----------------------------------------------------------------------------
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.