The MFC Answer Book: Solutions for Effective Visual C++ Applications
Authors: Eugene Kain
Venue:  (1998)

From the Book:PREFACE: Why Another Book on Microsoft Foundation Classes Programming? To answer this question, let us look at a typical MFC programming scenario. First, you attend an MFC training session or read some introductory books on MFC programming. You quickly become able to write and customize small tutorial applications. AppWizard and ClassWizard allow you reach an unprecedented level of productivity. Your applications support the multiple document interface (MDI) and have a professional-looking user interface with a floating toolbar, a status bar, printing and print preview, and so on. You then go back to work and start using MFC to produce great-looking applications. Code flows freely from your keyboard, the wizards work hard at your side, and life looks great under the MFC sun. One day, you start wondering about how to implement new features that were not explicitly covered in the training session. For example: Make your application remember the last active document and automatically reopen it. Support multiple kinds of views on the same document and allow the user to explicitly open any kind of view. Add ToolTips to the controls in a form view. Dynamically switch the view displayed in a window to replace it with another kind of view. Implement an expanding dialog box. Embed a property sheet (tabbed dialog box) inside another window, such as a form view, a dialog box, or a mini frame window. Display a progress indicator in a status bar pane. Have a menu pop up when the user clicks a button on a toolbar or in a dialog box. Support headers and footers in your print and print preview.Displaya custom Printing . . . dialog box with a progress indicator. You feel that implementing these features cannot be that difficult: after all, you have already seen them in other Windows applications. But where do you start looking for an answer? The solution may be as easy as knowing the specific MFC virtual functions that you must override to produce the desired effect or knowing the Windows messages you should trap and handle appropriately. For some features, however, more involved techniques may be neededeven to the point of tracing into MFC's source code to understand just where and how you can act to modify your application's default behavior. One infuriating fact of life is that the answer to your particular question may be lying around somewhere: buried in some MFC programming book or magazine article, on the Microsoft Developer's Network CD-ROM, in the Microsoft Knowledge Base, in the various threads and mailing lists maintained on the Internet, or even in the online books or samples contained on the Visual C++ CD-ROM. The problem is this: How are you going to locate the most relevant and reliable source of information among all these resources? How are you going to find the solution you need right now? Introducing The MFC Answer Book This book is intended to provide ready-to-use techniques that answer the most common real-world questions that typically confront MFC developers. The structure of this book is specifically designed to help you quickly locate the answers youire looking for and integrate the relevant solutions into your own programs. The FAQ format of this book makes it ideally suited to the needs of the developer looking for a quick answer to a pressing question. At the same time, you will find that many techniques will give you a better understanding of the inner workings of MFC applications and more generally help you improve your MFC programming skills. In particular, the Explanations and Additional Comments sections often delve into the MFC source code or undocumented functions to explain how the techniques discussed work and how they differ from or integrate with MFC's default behavior. Key Features of This Book Although most books about Visual C++ and MFC programming answer valid questions about MFC programming and provide useful tips if you read them from cover to cover, most of them are not structured in a way that allows you to quickly find an answer to a given problem. Moreover, even if you find the answer, it is likely to be buried inside a larger discussion and not readily available as a step-by-step technique that you can simply incorporate into your current project to add a required feature. In contrast, The MFC Answer Book is specifically designed to help MFC developers solve their programming problems in the most efficient way: This book is organized so that the table of contents will help you to quickly zoom in on the FAQs that answer your questions. I have made every effort to build a convenient and comprehensive index that will direct you to all the pages relating to any keyword or function referenced in this book. Each FAQ is written in a concise way that first gives you the step-by-step answer you need. Explanations and additional comments are deferred to later sections so that they do not get in the way of the solution but are readily available for those who want to go further than the cookbook recipe and wish to understand what goes on under the hood. Each explanation comes with tested and reusable sample code that you can plug into your MFC application in a few minutes to integrate the required functionality immediately. To summarize: The goal of this book is to offer you the shortest way from a problem to the corresponding step-by-step solution that you can integrate immediately into your current project. Who Should Read This Book This book is written for all MFC developers who wish to solve their MFC-related problems and at the same time learn advanced MFC techniques that will allow them to add a range of sophisticated features to their applications. This book assumes a basic proficiency both in the C++ language and in MFC programming as well as a knowledge of how to use the Visual C++ integrated development environment and tools such as AppWizard and ClassWizard. The Visual C++ wizards are discussed only when used in nonstandard ways to achieve a specific result. To benefit fully from this book, you should already understand the basic MFC concepts presented in the Scribble tutorial described in the Visual C++ documentation: the document/view architecture, message maps, the UPDATE_COMMAND_UI mechanism, dialog data exchange (DDX), and so on. Typically, you will either have followed the Scribble tutorial, attended a training session in MFC programming, or read one of the many introductory books on this topic. Of course, having a more extensive background in MFC programming will not hurt! Quite to the contrary. Based on feedback from reviewers and colleagues, I know that this book will also appeal to experienced MFC developers, who will find many useful techniques to add to their bag of MFC programming tricks. Finally, reading this book will allow all MFC developers to improve their understanding of fundamental MFC concepts and sharpen their MFC programming skills. How To Use This Book This book focuses on the 32-bit MFC version 4.x for Windows 95 and Windows NT. However, most techniques and concepts discussed here also apply to older versions of MFC. They should also remain valid for future MFC versions, because they rely on core MFC classes and behaviors that are not likely to evolve in a way that breaks existing code. I tried to write this book so that it will become a flexible tool that you can use as you want to. This means that you can either read this book from cover to coverI would certainly appreciate it if you door use it as a reference to look up only the specific topics that interest you. Most FAQs are cross-referenced to help you locate all the relevant information you might need even if you jump into the middle of the book. However, before you start hunting for answers to your MFC questions, I suggest that you take a few minutes to read Chapter 0 (Terminology and Conventions) and Chapter 1 (Document/View Architecture Backgrounder) to make sure that we start on the same ground with respect to fundamental document/view architecture concepts. What Is on the CD-ROM The companion CD-ROM contains source code and executables for all of the book's sample programs. The folder hierarchy is organized first by chapter number and then by project name. Thus, the AutoSaveDoc project for Chapter 2 is located in the d:\\Chap02\\AutoSaveDoc folder, where "d:" is your CD-ROM drive's letter. All the executables are located under their respective chapter folders. For example, all the executable sample programs for Chapter 2 are located in the d:\\Chap02 folder. The EkUtil.h and EkUtil.cpp files located at the root of the hierarchy contain the various helper Ek . . . . . . functions and classes that are presented throughout the book. You can choose to copy the whole folder hierarchy from the CD-ROM to your hard disk, copy only the examples that are of interest to you, or access the files directly from the CD-ROM. If you copy files from the CD-ROM to your hard disk, remember to remove the read-only attribute from the files on your hard disk. All sample programs have been compiled and tested under both Visual C++ 5.0 and Visual C++ 6.0. They will also work properly with Visual C++ 4.x, but you will have to manually create the appropriate .mdp project file. Note, however, that the .dsp project files on the CD-ROM have the Visual C++ 5.0 format: if you open them with Visual C++ 6.0, simply answer Yes to the dialog box asking whether you want to convert these files to the new format. Your Feedback Is Welcome I have done my best to accurately present topics that I feel should be of interest to most MFC developers. However, if you think that a topic should be covered differently or should use another technique, don't hesitate to send me e-mail at ekain@awl.com. Also, e-mail me if you want to submit a topic idea or a technique of your own that solves a problem you have encountered, if you find an error or have any problem with this book, or if you have suggestions or want to discuss anything with me. I can promise that I will read all e-mail messages, take them into account, and try to respond to each of them as soon as possible. Note, however, that I may not have the time to answer specific MFC programming questions. You can also visit my Web site at ...