The frame window Object
MFC's CWnd class and its derivatives provide object-oriented interfaces to the window or windows an application creates. Hello's window class, CMainWindow, is derived from MFC's CFrameWnd class, which is derived from CWnd. CFrameWnd models the behavior of frame windows. For now, you can think of a frame window as a top-level window that serves as an application's primary interface to the outside world. In the greater context of the document / view architecture, frame windows play a larger role as intelligent containers for views, toolbars, Status Bars, AND Other User-Interface (UI) Objects.
An MFC application creates a window by creating a window object and calling its Create or CreateEx function Hello creates a CMainWindow object in CMyApp :: InitInstance CMainWindow's constructor creates the window you see on the screen..:
Create (NULL, _T ("The Hello Application");
_T is a macro that's used to make string literals character set neutral. It's discussed later in this chapter. Create is a CMainWindow member function that's inherited from CFrameWnd. It's one of approximately 20 member functions that CFrameWnd defines in addition to the functions it inherits from CWnd. CframeWnd :: Create Is Prototyped As Follows:
Bool Create (LPCTSTSTSTSTSTSZAME,
LPCTSTR LPSZWINDOWNAME,
DWORD DWSTYLE = WS_OVERLAPPEDWINDOW,
Const Rect & Rect = RectDefault,
CWnd * pparentWnd = NULL,
LPCTSTR LPSZMENUNAME = NULL,
DWORD DWEXSTYLE = 0,
CcreateContext * pContext = null)
Default values are defined for six of the eight parameters Create accepts. Hello does the minimum amount of work required, specifying values for the function's first two parameters and accepting the defaults for the remaining six. The first parameter-lpszClassName-specifies the name of the WNDCLASS that the window is based on. Specifying NULL for this parameter creates a default frame window based on a WNDCLASS registered by the framework. The lpszWindowName parameter specifies the text that will appear in the window's title bar.The dwStyle parameter specifies the window style. The default is WS_OVERLAPPEDWINDOW. You can change the window style by specifying an alternative style or combination of styles in the call to Create. You'll find a complete list of window styles in the documentation for CFrameWnd :: Create. Two of the styles frequently Used with frame windows area ws_hscroll and ws_vscroll, Which Add Horizontal And Vertical Scroll Bars to the bottom and right Edges of the window's Client Area. The Statement
Create (NULL, _T ("Hello"), WS_OVERLAPPEDWINDOW | WS_VSCROLL);
. Creates an overlapped window that contains a vertical scroll bar As this example illustrates, multiple styles may be combined using the C |. Operator WS_OVERLAPPEDWINDOW combines the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_MINIMIZEBOX, WS_MAXIMIZEBOX, and WS_THICKFRAME styles, so if you'd like TO CREATE A WINDOW THAT LOOKS JUST LIKE A WS_OVERLAPPEDWINDOW Window But Lacks The Maximize Button In The Title Bar, You Could Call Create this Way:
Create (NULL, _T ("Hello"), WS_OVERLAPPED | WS_CAPTION |
WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME
An alternative way to specify a window style is to override the virtual PreCreateWindow function that a window inherits from CWnd and modify the style field of the CREATESTRUCT structure passed to PreCreateWindow. This capability is handy to have when the framework creates your application's main window for you , as is frequently the case in document / view applications, but it's not necessary when your code calls Create directly and therefore controls the parameters passed to it. Later in this book, you'll see examples demonstrating when and how PreCreateWindow is used.Additional window styles known as extended styles can be specified in CFrameWnd :: Create's dwExStyle parameter window styles are divided into standard and extended styles for a historical reason:.. Windows 3.1 added support for additional window styles by introducing the :: CreateWindowEx API function :: CreateWindowex Is Similar To :: CreateWindow, But ITS Argument List Includes An Additional Parameter Specifying The Window's extended style. Windows 3.1 supported just five extended styles. More recent versions of Windows offer a much greater selection that includes the WS_EX_WINDOWEDGE and WS_EX_CLIENTEDGE styles, which give window borders a more pronounced 3D look. MFC automatically adds these two styles to frame windows for you SO You Rarely Need to Specify Them Yourself.
After the dwStyle parameter comes rect, which is a C reference to a CRect object or a C-style RECT structure specifying the window's initial screen position and size. The default is rectDefault, which is a static member of the CFrameWnd class that simply tells Windows to choose the window's default initial position and size. If you want to, you can specify the initial position and size by initializing a CRect object with coordinates describing a rectangle on the screen and passing it to Create. The following statement creates a standard overlapped window whose upper left corner is located 32 pixels to the right of and 64 pixels down from the upper left corner of the screen and whose initial width and height are 320 and 240 pixels, respectively: Create (NULL, _T ( "Hello"), WS_OVERLAPPEDWINDOW ,
CRECT (32, 64, 352, 304));
Note that the window's width and height are determined by the difference between the first and third parameters and the second and fourth parameters rather than by the absolute values of the third and fourth parameters. In other words, the CRect object specifies the rectangular region of the Screen That The Window Will Occupy. The Four Parameters Passed To CRect's Constructor Specify, in Order, The Rectangle's Left, Top, Right, And Bottom Screen Coordinates.
The pParentWnd parameter to Create identifies the window's parent or owner. Do not worry for now about parents and owners. This parameter is always NULL for top-level windows because top-level windows have neither parents nor owners. (Actually, specifying NULL for pParentWnd makes the desktop window-the window that forms the backdrop for the screen-the window's owner. But that's an implementation detail that matters only to Windows.) Create's lpszMenuName parameter identifies the menu associated with the window. NULL indicates that the window has no WE'LL Begin Using Menus in Chapter 4.
The final parameter to CFrameWnd :: Create, pContext, contains a pointer to a CCreateContext structure that is used by the framework when it initializes frame windows in document / view applications. Outside the document / view architecture, this parameter is meaningless and should be set To NULL.
Create offers a tremendous variety of options to the programmer. The number of choices might seem overwhelming at this early stage, especially if you have not programmed for Windows before, but experience will teach you how and when to exercise the options available to you. Meanwhile, the class library's use of default function arguments hides much of the complexity when a standard CFrameWnd-type window is all you need. This is one example of the ways in which MFC makes Windows programming just a little bit easier.
Painting the window
Hello Doesn't Draw to The Screen Just WHENEVER IT WANTS To WM_PAINT Messages from Windows Signaling That's Time To Update The Window.
WM_PAINT messages can be generated for a variety of reasons. A WM_PAINT message might be sent because another window was moved, exposing a part of Hello's window that was previously obscured, or it could be sent because the window was resized. Whatever the stimulus, it is the application's responsibility to paint the client area of its window in response to WM_PAINT messages. Windows draws the nonclient area so that all applications will have a consistent look, but if the application does not implement its own drawing routines for the client area, the interior of the window will be blank.In Hello, WM_PAINT messages are processed by CMainWindow :: OnPaint, which is called anytime a WM_PAINT message arrives. OnPaint's job is to draw "Hello, MFC" in the center of the window's client area. IT Begins by Constructing a cpaintdc Object named DC:
CPAINTDC DC (this);
MFC's CPaintDC class is derived from MFC's more generic CDC class, which encapsulates a Windows device context and includes dozens of member functions for drawing to screens, printers, and other devices. In Windows, all graphical output is performed through device context objects that abstract the physical destinations for output. CPaintDC is a special case of CDC that is used only in WM_PAINT message handlers. Before drawing in response to a WM_PAINT message, an application must call the Windows :: BeginPaint API function to obtain a device context and prepare the device context for painting. When it's finished painting, the application must call :: EndPaint to release the device context and inform Windows that painting is complete. If an application fails to call :: BeginPaint and :: EndPaint when it processes a WM_PAINT message, the Message Will NOT BE Removed from The Message Queue. NOT SURPRISINGLY, CPAINTDC CALLS :: Beginpaint from Its Constructor and :: Endpaint from Its Destructor to ensure that this does not happen.In MFC, you'll always draw to the screen with a CDC object of some type, but you must use a CPaintDC object only inside OnPaint handlers. Furthermore, it's good practice to create CPaintDC objects on the stack so that their destructors will be called automatically when OnPaint ends. you can instantiate a CPaintDC object with the new operator if you want to, but then it becomes critical to delete that object before OnPaint ends. Otherwise, :: EndPaint will not be Called, and your application Won't redraw prot.
After creating a CPaintDC object, OnPaint constructs a CRect object representing a rectangle and calls CWnd :: GetClientRect to initialize the rectangle with the coordinates of the window's client area: CRect rect;
GetClientRect (& RECT);
Onpaint Then Calls CDC :: DrawText to Display "Hello, MFC" in The Window's Client Area:
Dc.drawtext (_T ("Hello, MFC"), -1, & review,
DT_SINGLINE | DT_CENTER | DT_VCENTER);
DrawText is a powerful general-purpose function for outputting text It accepts four parameters:. A pointer to the string to display, the number of characters in the string (or -1 if the string is terminated with a NULL character), the address of a RECT structure or CRect object specifying the formatting rectangle (the rectangle in which the string is displayed), and flags specifying output options. in Hello, CMainWindow :: OnPaint combines the DT_SINGLELINE, DT_CENTER, and DT_VCENTER flags to display a single line of text .
Conspicuously missing from DrawText's argument list are parameters specifying basic properties of the output such as the font and text color. These and other characteristics of the output are attributes of the device context and are controlled with CDC member functions such as SelectObject and SetTextColor. Because Hello did not change any of the device context's attributes, the default font and default text color (black) were used. DrawText also fills a small rectangle surrounding the text it outputs with the device context's current background color. The default is white, so you do not see it if your system's default window background color also happens to be white. But change the window background color to gray and the white text background will stick out like a sore thumb.In Chapter 2, you'll learn how to customize The Output from DrawText and Other CDC Drawing Functions by Modifying Device Context Attributes. Once You Know How To Do It, It's Simple To Change The Text Color Or Tell D RawText to Paint The Text Background with "Transparent" Pixels.
The Message Map
How is it that a WM_PAINT message from Windows turns into a call to CMainWindow :: OnPaint? The answer lies in the message map. A message map is a table that correlates messages and member functions. When Hello's frame window receives a message, MFC scans the window's message map, sees that a handler exists for WM_PAINT messages, and calls OnPaint. The message map is MFC's way of avoiding the lengthy vtables that would be required if every class had a virtual function for every possible message it might receive. Any class derived from CCmdTarget can contain a message map What MFC does internally to implement message maps is hidden behind some rather complex macros, but using a message map is exceedingly simple Here's all you have to do to add a message map to a class:.. Declare the message map by adding a DECLARE_MESSAGE_MAP statement to the class declaration. Implement the message map by placing macros identifying the messages that the class will handle between calls to BEGIN_MESSAGE_M AP and END_MESSAGE_MAP. Add Member Functions to Handle The Messages.
Hello's CMainWindow Class Handles Just One Message Type, WM_Paint, So ITS Message Map Is Implement As Follows:
Begin_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
END_MESSAGE_MAP ()
BEGIN_MESSAGE_MAP begins the message map and identifies both the class to which the message map belongs and the base class. (Message maps are passed by inheritance just as other class members are. The base class name is required so that the framework can find the base class's message map when necessary.) END_MESSAGE_MAP ends the message map. in between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP are the message map entries. ON_WM_PAINT is a macro defined in the MFC header file Afxmsg_.h. It adds an entry for WM_PAINT messages to the message map. The macro accepts no parameters because it is hardcoded to link WM_PAINT messages to the class member function named OnPaint. MFC provides macros for more than 100 Windows messages, ranging from WM_ACTIVATE to WM_WININICHANGE. You can get the name of the message handler that corresponds to a given ON_WM Macro from The MFC Documentation, But it's Fairly Easy To DEDUCE The name yourself by Replacing WM_ WITH AND Converting All the remaining letters except those at the beginning of the word to lowercase. Thus, WM_PAINT becomes OnPaint, WM_LBUTTONDOWN becomes OnLButtonDown, and so on.You'll need to consult the MFC documentation to determine what kinds of arguments a message handler receives and what type of value it Returns. Onpaint Takes No Arguments and Returns No Value, But Onlbuttondown Is Prototyped Like this:
AFX_MSG Void Onlbuttondown (uint nflags, cpoint point)
nFlags contains bit flags specifying the state of the mouse buttons and the Ctrl and Shift keys, and point identifies the location at which the click occurred. The arguments passed to a message handler come from the wParam and lParam parameters that accompanied the message. But whereas wParam and lParam are of necessity generic, the parameters passed to an MFC message handler are both specific and type-safe.What happens if you want to process a message for which MFC does not provide a message-map macro? you can create an entry for the message using the generic ON_MESSAGE macro, which accepts two parameters:. the message ID and the address of the corresponding class member function The following statement maps WM_SETTEXT messages to a member function named OnSetText:
ON_MESSAGE (WM_SETTEXT, ONSETTEXT)
OnsetText Would Be Declared Like this:
AFX_MSG LRESULT ONSETTEXT (WPARAM WPARAM, LPARAM LPARAM);
Other special-purpose message-map macros provided by MFC include ON_COMMAND, which maps menu selections and other UI events to class member functions, and ON_UPDATE_COMMAND_UI, which connects menu items and other UI objects to "update handlers" that keep them in sync with the INTERNAL state of the application. You'll be introducesd to these and other message-map macros in the Chapter That Follow.
Getting Back to Hello for a Moment, Cmainwindow's onpaint function and message map area declared with the following statements in Hello.h:
AFX_MSG void onpaint ();
Declare_message_map ()
afx_msg is a visual reminder that OnPaint is a message handler. You can omit it if you'd like because it reduces to white space when compiled. The term afx_msg is meant to connote a function that behaves as if it were a virtual function but does so without requiring a vtable entry. DECLARE_MESSAGE_MAP is usually the final statement in the class declaration because it uses C keywords to specify the visibility of its members. you can follow DECLARE_MESSAGE_MAP with statements declaring other class members, but if you do, you should also lead Off with a public, protected, or private keyword to ensure the visibility you want thinkthing.how message map work
You can find out how message maps work by examining the DECLARE_MESSAGE_MAP, BEGIN_MESSAGE_MAP, and END_MESSAGE_MAP macros in Afxwin.h and the code for CWnd :: WindowProc in Wincore.cpp. Here's a synopsis of what goes on under the hood when you use message- Mapping macros in your code, and how the framework buyes the code and data generated by the macros to convert message..............
MFC's DECLARE_MESSAGE_MAP macro adds three members to the class declaration: a private array of AFX_MSGMAP_ENTRY structures named _messageEntries that contains information correlating messages and message handlers; a static AFX_MSGMAP structure named messageMap that contains a pointer to the class's _messageEntries array and a pointer to the base class's messageMap structure; and a virtual function named GetMessageMap that returns messageMap's address (The macro implementation is slightly different for an MFC application that's dynamically rather than statically linked to MFC, but the principle is the same.) BEGIN_MESSAGE_MAP contains the implementation for the GetMessageMap function. and code to initialize the messageMap structure. The macros that appear between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP fill in the _messageEntries array, and END_MESSAGE_MAP marks the end of the array with a NULL entry. For the statements // in the class declaration
Declare_message_map ()
// in The Class Implementation
Begin_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
END_MESSAGE_MAP ()
The Compiler's Preprocessor Generates this:
// in the class declaration
Private:
Static const AFX_MSGMAP_ENTRY _MESSAGEENTRIES [];
protected:
Static const AFX_MSGMAP MessageMap;
Virtual const AFX_MSGMAP * getMessageMap () const;
// in The Class Implementation
Const AFX_MSGMap * cmainwindow :: getMessageMap () const
{Return & CmainWindow :: MessageMap;
Const AFX_MSGMAP CMAINWINDOW :: MessageMap = {
& CframeWnd :: MessageMap,
& Cmainwindow :: _ Messagentries [0]
}
Const AFX_MSGMAP_ENTRY CMAINWINDOW :: _ Messagentries [] = {
{WM_Paint, 0, 0, 0, AFXSIG_VV,
(AFX_PMSG) (AFX_PMSGW) (void (cwnd :: *) (void)) onpaint}, {0, 0, 0, 0, AFXSIG_END, (AFX_PMSG) 0}
}
With this infrastructure in place, the framework can call GetMessageMap to get a pointer to CMainWindow's messageMap structure. It can then scan the _messageEntries array to see if CMainWindow has a handler for the message, and if necessary it can grab a pointer to CFrameWnd's messageMap structure And Scan The Base Class's Message Map, TOO.
That's a pretty good description of what happens when a message for CMainWindow arrives. To dispatch the message, the framework calls the virtual WindowProc function that CMainWindow inherits from CWnd. WindowProc calls OnWndMsg, which in turn calls GetMessageMap to get a pointer to CMainWindow :: messageMap and searches CMainWindow :: _ messageEntries for an entry whose message ID matches the ID of the message that is currently awaiting processing. If the entry is found, the corresponding CMainWindow function (whose address is stored in the _messageEntries array along with the message ID) is called. Otherwise, OnWndMsg consults CMainWindow :: messageMap for a pointer to CFrameWnd :: messageMap and repeats the process for the base class. If the base class does not have a handler for the message, the framework ascends another level and consults the Base Class's Base Class, SystemAtical Working Its Way Up The Inheritance Chain Until It Finds a Message Handler OR Passes The Message To Windows for default processing. Figure 1-5 illustrates CMainWindow's message map schematically and shows the route that the framework travels as it searches for a handler to match a given message ID, beginning with the message map entries for CMainWindow.
What MFC's message-mapping mechanism amounts to is a very efficient way of connecting messages to message handlers without using virtual functions. Virtual functions are not space-efficient because they require vtables, and vtables consume memory even if the functions in them are not overridden. The amount of memory used by a message map, in contrast, is proportional to the number of entries it contains. Since it's extremely rare for a programmer to implement a window class that includes handlers for all of the different message types, message mapping conserves a Few Hundred Bytes of Memory Just About Every Time A CWND IS Wrapped AN HWND.FIGURE 1-5. Message-Map Processing.
Windows, Character Sets, And The_T Macro
Microsoft Windows 98 and Microsoft Windows NT use two different character sets to form characters and strings. Windows 98 and its predecessors use the 8-bit ANSI character set, which is similar to the ASCII character set familiar to many programmers. Windows NT and Windows 2000 use the 16-bit Unicode character set, which is a superset of the ANSI character set. Unicode is ideal for applications sold in international markets because it contains a rich assortment of characters from non-US alphabets. Programs compiled with ANSI characters will run on Windows NT and Windows 2000, but Unicode programs run slightly faster because Windows NT and Windows 2000 do not have to perform an ANSI-to-Unicode conversion on every character. Unicode applications will not run on Windows 98, period, unless you convert Every Character String Passed to Windows from Unicode To Ansi Format.
When an application is compiled, it is compiled to use either ANSI or Unicode characters. If your application will be deployed on both Windows 98 and Windows 2000, it may behoove you to make strings character set neutral. Then, by making a simple change to The Project's Build Settings Or Adding a #define to a Header File, You Can Tell The Compiler WHether To Produce An Ansi Build OR A String Litral Like this: "Hello"
The Compiler Forms The string from Ansi Characters. If you declare the string like this:
L "hello"
The Compiler Uses Unicode Characters. But if you use mfc's _t macro, Like this:
_T ("Hello")
the compiler will emit Unicode characters if the preprocessor symbol _UNICODE is defined, and ANSI characters if it is not. If all your string literals are declared with _T macros, you can produce a special Windows NT_only build of your application by defining _UNICODE. Defining this symbol implicitly defines a related symbol named UNICODE (no underscore), which selects the Unicode versions of the numerous Windows API functions that come in both ANSI and Unicode versions. of course, if you'd like the same executable to run on either platform and You're NOT Concerned About The Performance Hit An Ansi Application Incurs Under Windows NT, You Can Forget About The_t Macro. I'll Use_t Throughout this book to make the sample code character set neutral.
Is Wrapping String Literals in _t Macros Sufficient To Make An Application Completely Agnostic with regard to character sets? NOT QUITE. You must also do the following:
Declare characters to be of type TCHAR rather than char. If the _UNICODE symbol is defined, TCHAR evaluates to wchar_t, which is a 16-bit Unicode character. If _UNICODE is not defined, TCHAR becomes plain old char. Do not use char * or wchar_t * to declare pointers to TCHAR strings. Instead, use TCHAR * or, better yet, the LPTSTR (pointer to TCHAR string) and LPCTSTR (pointer to const TCHAR string) data types. Never assume that a character is only 8 bits wide . to convert a buffer length expressed in bytes to a buffer size in characters, divide the buffer length by sizeof (TCHAR). Replace calls to string functions in the C run-time library (for example, strcpy) with the corresponding macros in the Windows header file tchar.h (for example, _tcscpy) .consider The Following Code Snippet, Which Uses The Ansi Character Set:
Char szmsg [256];
PWND-> GetWindowText (SZMSG, SIZEOF (SZMSG));
STRCAT (SZMSG, "Is The Window Title);
MessageBox (SZMSG);
Here's What The Same Code Would Look Like if it were revised to be Character Set Neutral:
Tchar Szmsg [256];
PWND-> GetWindowText (Szmsg, Sizeof (Szmsg) / Sizeof (tchar));
_TCSCAT (SZMSG, _T ("is the window title");
MessageBox (SZMSG);
The revised code uses the generic TCHAR data type, it makes no assumptions about the size of a character, and it uses the TCHAR-compatible string-concatenation function _tcscat in lieu of the more common but ANSI character set_dependent strcat.
There's more that could be said about ANSI / Unicode compatibility, but these are the essentials. For additional information, refer to the online documentation that comes with Visual C or to Jeffrey Richter's Advanced Windows (1997, Microsoft Press), which contains an excellent chapter ON Unicode and a Handy Table Listing The String Macros Defined in Tchar.h and Their C Run-Time Counterparts.Building The Application
The CD in the back of this book contains everything you need to use the Hello program in Visual C . The folder named / Chap01 / Hello contains the program's source code as well as the files that make up a Visual C project. To open the project Simply Select Open Workspace from Visual C 's File Menu and Open Hello.dsw. if You Modify The Application and Want To Rebuild It, Select Build Hello.exe from The Build Menu.
You do not have to use the Hello files on the CD. If you'd prefer, you can create your own project and type in the source code. Here are step-by-step instructions for creating a new project in Visual C 6 :
Select New from the Visual C File menu, and click the Projects tab to go to the Projects page. Select Win32 Application, and enter a project name in the Project Name text box. If desired, you can change the path name-the drive and folder where the project and its source code will be stored-in the Location text box. Then click OK. in the Win32 Application window, select An Empty Project and then click Finish. Add source code files to the project. to enter a source code file from scratch, select New from the File menu, select the file type, and enter the file name. Be sure the Add to Project box is checked so that the file will be added to the project. Then click OK, and edit the file as you see fit. to add an existing source code file to a project, go to the Project menu, select Add to Project and then Files, and pick the file. Select Settings from the Project menu. In the Project Settings dialog box, be Sure what the project name is success in the left pane and then click th e general Tab if The General Page isn't Already DISPLAYED DLL from The Drop-Down List Labeled Microsoft Foundation Classes, and The Click Ok To Register The Change With Visual C .
Choosing Use MFC In A Shared DLL minimizes your application's executable file size by allowing MFC to be accessed from a DLL. If you choose Use MFC In A Static Library instead, Visual C links MFC code into your application's EXE file and the file size grows considerably . Static linking uses disk space less efficiently than dynamic linking because a hard disk containing 10 statically linked MFC applications contains 10 copies of the same MFC library code. On the other hand, an application that is statically linked can be run on any PC, whether or not the MFC DLL is present. It's your call whether to link to MFC statically or dynamically, but remember that if you distribute a dynamically linked EXE, you'll need to distribute the DLL that houses MFC, too. For a release-build MFC Application Created with Visual C Version 6, That DLL IS named mfc42.dll if The program Uses Ansi Characters and mfc42u.dll if it buy unicode characters.The Big Picture
Before we move on, let's pause for a moment and review some of the important concepts learned from the Hello application. The very first thing that happens when the application is started is that a globally scoped application object is created. MFC's AfxWinMain function calls the application object's InitInstance function. InitInstance constructs a window object, and the window object's constructor creates the window that appears on the screen. After the window is created, InitInstance calls the window's ShowWindow function to make it visible and UpdateWindow to send it its first WM_PAINT message. Then InitInstance returns, and AfxWinMain calls the application object's Run function to start the message loop. WM_PAINT messages are converted by MFC's message-mapping mechanism into calls to CMainWindow :: OnPaint, and OnPaint draws the text "Hello, MFC" in the window's client Area by Creating a cpaintdc Object and calling ITS DrawText function.
If you're coming to MFC straight from the Windows SDK, this probably seems like a pretty strange way to do business. Two-step window creation? Application objects? No more WinMain? It's definitely different from the way Windows used to be programmed. But compare Hello's source code to the C program listing back in Figure 1-2, and you'll find that MFC undeniably simplifies things. MFC does not necessarily make the source code easier to understand-after all, Windows programming is still Windows programming -but by moving a lot of the boilerplate stuff out of the source code and into the class library, MFC reduces the amount of code you have to write. that, combined with the fact that you can modify the behavior of any MFC class by deriving from it a class of your own, makes MFC an effective tool for programming Windows. The benefits will really become apparent when you begin tapping into some of the more sophisticated features of Windows or building ActiveX controls and other Windows-based Software Components. with MFC, You Can Get An ActiveX Control Up and Running In Nothing Flat. Without IT-Well, Good Luck.