Electronic Review of Computer Books

[ ERCB Home | New | Feature | Brief | DDJ | Letters | Links ]

[an error occurred while processing this directive]

Vital Statistics

Title The MFC Answer Book
Author Eugène Kain
Publisher Addison-Wesley
www.awl.com
Copyright 1998
ISBN 0-201-18537-7
Pages 674
Price $42.95


Beyond the Theory -- Put MFC to Work!

In my opinion, there are three categories of skills that a software developer needs to possess in order to successfully bring a software project to completion, correlated to one another by an intrinsic dependency.

The first is a clear understanding of the problem’s domain under analysis, coupled with a solid grasp of the high-level paradigms that are going to be embraced to model the architecture of the solution. The second is a deep knowledge of the technologies, libraries and tools employed in the detailed design phase (and particularly in the implementation phase) of the development process. The third is a lot of direct experience amassed by applying the tools and technologies that were elected to have a role in the project in real world scenarios. Awareness and knowledge of the concepts and theoretical relations among the elements of the system constitute the first key step in the right direction, but alone they do not suffice to produce actual solutions to real problems.

By its very nature, the first skill crosses the borders between the diverse development tools and technologies -- it spans several languages (including the informal one spoken by the customer) and the command the programmer has on it increases proportionally with the accumulation of more positive experiences in any kind of software project, no matter how apparently unrelated to the current one they might seem. The second skill can be acquired first of all by reading a few well-focused books; speaking of MFC, many such titles have been populating the shelves of any respectable technical bookstore for several years. But when it comes to the third point, the scene suddenly darkens. Finding even extensive info on the goals and the workings of MFC is relatively easy, but on the road that takes one from that level to the utilization of the framework to accomplish real-world tasks, the programmer is generally left to his/her raw intuition.

Well, he was until Eugene Kain decided to take the time to write this book. The MFC Answer Book is organized as a large collection of frequently asked questions (FAQs) and programming problems accompanied by the detailed description of how the MFC classes (and the Visual C++ wizards) can be leveraged to build effective solutions. The questions are divided into categories based on the main class, the MFC-specific mechanism, or the user-interface feature they can be more closely associated with. Among the many categories you can find manners to customize the infamous document/view paradigm, manage documents, customizing dialog box elements, operating menus, toolbars and status bars, and coping with common issues of the printing and print preview mechanisms. For every FAQ, the question is first formulated in a concise format. Subsequently, the author provides the theoretical explanation of what the question really means and how the MFC machinery can accomplish it. Then the equivalent source code is presented in its entirety, coupled by both inline comments and step-by-step instructions. Occasionally, this part is followed by a section that delves deeper into the MFC architecture and unearths its interaction with the Windows APIs, obviously aimed at the more curious readers who wish to learn why the solution would work like it does. Eventually the "See Also" section suggests other FAQs covered by the book that are strictly related, and therefore would probably be a profitable read as a companion to the current one.

The underlying idea of this text is not new or particularly revolutionary: Other titles on the market clearly resemble the same structure and a similar solution-oriented perspective on MFC, of which Al William's MFC Black Magic (published by The Coriolis Group) in particular is worth mentioning. Moreover, most of the techniques explained do not emerge for their originality, and more often than not they had already been published in a substantially equivalent form on some Windows development magazine, on the Internet MFC FAQ, or on web sites specifically devoted to collecting short tips and code samples (the most famous being CodeGuru).

Despite this, I would advise developers of any experience who are directly involved in the creation of GUI MFC applications to add it to their personal bookshelves. The vastness of the spectrum of topics covered and the depth of the explanations make it suitable for even the most proficient MFC programmers, while the step-by-step approach followed in the descriptions of how to generate the actual code guarantees the usability by the less experienced practitioners.

Finally, even though the quality of the editing is lacking at times, room wasted by redundant screenshots, and code listings could have been easily reduced, the writing style flows pleasantly and you rarely find yourself rereading a paragraph because you did not understand it at first. Probably not a book that will leave a sign in history, but surely one of undeniable usefulness for real-world MFC programmers.

-- Davide Marcato (marcato@programmers.net or http://www.DavideMarcato.com)


Excerpts from The MFC Answer Book

FAQ 2.5 ("How do I associate multiple file extensions with the same document or view class?"), page 127

"Sometimes, you would like your application to have one unique document class or view class (or both) associated with different file extensions. For example, a simple ASCII file text viewer should be able to read files with extensions such as .TXT, .ASC, or .INI. Any of these extensions should map to the same document and view class. How can we trick MFC into accepting this behavior? As explained in FAQ 2.4 How does MFC select the document template object to use when opening a file?, the algorithm used by MFC is essentially based on the file's extension, which gets compared with the filterExt substring of each registered document template's resource string.

However, each document template object can be associated with only one resource string and therefore with no more than one file extension -- "no more than one" because the filterExt substring can be left empty. Therefore, you will need to create (usually in your application's InitInstance() function) one distinct document template object for each file extension that you want to use in your application. Remember that the document template constructor takes the following four arguments:

Each of the additional document template objects that you will create will share the same arguments as your initial document template object for the document, frame window and view classes, because you want to handle the various file extensions using the same C++ classes. However, each of these document template objects will have a distinct resource ID. This arrangement will allow you to associate a different resource string -- and in particular a different filterExt substring -- with each document template object.

Following a simple sample will help you better understand the necessary steps. We will assume that your application’s InitInstance() function initially registers only one document template object for the .TXT file extension, using code similar to that shown in Listing 2.9. […]"

FAQ 5.12 ("How do I embed a property sheet inside a miniframe window?"), page 381

"Have you ever wanted to include in your application a floating palette similar the Properties window of Developer Studio? Embedding a property sheet inside a miniframe window allows you to achieve a cool visual effect as shown in Figure 5-15.

Step 1. Use ClassWizard to create a new CMiniFrameWnd-derived class.

Step 2. Change the access of the generated constructor access to public -- instead of protected -- and manually add a public member variable of type CyourPropertySheet to your miniframe class, as shown in Listing 5-34.

Step 3. Use ClassWizard to ass a handler in your miniframe class for the WM_CREATE message. Implement this handler as shown in Listing 5-35 to create the embedded property sheet.

Step 4. When you want to create and show the miniframe window, use code similar to that shown in Listing 5-36. […]

See Also


Table of Contents

Foreword by Scot Wingo

Preface

Acknowledgments

Chapter 0 Terminology and Conventions 1

Terminology Used in This Book 1

Conventions Used in the Sample Code in This Book 3

Chapter 1 Document/View Architecture Backgrounder 5

FAQ 1.1 What is the document/view architecture? 6

FAQ 1.2 What are the benefits of the document/view architecture? 6

FAQ 1.3 What components make up the document/view architecture, and how do they relate to each other? 8

FAQ 1.4 What is the role of the document? 12

FAQ 1.5 What is the role of the view? 13

FAQ 1.6 What is the role of the view's frame window? 14

FAQ 1.7 What is the role of the document template? 15

FAQ 1.8 What is the role of the document template resource ID? 17

FAQ 1.9 How do the document/view architecture component objects get created? 19

FAQ 1.10 How does MFC route command messages? 21

FAQ 1.11 How does MFC implement the standard menu commands? 24

Chapter 2 Documents and Document Templates 29

Managing Document Templates 30

FAQ 2.1 How should I create and reference multiple document templates in my application? 30

FAQ 2.2 How do I manage multiple document templates without having a dialog box pop up each time the user tries to create a new document? 33

FAQ 2.3 How do I create the resources associated with a new document template resource ID? 40

FAQ 2.4 How does MFC select the document template object to use when opening a file? 42

FAQ 2.5 How do I associate multiple file extensions with the same document or view class? 47

Managing Documents 52

FAQ 2.6 How do I prevent an MDI application from opening a new (empty) document at start -- up? 52

FAQ 2.7 How do I programmatically create a new (empty) document? 60

FAQ 2.8 How do I programmatically open an existing document file? 64

FAQ 2.9 How do I make my application remember the last active document and automatically reopen it? 66

FAQ 2.10 How do I make my documents autosave themselves without prompting the user? 72

FAQ 2.11 How do I implement a "Save all documents" menu command that does not prompt the user before saving each modified document? 74

FAQ 2.12 How do I programmatically close a document? 79

FAQ 2.13 How do I implement a "Close all documents" menu command? 83

Managing the Recent Files List (MRU) 88

FAQ 2.14 How do I customize the number of recent files shown in the "File" menu? 88

FAQ 2.15 How do I programmatically add a specific string to the Recent Files list? 94

FAQ 2.16 How do I intercept the selection of a Recent Files item by the user? 97

FAQ 2.17 How do I make my application automatically open the document that is at the top of the Recent Files list? 101

Miscellaneous Items 103

FAQ 2.18 How do I get a pointer to the currently active document? 103

FAQ 2.19 How do I iterate through the list of all the currently opened documents? 107

FAQ 2.20 Why doesn't my application register its document files with Windows Explorer, and how do I correct this situation? 114

Chapter 3 Views and Frame Windows 117

General Topics 118

FAQ 3.1 How do I add a new kind of view or frame window to my application? 118

FAQ 3.2 How do I choose between implementing a specific behavior in the view class or in the view's frame window class? 124

Opening and Closing Views and Frame Windows 125

FAQ 3.3 How do I programmatically open a view based on a specific document template? 125

FAQ 3.4 How do I open two (or more) specific views each time a new document is created? 129

FAQ 3.5 How do I programmatically close a view? 136

FAQ 3.6 How do I programmatically close all the views opened on a document? 136

FAQ 3.7 How do I prevent a view from being closed by the user? 137

Managing Size and Positions 145

FAQ 3.8 How do I programmatically resize or reposition a view? 145

FAQ 3.9 How do I set the initial position and size of a view? 147

FAQ 3.10 How do I center my main application window? 150

FAQ 3.11 How do I center a view? 154

FAQ 3.12 How do I make a frame window nonresizable? 156

FAQ 3.13 How do I limit the maximum or minimum size of a view? 158

FAQ 3.14 How do I limit the repositioning of a view? 162

FAQ 3.15 How do I make a view initially appear minimized or

maximized? 167

FAQ 3.16 How do I make my application start maximized or minimized? 171

FAQ 3.17 How do I make my main application window stay always on top of other windows? 176

FAQ 3.18 How do I make one of my views stay always on top of other views? 178

FAQ 3.19 How do I implement a full-screen view mode like the one in Visual C++ and Word? 183

Managing Captions, Icons, Cursors, and Background 191

FAQ 3.20 How do I customize the captions of my views? 191

FAQ 3.21 How do I show a "modified" indicator in the captions of the views associated with a "dirty" document? 197

FAQ 3.22 How do I modify the icons associated with each kind of window in my application? 200

FAQ 3.23 How do I dynamically change the icon of my main application window ? 201

FAQ 3.24 How do I dynamically change the cursor for a view 206?

FAQ 3.25 How do I display an hourglass cursor during a lengthy operation? 210

FAQ 3.26 How do I change the background color of a view? 212

FAQ 3.27 How do I draw on the main application window background? 215

Form Views 224

FAQ 3.28 How do I keep my form views synchronized with my other views? 224

FAQ 3.29 How do I make a form view initially appear with the exact size of the associated dialog resource? 228

FAQ 3.30 How do I use the UPDATE_COMMAND_UI mechanism in form views? 229

FAQ 3.31 How do I change the background color of a form view? 234

FAQ 3.32 How do I add ToolTips to the controls in a form view? 238

Splitter Windows 241

FAQ 3.33 How do I program a window with both horizontal and vertical static splitter panes (three-way splitter)? 241

FAQ 3.34 How do I lock a splitter window so that the user cannot move the divider line? 246

FAQ 3.35 How do I programmatically resize the panes in a splitter window? 249

FAQ 3.36 How do I visually show the user which splitter pane contains the active view? 253

Switching Views 257

FAQ 3.37 How do I dynamically switch the view displayed in an MDI child window or SDI main application window? 257

FAQ 3.38 How do I dynamically switch the view displayed in a splitter window? 264

Miscellaneous Items 269

FAQ 3.39 How do I get a pointer to the active view? 269

FAQ 3.40 How do I get a pointer to the active frame window? 270

FAQ 3.41 How do I iterate through the list of all the views associated with a document? 271

Chapter 4 Dialog Boxes 273

General Topics 274

FAQ 4.1 How do I set the initial position of a dialog box? 274

FAQ 4.2 How do I center a dialog box in relation to another window? 278

FAQ 4.3 How can a dialog box access the active document (or view) object? 283

FAQ 4.4 How do I control the background color of a dialog box? 286

FAQ 4.5 How do I add a preview area to (draw inside) a dialog box? 290

FAQ 4.6 How do I add an icon to a dialog box? 294

FAQ 4.7 How do I implement an expanding dialog box? 295

FAQ 4.8 How do I add a toolbar to a dialog box? 304

Managing Controls on a Dialog Box 316

FAQ 4.9 How do I choose the control that will initially have the focus when a dialog box is displayed? 316

FAQ 4.10 How do I implement custom validation rules in a dialog box? 317

FAQ 4.11 How do I change the font and color of controls on my dialog box? 321

FAQ 4.12 How do I use the UPDATE_COMMAND_UI mechanism in dialog boxes? 324

FAQ 4.13 How do I add ToolTips support to the controls in a dialog box? 330

Chapter 5 Property Sheets 335

General Topics 336

FAQ 5.1 How do I build and use a property sheet? 336

FAQ 5.2 How do I manage the Apply button in a property sheet? 342

FAQ 5.3 How do I programmatically change the active page in a property sheet? 349

FAQ 5.4 How do I customize the standard property sheet buttons? 351

FAQ 5.5 How do I control the size of my property sheet window? 357

Managing Tabs 361

FAQ 5.6 How do I choose between a stacked row of tabs and a single row of scrolling tabs? 361

FAQ 5.7 How do I change the captions of the tabs of my property pages? 363

FAQ 5.8 How do I add icons to the tabs of my property pages? 366

Embedding Property Sheets 367

FAQ 5.9 How do I embed a property sheet inside a dialog box? 367

FAQ 5.10 How do I embed a property sheet inside a form view? 375

FAQ 5.11 How do I embed a property sheet inside a splitter window pane? 378

FAQ 5.12 How do I embed a property sheet inside a miniframe window? 381

Chapter 6 Toolbars and Status Bars 385

Toolbars 386

FAQ 6.1 How do I add one or more toolbars to my main frame window or to a view's frame window? 386

FAQ 6.2 What are the various options available for controlling the position, orientation, and general behavior of my toolbars? 393

FAQ 6.3 How do I programmatically dock a toolbar next to another one? 399

FAQ 6.4 How do I add a combo box to a toolbar? 403

FAQ 6.5 How do I add text labels to my toolbar buttons? 412

FAQ 6.6 How do I implement a menu that allows users to select the toolbars they want to see? 416

FAQ 6.7 How do I allow users to choose the toolbar they want to see by selecting it in a popup menu that appears when they right-click on a docking zone? 422

FAQ 6.8 How do I dynamically switch between different toolbars? 425

Status Bars 433

FAQ 6.9 How do I add a custom status bar to a view's frame window? 433

FAQ 6.10 How do I update the text of my status bar panes? 437

FAQ 6.11 How do I customize my status bar font? 444

FAQ 6.12 How do I draw in a status bar pane? 447

FAQ 6.13 How do I maximize the message pane when displaying menu prompts? 453

FAQ 6.14 How do I display a progress indicator in a status bar? 462

General Control Bar Topics 467

FAQ 6.15 How do I programmatically show or hide a control bar? 467

FAQ 6.16 How do I set the caption of a floating control bar? 470

FAQ 6.17 How do I save and restore the position and state of my control bars? 473

FAQ 6.18 How do I create custom control bars? 476

Chapter 7 Menus 499

FAQ 7.1 How do I handle several distinct menu commands with a single function? 499

FAQ 7.2 How do I implement a right-button popup menu (context menu)? 506

FAQ 7.3 How do I display a popup menu when the user clicks a button on a toolbar? 510

FAQ 7.4 How do I display a popup menu when the user clicks a button in a dialog box? 513

FAQ 7.5 How do I implement an owner-drawn menu? 521

Chapter 8 Printing and Print Preview 535

FAQ 8.1 What is the MFC printing architecture? 537

FAQ 8.2 How do I implement printing and print preview in my MFC application? 544

FAQ 8.3 How do I print a document whose page length is not known in advance (on-the-fly pagination)? 553

FAQ 8.4 How do I predict page breaks as Microsoft Word does? 560

FAQ 8.5 How do I programmatically change the printing orientation (portrait versus landscape) on-the-fly? 562

FAQ 8.6 How do I print without prompting the user with the standard Print dialog box? 565

FAQ 8.7 How do I customize the standard Print dialog box and retrieve the options selected by the user? 569

FAQ 8.8 How do I customize the Printing . . . dialog box? 575

FAQ 8.9 How do I stop or abort a print job? 583

FAQ 8.10 How do I implement print preview in shades of gray for a monochrome printer? 584

FAQ 8.11 How do I distinguish between drawing, print preview, and actual printing in my code? 586

Appendix A Utility Functions and Classes 591

Appendix B Bibliography and Additional Resources 641

Index 645


Quick Rating

Readability Star Star Star
Originality Star Star
Organization Star Star Star HalfStar
Accuracy Star Star Star
Consistency Star Star Star
Depth Star Star Star
Timeliness Star Star HalfStar
Editing Star Star
Design Star Star
Overall Value Star Star Star

Explanation of ERCB rating scale: No stars = unacceptable, 1 Star = marginal, 2 Stars = average, 3 Stars = above average, 4 Stars = exceptional.


Copyright ©1999 Electronic Review of Computer Books
Created 2/1/1999 / Last modified 2/6/1999 / webmaster@ercb.com