Windows Programming, Under the Hood of MFC: A Quick Tour of Visual C++ Tools - Softcover

Draxler, Laura B.

 
9780134889757: Windows Programming, Under the Hood of MFC: A Quick Tour of Visual C++ Tools

Synopsis

Based on the latest Visual C++ 5.0 release, experienced C++ developers will master the Visual C++ MFC libraries and the Windows Win32 API at the same time; understand the Windows programming model and how MFC classes support it; learn about DLLs, OLE and the Document View model--critical elements of Windows client/server programming; and more. Book & CD-Rom.

"synopsis" may belong to another edition of this title.

About the Author

Laura Draxler, MSE, has been designing and developing Microsoft Windows applications since 1987. As president of The Code Lab Company, she is a consultant and trainer in MFC with Visual C++, Microsoft Windows API, OS/2, UNIX, and C++. In addition to developing Windows applications at numerous corporations, she has taught MFC, Windows, UNIX and PASCAL programming at UC Berkeley Extension and DeAnza College in California since 1991. This book was developed from her teaching of over one thousand professionals and students, as well as her consulting experiences.

From the Back Cover

The first book to teach Microsoft Foundation Classes along with fundamental Windows programming concepts.

Jump-start your Windows development skills by learning the Win32 API as you master MFC. If you have previous development experience on other platforms, you may have been overwhelmed by the hidden features of the MFC when you came to work in Windows.

Windows Programming Under the Hood of MFC gives you the lowdown on these core components of the Windows programming model:


* Windows Program Flow
* GDI
* Menus
* Messages
* Callback window procedures
* Toolbars
* Window resources
* Dialog boxes
* Scrolling
* Child window controls
* Multiple & single document interfaces
* Dynamic Link Libraries

You will learn the key Microsoft Foundation Classes that comprise an MFC application on top of Win32, as well as the following MFC topics:


* Message Mapping
* Common Control Classes
* Form Views
* Object Run-Time Support
* Document Templates
* ActiveX Control Classes
* Object Serialization
* DDX/DDV
* Toolbar Classes

You will also learn how they work together, and how to make the most of them with powerful Windows Visual C++ tools in Developer Studio, such as AppWizard, Resource Editor, ActiveX ControlWizard, Components and Controls Gallery, ClassWizard, Visual C++ Debugger, and Spy++.

As you work through the text, you'll learn how each new concept relates to MFC and its hierarchical structure. Then you'll be ready to shift into high gear, using your existing C and C++ skills to create dynamic applications for the Win32 architecture with Microsoft Visual C++.

Icons throughout the text help you quickly identify the topics under discussion. Each chapter also includes tutorials for self-guided learning.

Aimed at developers, Windows Programming Under the Hood of MFC assumes a knowledge of C++ and data structures. You should also have experience with some graphical windowing environment, and at least a passing familiarity with Windows(r) 95 or Windows NT(tm).

From the Inside Flap

Preface



Welcome to MFC Windows Programming!

Windows Programming Under the Hood of MFC presents the latest Microsoft Foundation Classes (MFC) using Visual C++ 5.X with Windows NT or Windows 95. The purpose of this book is to provide a single comprehensive book on Microsoft Foundation Classes (MFC) and underlying Windows application programming concepts. Currently, the only way to obtain this knowledge is to read two or more books at the same time, trying to synchronize MFC concepts of one book with the underlying Windows concepts of another book.

I wrote this book with the talented computer programmer in mind - the excited developer who wants to jump into Windows development with Visual C++ and MFC, but hasn't grasped the basics of Windows itself yet. Visual C++ is more approachable than earlier Microsoft tools. But just to use it simply, a developer must understand core components of the Windows programming model, such as, messages, callback window procedures, window resources, dialog boxes, child window controls, and multiple or single document interfaces. Even with form-based applications assembled with regular Window controls and ActiveX controls, the developer still has to paint windows. The Microsoft Foundation Classes can't possibly imagine how you want your entire application or ActiveX control to look!

Now, you may be saying, "What is different about this book from all the others?" At a glance, the title and perhaps the Table of Contents won't reveal this. Since Visual C++ was released, I've seen the need for a book which goes "under the hood" of the Microsoft Foundation Classes (MFC) and targets developers who have not programmed with Windows before. Based on my years teaching primarily at University of California Berkeley, I have observed anxious students who want to dive in and take-off in Windows development but find the steep learning curve overwhelming. With the introduction of Visual C++, many developers have been relieved that they have a clear starting point in Windows development, but quickly they are overwhelmed by what is hidden from them in MFC. So, it has been my role to merge these two worlds, MFC and the Windows model beneath, cross-referencing Visual C++ books with Windows development books for the students. By the end of each quarter, I have been thanked by students for helping them see through the cloud of MFC and to understand what is Windows really doing? This effort has led to hundreds of pages of course notes which I use to supplement text book reading. I still have not found the ideal text book for teaching my classes, so I decided that I would grow my unique course notes into a book for such classes and other MFC programmers.

My approach is different from others because of the way my career has evolved with Windows. I began programming in Windows in 1987 with version 1.03, as one of the early developers of Hewlett-Packard Company's New Wave product. Back then, there wasn't much to see in Windows. That is, there was a file manager which was only slightly better than looking at a directory listing in DOS. What was in Windows, was a rich set of Application Programming Interfaces which opened the door for all kinds of development that Microsoft had not even imagined! This Windows Application Programming Interface or API was the foundation for what became the New Wave Development Kit, a massive development environment that ran on top of Windows. In short, New Wave gave Windows the object-oriented look-and-feel of a Macintosh(!), data sharing between applications, automation and scheduling of common user tasks, ways to work with DOS applications in Windows, and a consistent Help interface for users. Because these "new" Windows features had been created several years before they appeared in Windows itself, I understand how they work and why they were added to Windows over time. This early initiation made it possible for me to build a business of Windows consulting based on under the hood knowledge of Windows. I have learned first hand why even with today's Windows tools, the developer must have a strong grasp of the Windows programming model, memory management, and available C APIs.



Who Should Read This Book

This book is aimed at seasoned computer programmers by diving into MFC and Win32 quickly, after Chapter One's quick tour of Visual C++ 5 tools that are covered in more depth throughout the book. Once you begin this adventure into Windows, you will realize quickly that there is an enormous amount of material to learn and to absorb before you are comfortable with Windows development. For this reason, I strongly advise that you first learn how to develop programs using C++ only. This means that you should feel comfortable programming in and understanding this language. You should be knowledgeable in data structures since Windows has defined numerous ones that you use in MFC programs. I will not discuss C++ (or C) at all unless it pertains to the Microsoft Foundation Classes themselves. My book is different from others out there because I've packed as much Windows as I could without overlooking MFC, assuming you could refer to several good C++ books available. Although MFC doesn't require in depth knowledge of C++, it is wise to minimize your list of unknowns before launching into the large world of Win32 and MFC.

I also assume that you are familiar with the Windows 95 or Windows NT 4 user interface, or that you have significant experience with other windowing user interfaces such as, Motif or Macintosh interfaces. It is not difficult to become familiar with the Windows user interface, but I suggest that you first spend about one week using Windows and Windows programs.

This book can be used also by experienced Windows programmers who want to understand the parallel of MFC and their known Windows world. Through discussions and sample programs, these two overlapping worlds are presented together. Previously, this content could only be found by leafing through two separate books side-by-side on Windows API and MFC.



Windows Development with Visual C++

Up until 1993, Microsoft continually updated its C and C++ compilers and other development tools, but an integrated Windows-oriented development environment did not exist from Microsoft. Developers used third-party Windows tools, used whichever text editor they preferred for source code development, used Microsoft's CodeView or third-party tools for debugging, and various other Windows tools included in the Windows Software Development Kit. This style of coding required programmers to have a strong grasp of Windows concepts to even begin to develop! In some ways, this was good, because nothing was hidden from the developer. With pressure to offer their own Windows integrated C/C++ development environment, Microsoft released Visual C++ 1.0 for Windows 3.1 in 1993. For those who had only used Microsoft tools, this was a major product step from Microsoft. With its release, Visual C++ also opened up the development market to many who wouldn't attempt Windows programming otherwise. As you will see, for C++ programmers in particular, the tools of Visual C++ and the Microsoft Foundation Classes (MFC) included, make it easier to get started in Windows development. But, you will also see that there is still a significant need to learn the Windows programming model beneath MFC and to become familiar with the traditional Windows C function interface.



Windows 95 and Windows NT

I discuss Windows 95 and Windows NT 4 interchangeably in this book, in terms of their common Win32 API. But, these two operating systems differ significantly in design and extensibility. I point out the differences that pertain to the MFC and Windows topics covered in this book. Windows 95 offers much of the Win32 programming API of Windows NT and a lot of advantages over Windows 3.1. Although Windows 95 is quite similar to OS/2, PC users have matured since OS/2's initial release, and are ready for the technology benefits that such an operating system offers. Microsoft has built a Windows following, and will take them into a new era with Windows 95 and Windows NT 4.

Windows 95 is the first 32-bit Windows for today's desktop users, whereas Windows NT was the first 32-bit Windows for enterprise users. Windows 95 provides per-process memory protection with a full virtual memory management scheme that restricts access to the current process' memory, conceptually similar to NT. It supports multithreading and preemptive multitasking as in Windows NT. The Graphics Device Interface (GDI) has been enhanced with more features. The worst features of DOS that affected Windows 3.X have been removed from Windows 95. The user interface and the Program Manager have been replaced with a more object-oriented visual metaphor. This same user interface is used in Windows NT 4 now.

Because Microsoft's target platform included only 4 MB RAM and 80386 microprocessor, there are limitations in the internal design of Windows 95. In particular, some operating system components previously written in tight assembler code for Windows 3.1 were left intact to minimize memory use, but often increase operating overhead from "thunking" the 32-bit API back to the 16-bit API. These issues do not exist in Windows NT, which truly was written from scratch. Several Windows 95 operating system books cover these topics thoroughly. This book will mention the programming features that have been limited or are not operational in Win32 for Windows 95 (that is, the API is there but may not be doing much), but are available in Windows NT.

With the lower costs of improved PC hardware now, you can see a move towards NT over Windows 95 finally. I expect that Windows 95 will fade away within a few years, allowing future versions of NT to grow.



The Importance of the Microsoft Foundation Classes

Included in Visual C++, the Microsoft Foundation Classes (MFC) contain the C++ application framework that Microsoft has developed for cross-platform and C++ Windows development. Because this is Microsoft's development solution, all Windows programmers should at least be familiar with MFC's structure and use. Naturally, as developers enter the Windows world, they will feel comfortable starting with Microsoft's tools, based on the reputation it has built with Windows. Whether developers stand behind MFC or move on to other development tools and application frameworks, MFC will play a role in all of their software development. It may be that MFC is the framework of choice, or the basis for developing another one from scratch, or an extra frame of reference when comparing development environments. But, sooner or later, all Windows developers will find the need to understand MFC in their professional careers.

As with other C++ application frameworks, one of the goals of MFC is to simplify Windows programming by encapsulating and hiding much of the Windows C-language programming model. In most cases, this has been done. But, the design of MFC makes it easier for experienced Windows programmers to use in comparison with other application frameworks. This is because much of the Windows C-language API is not really hidden and some of it is not in MFC at all, causing the need to use the Windows C API directly. This can be an advantage and a disadvantage at the same time. In short, experienced Windows programmers and inexperienced Windows programmers who understand the Windows C programming model are most comfortable with MFC and learn to use MFC optimally. And, after initial exposure to MFC or other application frameworks, most professional developers yearn to understand what is going on underneath! This is the motivation of this book.



MFC and the Windows Programming Model

Chapters Two and Three of this book will introduce MFC while discussing the Windows programming model and the Win32 application programming interface (API) in parallel. As an intense introductory book, it will cover the core components of Windows from an MFC and a Win32 API standpoint. Along the way, the Windows windowing model, the Windows GDI programming model, and the basics of the Windows memory models will be covered. These Windows topics and Win32 API will be mapped into the MFC framework and class hierarchy, providing you a view of the entire development environment. This will enrich your understanding of Windows while learning MFC, so that you can readily return to the Win32 API partially or completely in software development.

For historical and comparative reasons, some Windows API will be described in terms of Windows 3.1. However, the focus of this book will be on the Windows 95 and Windows NT Win32 API.



Core Windows Topics From an MFC Point-Of-View

One of this book's primary goals is to make you a strong Windows developer who can readily use the Microsoft Foundation Classes by understanding its class hierarchy the program design it imposes. Another is to teach core Windows topics that you will need to know before you can move on to more advanced topics.

In this spirit, real Windows programming begins in Chapter Four when you learn about the Windows Graphics Device Interface (GDI) and how to paint your windows, meaning the blank space within the borders of a window. GDI concepts are presented from within MFC and significant parallels are made with the MFC CDC class and the Windows GDI functions. You will learn to use "generic" Windows functions to display characters, geometric figures, and bitmaps, for the basis of word processing to spreadsheet to drawing applications. In addition, Windows TrueType fonts and Windows metafiles will be introduced.

In Chapter Six, I discuss child windows, starting with Windows controls. I describe the use of Windows predefined control windows, such as, Edit, List Box, Combination Box, and Button in conjunction with their respective MFC classes. Newer common controls such as, a Progress Control Bar are also illustrated. The necessary Window scroll bars with their position and size flexibility are covered also. MFC's very useful encapsulation of toolbars containing controls, is discussed through examples.

With an understanding of child window controls, Chapter Seven is on their typical parent window, dialog boxes. Windows dialog boxes are the primary input mechanism for complex settings and actions. The Visual C++ Resource Editor (AppStudio) tool is highlighted here, for its role in developing the appearance of a dialog box or a window that looks like a paper form. ClassWizard plays a major role in creating CDialog and CFormView classes and their message handling functions. In addition, ClassWizard is used to put the automatic Dialog Data Exchange and Validation (DDX/DDV) to work! The Windows functions which have the same name as MFC class members are explained also, which are often needed to fine-tune the controls of a dialog box. The MFC classes for the popular tabbed dialog interface of property pages are introduced too. Finally, the MFC and Windows C interfaces for Windows "Common" dialogs are discussed. These dialogs are the common ones used in all applications, such as, the Open File, Save File, and Search Text dialogs. Using these solutions makes it much easier to implement these often-duplicated dialog boxes.

Even though controls and dialogs have become a major portion of your Windows applications, you need to learn to respond to "raw" mouse and keyboard input from the user in Chapter Eight. Windows informs a program of these events through messages which can be responded to or ignored by a program. My example programs will illustrate how to draw on-the-fly as the user clicks and drags the mouse.



MFC Programming Design: The Document-View Model

In addition to the core components of MFC and Windows programming interfaces, this book will discuss MFC program structure brought about by emerging Windows user interface design and the MFC framework structure itself. Once you can paint static items in your window, in Chapter Five you start learning how to use the MFC Document-View programming model that contains many MFC features. In particular, MFC modularizes programs in terms of document classes (where the data is stored) and view classes (where the window painting occurs).

One motivation for this separation of tasks is to simplify the capability of having multiple views of the same data. Especially as Windows users move to client/server computing, there will be the need to manage multiple views of the same data on different computer systems. The MFC Document-View program design is consistent with Microsoft's compound document architecture that has led to distributed ActiveX technology.

On a single computer system, the Document-View model can help modularize program design so that the program can readily display different views of the same data. For instance, a set of numbers (the data) could be displayed as a table in one view and as a bar chart in another view. MFC provides support for user input changes to one view, to readily inform the other views to update their appearances.

In addition, MFC's support for object persistence through archival storage is based on data storage in an application's document class, separate from non-persistent data of the view class.



Somewhat Advanced but Necessary Programming Topics: ActiveX and DLLs

Chapters Nine and Ten are devoted to Dynamic Link Libraries (DLLs) and Microsoft's ActiveX and OLE development. These topics are both crucial to every Windows developer. OLE and ActiveX provide an object-oriented user interface, for sharing and re-using the data and capabilities of one application within another application. The underlying Component Object Model (COM) is the basis for all future Windows program designs. I will introduce how to use the popular ActiveX controls (OCXs) and how to create your own with MFC.

I refer to ActiveX controls and related technology, as it pertains to other topics throughout the book. I do not expect you to fully understand the role of ActiveX even at the end of this book. But, you should understand how it relates to other Windows and MFC topics, and how to begin using ActiveX in Visual C++. I use both the term "OLE" as well as the term "ActiveX, " even though Visual C++ has dropped the OLE terminology in most of its tools. However, for understanding ActiveX better in some cases, and because the Visual C++ Help uses the term "OLE" freely still, you will benefit from some of the history of OLE and its evolution to COM and ActiveX. Please forgive me if I have left in "OLE" erroneously, where it is now "ActiveX" in Visual C++ 5. Like most books, Visual C++ has been revised since I began writing!

Dynamic Link Libraries or DLLs comprise the majority of "executable" files developed for most applications, primarily because of the DLLs' ability to be shared and re-used easily across applications. When you become a Windows developer, you will need to develop DLLs. A good DLL interface reduces your development time in subsequent applications using the same functionality. And DLLs minimize hard disk usage since the code is not in every application. With MFC, you have two main development choices with DLLs: regular DLLs and MFC extension DLLs. We will look at the advantages and disadvantages of each and the underlying Win32 interface.

The ActiveX and the DLL topics are covered conceptually and from an MFC point-of-view with programming examples.



New Features of Windows 95 and Windows NT

This intent of this book is to include coverage of Windows 95 and Windows NT topics as they pertain to the core Windows topics of MFC programmers. It does not attempt to be a complete reference manual for Windows 95 or NT. It will stress features which have changed the way programmers design their programs to take advantage of these more robust operating systems. And, of course, it will present MFC support where it exists in parallel with Windows functions (in addition to the extras in the Win32 API).

As Windows NT and Windows 95 have introduced preemptive multitasking, multithreading and virtual memory management with per-process protection, new features are available to a broad PC market for the first time with Windows. These features can either be ignored or supported by new Windows MFC programmers. This book does not cover these topics in any depth like an operating system book, because this divergence would be too long for this book. You should refer to an operating system book if the concepts are totally new for you. (NT, Windows 95, UNIX or generic operating systems books)

Features in Windows 95 that pertain to its integration with Windows for Work Groups and other networks are also not covered here. Detailed coverage of Windows Plug-and-Play also are not covered in this book either. There are other books on these topics as well.



Visual C++ Tools Overview

This book includes an overview of the latest visual tools in Visual C++ 5. My goal is to bring you up and running in the environment and knowledgeable about what it has to offer. After an initial chapter on these tools, they will be covered in more depth throughout the book where necessary, to learn how to allow the tools to add code or to design visual interfaces of programs. This book emphasizes programming concepts over the use of Visual C++ tools with the hopes that you will be competent Windows and MFC programmers after completion. You can reference many other detailed Visual C++ (tools) books if you feel the need. And the Visual C++ Help is sufficient for learning how to use its integrated tools.



Program Examples in MFC and Win32 API

Since this book has ongoing comparisons of Windows MFC programming and Win32 C API programming, numerous program examples will demonstrate this parallel. This will be true for all major topics covered, but not necessarily all program examples. By having these side-by-side examples, you will be able to immediately reinforce the parallel discussions. This style will stress the similarities of the two, and also the code encapsulation that MFC provides. In addition to these comparisons, the C API examples will often improve your comprehension of key Windows design concepts.

Much of this book builds on a single application at first, and then modifies it for comparison as a DLL and an ActiveX control. I have found this technique very useful in teaching students, and they want to finish a course feeling like they have created a practical, comprehensive project. At the end of this book, you should feel the same.

These program examples are provided on an attached CD-ROM.



Tutorials for Teaching Courses or Self-Teaching

Because this book is intended for Windows and MFC development courses, tutorials have been included in each chapter. These tutorials will step you through using the Visual C++ tools and MFC for specific programming tasks. These tutorials can be used by an instructor for computer lab exercises or for homework.

These tutorials also serve as a guide for the independent reader's progress. They will help you teach yourself about Visual C++, MFC and Windows while reading this book.



Hardware, Operating System, and Visual C++ Requirements

Since this book describes Visual C++ running on Windows 95 and Windows NT, you can install either Windows operating system for the examples. The sample programs have been tested and will run on both Windows 95 and Windows NT systems. However, because of the differences inherent in these two operating systems, it is possible that some things will differ, such as, program speed, memory use, and the thoroughness of the Windows implementation.

The hardware required for the sample programs is the same hardware required by the respective Windows 95 or Windows NT operating systems. For Windows 95, you must minimally have an 80386 PC with 4 MB RAM (16 MB recommended) and a pointing device, such as, a mouse or pen. For Windows NT, you will need an 80386 PC (80486 recommended) minimally, and 8 MB RAM (32 MB recommended) and a pointing device.

This book and its sample programs use Visual C++ 5.X. To run the sample programs and for your Visual C++ menus to be in sync with this book's descriptions, you should install Visual C++ 5.0 or higher. Further, if you are a new Windows developer, you should not bother to acquaint yourself with the old Windows 3.1 and Visual C++ 1.5. Because most developers do not purchase the reference manuals anymore, I strongly advise that you install all of the Windows Visual C++ Help.



This Book's Conventions

Throughout this book and in its Table of Contents, italicized characters are used to identify MFC class names, their members, Win32 function names, the names of specific tools within Visual C++, and the Visual C++ Developer Studio menus and button labels. Windows messages are CAPITALIZED as they are used in Windows programs. Filenames, source code, and keys that you press, are in Helvetica font.

Because this book discusses Windows and MFC in parallel and it sometimes is confusing to keep track of it, I've added icons to the margins to alert you of the kind of topic being discussed at times. They are:

Win32Win32 Under the Hood Concept MFCMFC-specific concept that may not be relevant in Win32 only programming, or it is provided for clarification in MFC. Win16Win16 concept for comparison with Win32. Visual C++ 5New feature or is used in comparison with a 4.X feature. Visual C++ 4.XComparison with the last release FYI, for clarification. ActiveXActiveX or OLE concept - especially for new VC 5 features. Visual BasicVisual Basic concept for comparison or for clarification.

"About this title" may belong to another edition of this title.