Developing Professional Applications for Windows 98 and NT Using MFC

Developing Professional Applications for Windows 98 and NT Using MFC

by Marshall Brain, Lance Lovette

Paperback

$42.08 $49.99 Save 16% Current price is $42.08, Original price is $49.99. You Save 16%.

Product Details

ISBN-13: 9780130851215
Publisher: Prentice Hall Professional Technical Reference
Publication date: 05/27/1999
Series: Prentice Hall Microsoft Technology Series
Pages: 856
Product dimensions: 7.03(w) x 9.21(h) x 1.60(d)

Read an Excerpt

Preface:

Getting Your Bearings

You are probably opening this book because you are new to Windows Programming or because you are new to MFC (Microsoft Foundation Classes) or the Visual C++ programming environment. For example, you might be an experienced UNIX or Macintosh programmer. Or perhaps you have a lot of talent with C programming and command-driven user interfaces on PCs and want to move over to Windows. You may be experienced with Windows programming in C, but have never before used MFC and C++ to develop Windows applications. Regardless of your origin, you will find that as you try to make your transition you are hampered by two problems. The purpose of this book is to quickly solve those problems so that you can begin creating your own professional applications with Visual C++ as quickly as possible.

The first problem is mental: you have to get past the wall that surrounds Visual C++. That wall arises because of the obvious complexity of the Windows and C++ programming environments. When you load Visual C++ from its CD, you notice that there are tens of thousands of pages of documentation, hundreds of sample programs, and untold megabytes of help files. No one has the time to sort through all of this material, but you know that hidden in those megabytes are hundreds of important concepts that you need to master.

The second problem is more pedestrian: you have to pick a place to start. But where should you begin? How do you write a simple Windows application? How do you learn how to write an advanced one?

This book is designed to help you move into the Visual C++ environment rapidly and confidently. The purpose of thischapter is to help you get your bearings in this new environment. It will introduce you to Visual C++ and then give you a starting point and a direction so that you can become an accomplished Windows programmer very quickly using the most modern tools and techniques available.

What is Visual C++?

The Visual C++ environment is huge and can be extremely intimidating initially. Visual C++ combines a complete suite of powerful tools into a single application development environment, and the first time you face all of these tools it can be very difficult to discern what they all do or how to use them. When you look at the book reader application that comes with the Visual C++ CD-ROM, you face another hurdle: You find thousands and thousands of pages in many different books. The thought of wading through all of these manuals can be daunting.

So letÕs start at the beginning and look at Visual C++ in an organized way. First of all, what is it? Here is a brief summary:

  • Visual C++ is a C++ compiler
  • Visual C++ is a debugging environment
  • Visual C++ is an application framework generator
  • Visual C++ is a project manager
  • Visual C++ is an easy way to design and implement menus, dialogs, and other "resources"
  • Visual C++ is a programmer acceleratorÑseveral tools inside Visual C++ are designed to make you more efficient by making your life as a programmer easier or by reducing the code you must write

In other words, Visual C++ is a complete and extremely powerful application development environment. In order to take full advantage of this environment, you have to become comfortable with all the too to know how they can work together to accelerate your software development cycle.

In its most basic form, Visual C++ is simply a C++ compiler. You can use it to create simple text programs in C or C++. If you would like to try this out, go to Appendix B.1 and work through the example there. You will find that it is extremely easy to write, compile, and debug simple text programs using Visual C++.

Most people who purchase Visual C++ do not want to create text programs, however. They want to create advanced Windows applications that make effective use of the Windows 98 and Windows NT user interface. To do this, you must know C++, and you must understand the MFC hierarchy. MFC is designed make you as productive as possible by encapsulating common Windows code in classes that are already written, tested, and debugged. Once you invest the time to learn MFC, you are greatly rewarded in increased speed, flexibility and robustness.

Part 1 of this book gives you a thorough introduction to MFC. It shows you the basic principles used in every MFC program you write. Part 2 gives a complete overview of all the controls and features that MFC offers. Part 2 contains hundreds of examples that make it easy to understand the different MFC classes.

Once you feel comfortable with MFC, you are ready to begin creating professional Windows applications. Part 3 introduces the AppWizard, the ClassWizard, and the resource editing tools of Visual C++. The AppWizard is your starting point when creating any full-blown Windows application: It helps you by generating a complete file framework that organizes the entire application around a consistent core of MFC classes. The ClassWizard, in combination with the resource editing features that the Visual C++ environment provides, then makes it easy to add to and complete your application by helping you design, create, and install menus, dialog boxes, and other application resources. The ClassWizard also helps you add the code that lets your application respond to user input properly. Using these three toolsÑthe AppWizard, the ClassWizard, and the resource editorsÑtogether with the MFC class hierarchy, it is extremely easy to complete professional applications very quickly. Part 3 contains four different example applications to help demonstrate the process.

Part 4 continues by demonstrating advanced features. It shows you how to use a variety of techniques to create such things as expanding dialogs, property sheets, dialog bars, splash screens, self-drawn controls and bitmapped backgrounds. These techniques add significant utility to your applications when used appropriately. Finally, Part 5 concludes the book by discussing advanced MFC classes for database connectivity, OLE features, and so on.

Available Documentation

The Visual C++ CD-ROM contains over 100 megabytes of on-line documentation covering various aspects of Windows, MFC, and the tools available in Visual C++. It contains many more megabytes of sample code. The MFC class hierarchy contains hundreds of different classes holding thousands of member functions. The Win32 API contains thousands of functions as well. All of this material is documented in on-line help files. Obviously, there is no lack of documentation with this product.

This book, therefore, makes no attempt to replace the documentation. Its goal is to help you wind your way through the Visual C++ forest and find what you need. Using the base you gain from reading this book, you will be able to approach Visual C++ and begin using it in productive ways very quickly.

There are currently seven different types of documentation provided by Microsoft for Visual C++ and MFC:

  1. On-line Books Ð A series of manuals on the CD-ROM that act as the documentation for the system. The collection of books is available from the Contents section of the Help menu. Look at the titles of all the different books and articles available. You will find that there are many. The books cover Visual Studio, the tools within Visual Studio like the compiler, debugger, etc., the many function libraries available, etc
  2. Sample Code Ð The Visual C++ directory on your hard disk may contain a sample directory that contains source code demonstrating a wide variety of techniques. Some of the samples are written in C, while other samples use MFC and C++.
  3. Developer Network CD Ð MicrosoftÕs DeveloperÕs Network CD provides quite a bit of additional sample code, along with books and files containing a variety of valuable information. You receive this CD when you become a member of the Microsoft DeveloperÕs Network.
  4. MicrosoftÕs Web Site - the Microsoft Web site contains knowledge bases and articles that may be helpful.
  5. Internet News Groups - Numerous newsgroups and mailing lists on the Internet bring C++/MFC developers together to share tips and techniques. There are also other web sites available full of sample code and tips.

Using all of these different forms of documen find anything you need to know. The key is understanding where and how to look for what you need. This book will help accelerate that process tremendously.

Road Map

The tools in Visual C++ require a great deal of prior knowledge if you want to use them effectively. For example, when you open the Visual C++ package and load the CD, you may have the impression that you can use the AppWizard to generate any program you like. Unfortunately, the code that the AppWizard generates is virtually worthless unless you know a good bit about MFC already. That is why this book is structured the way it is. The progression presented in this book is exactly the progression you will need to follow if you do not already know MFC. However, different people come into Visual C++ with varying levels of experience and different goals. Here is a road map to guide you through the material so that you can find the best starting point for your particular situation:

  • If you do not know C++, you will need to learn it. Proceed to the accelerated introduction to C++ in Appendix A of this book.
  • If you want to simply try out Visual C++ and compile some simple programs, proceed to Appendix B. It will show you how the compiler works and how to compile and debug simple applications.
  • If you know C++ but have never done any Windows programming of any kind, proceed to Part 1. It will teach you the fundamentals of event-driven programming and then quickly introduce you to MFC programming.
  • If you have experience with Windows programming in C but have never done Windows programming using C++ and MFC, proceed to Part 1. It will quickly introduce you to the MFC class hi programming.
  • If you have used MFC before (for example, if you are familiar with MFC version 1.0) but are unfamiliar with the new application development tools like the AppWizard and the ClassWizard, skim Part 2 and then proceed to Part 3 for a complete introduction to the tools.
  • If you are familiar with Visual C++ and MFC but want to learn about a variety of techniques that can make your applications look more professional, turn to Part 4. It will show you how to create things like splash screens, expanding dialogs, property sheets, and self-drawn controls.
  • If you are a corporate programmer who needs to attach to a client/server database, pay particular attention to Chapter 33 in Part 5.

Common Questions

The goal of this section is to show you how to find answers to the most common questions about Visual C++ and MFC. You may wish to scan this list now and periodically in the future to quickly find answers to your questions.

Part 1

  1. What is MFC? Why does it exist? See Chapter 1
  2. How do I compile and run a simple MFC program? See Appendix B.3 and Chapter 1.
  3. How do I create a simple "Hello World!" program in MFC? What does the code actually mean? See Chapter 2.
  4. I have found the AppWizard, but when I run it I find it generates 15 files that make absolutely no sense to me. What do I do? See the discussion at the beginning of Part 3 of this book.
  5. How do I create a simple MFC control? See Chapter 3.
  6. How do I customize MFC controls and change their styles? See Chapter 3.
  7. How do I create a push button and respond to its events in MFC? See Chapter 4.
  8. What is a message ma scroll bar and respond to its events? See Chapter 4.
  9. How do I create an edit control and respond to its events? See Chapter 5 and Chapter 8.
  10. How do I create simple applications? See Chapter 5.
  11. How do I make a simple application appropriately handle tab keys, accelerators, etc.? See Chapter 5.

Part 2

  1. What is a resource? What is a resource file? What are the advantages of resources? See Chapter 6.
  2. How do I create and use icon, dialog, menu, string table, and accelerator resources? See Chapter 6.
  3. How do I create a message dialog? A File Open dialog? A Font dialog? A Color dialog? A Print dialog? A Find/Replace dialog? See Chapter 7.
  4. What is the difference between modal and modeless dialogs? See Chapter 7.
  5. How do I use an edit control in single and multi-line modes? See Chapter 8.
  6. How do I create a simple text editor? See Chapter 8.
  7. How do I create and use lists, drop down lists, and combo boxes in my applications? See Chapter 9 and Chapter 20.
  8. How do I make multi-column and tabbed lists? See Chapter 9.
  9. How do I load and display system and custom icons? See Chapters 6 and 10.
  10. How do I change the application cursor? See Chapter 10 and 11.5.3.
  11. How do I display a watch cursor? See Chapter 10.
  12. How do I perform background processing while the application is idle? See Chapter 10.
  13. What is a document template? See Chapter 10 and Chapter 16.
  14. How do I create an MRU file list? See Chapter 10.
  15. How do I use INI files with my applications? See Chapter 10.
  16. How do I draw lines, rectangles, circles, et to an application? See Chapter 11.
  17. How do I respond to mouse clicks in a drawing? See Chapter 11.
  18. How do I create rubber-banded lines, rectangles, etc. in a drawing?
  19. How do I create a drawing space larger than the current window? See Chapters 11 and 15.
  20. How do I create animated drawings? See Chapter 11.
  21. How do I work with text and binary files in MFC? See Chapter 12.
  22. How do I work with strings in MFC? See Chapter 12.
  23. How do I work with time values in MFC? See Chapter 12.
  24. Is there an easy way to create arrays, lists and hash tables in MFC? See Chapter 12.
  25. What debugging facilities are built into MFC? How do I make use of the MFC exception handling mechanisms? See Chapter 13.
  26. How do I use TRACE and ASSERT statements? See Chapter 13.
  27. How do I prevent and detect memory leaks in my applications? See Chapter 13.

Part 3

  1. Are there any simple applications in this book showing me how to use the AppWizard and ClassWizard? See the drawing example, the editor example, the form example and the address list example in Part 3 of this book.
  2. What is the AppWizard? What is the ClassWizard? How do I use them to speed up application development? See Chapter 14.
  3. How do I create a simple framework with the AppWizard? See Chapter 14.
  4. What do all of the files generated by the AppWizard do? See Chapter 14.
  5. What is the document/view paradigm? See Chapters 14, 15 and 18.
  6. What do the STDAFX files do? See Chapter 14.
  7. Can you give me a simple example of the AppWizard and ClassWizard in action? See Chapter 14.
  8. How paradigm? See Chapter 15.
  9. What is the difference between an SDI and an MDI application? See Chapter 15.
  10. How do I understand what is going on inside the AppWizard framework? See Section 15.3 and Chapter 21.
  11. How do I add new menus and menu options to an application? See Chapter 15.
  12. How do I add scrolling to a drawing application? How do I use splitter windows? See Chapter 15.
  13. How do I add a new dialog to an AppWizard framework? How do I use DDX and DDV? See Chapters 15 and 18.
  14. How do I add a dialog class with the ClassWizard? See Chapters 15 and 18.
  15. How do I add printing to an application? What do the MFC printing functions do? How do I handle multi-page printing? See Chapters 15 and 18.
  16. How do I create a text editor with the AppWizard? See Chapter 16.
  17. How do I handle multiple document types in a single MDI application? See Chapter 16.
  18. What is a document template? See Chapter 16.
  19. How do I use form views? How do I put controls on the face of an application? See Chapter 17.
  20. Can you give me an example that combines all of these different concepts in a single application? See Chapter 18.
  21. How do I create a resizable tabbed list in a form view? See Chapter 18.
  22. How do I enable and disable menu options? See Chapters 18 and 6.
  23. How do I customize the tool bar and status bar? See Chapter 18.
  24. How do I work with the clipboard in an application? See Chapter 18.
  25. How do I print text information from an application? See Chapter 18.
  26. How do I add context sensitive help to my applications? See Chapter 19.
  27. What is the help compiler and how do I use it? See C applications? See Chapter 20.
  28. How do I create Property sheets (tabbed dialogs) in my applications? See Chapter 20.

Part 4

  1. How do DDX and DDV really work behind the scenes? See Chapter 22.
  2. How do I integrate all of the different types of controls and use DDX to access them? See Chapter 22.
  3. Is there a way to create new DDX functions for different data types? See Chapter 22.
  4. How does MFC really work? What is happening inside of MFC? How does a C++ program using MFC compare to a C program? See Chapter 23.
  5. How does MFC handle window handles? See Chapter 23.
  6. Where is the window procedure in an MFC program? See Chapter 23.
  7. How does subclassing work with Windows controls? See Chapter 23.
  8. How can I take an existing control, like the CEdit control, and enhance its behavior without completely rewriting it? How do I integrate a new control like this into a dialog? See Chapter 24.
  9. How do I create list boxes and combo boxes that contain icons, bitmaps or other graphical elements? See Chapter 26.
  10. How do I handle owner-drawn controls in MFC? See Chapter 26.
  11. How do I enumerate fonts and other resources under Windows? See Chapter 26.
  12. How can I add a splash screen to my applications? See Chapter 27.
  13. How do I add expanding dialogs to my applications? See Chapter 28.
  14. How do I stretch a bitmap over an area? See Chapter 29.
  15. How can I draw onto a CStatic control? See Chapter 29.
  16. How do I add a bitmap or a drawing to the background of a dialog or a window? See Chapter 29.
  17. How do I create my own floating pale Manager? See Chapter 32.
  18. How do I make an application float so that it is "always on top." See Chapter 32.
  19. How do I start an application in a minimized or maximized state? See Chapter 32.
  20. How do I create a modelss dialog box? See Chapter 32.
  21. How do I create a mini-frame window? See Chapter 32.
  22. How do I create a popup menu activated by the right mouse button? See Chapter 32.
  23. How do I customize the system menu? See Chapter 32.

Part 5

  1. How to I access SQL databases from an MFC program? Chapter 33.
  2. What is a relational database? What is SQL? See Chapter 33.
  3. What is ODBC? How do I create ODBC data sources? See Chapter 33.
  4. What is the CRecordset class? How do I access databases with it? See Chapter 33.
  5. How do I retrieve records from a database? How do I add and delete records? See Chapter 33.
  6. What is OLE? How can I use it in my applications? See Chapter 34.
  7. What features does OLE support? See Chapter 34.
  8. What is the registry? What is a class ID? See Chapter 34.
  9. How do I create OLE servers and containers with MFC? See Chapter 34.
  10. How do I create an OLE automation server? How do I access an automation server from a Visual Basic or Visual C++? See Chapter 34.
  11. What is an OCX? How do I create an OLE control? See Chapter 34.
  12. What is a thread? How can I use threads to improve applications? See Chapter 35.
  13. What is the difference between worker and user-interface threads? See Chapter 35.
  14. How do thread priorities work? What are they? See Chapter 35.
  15. What is C++? How do I move from C to C++? Se browser? See Appendix B.
  16. What is OpenGL and how do I use it to create realistic graphical images? See Appendix D.

Table of Contents

Preface xiii
Version Free xiii
Audience xiv
Organization xiv
The CD-ROM and the On-line Index xv
Contacting the Authors: Questions, Comments, and Version Changes xv
Acknowledgments xvi
Getting Your Bearings 1(10)
What is Visual C++?
2(1)
Available Documentation
3(1)
7. Road Map
4(1)
Common Questions
5(6)
Part 1 Visual C++ and MFC Basics 11(72)
Introduction
13(8)
What is the Microsoft Foundation Class Library?
13(1)
Windows Vocabulary
14(2)
Event-driven Software and Vocabulary
16(3)
An Example
19(1)
Conclusion
20(1)
Understanding an MFC Program
21(12)
An Introduction to MFC
21(2)
Designing a Program
23(1)
Understanding the Code for "Hello World"
23(7)
Completing the Program
30(2)
MFC Application Structure
32(1)
Conclusion
32(1)
Customizing Controls
33(16)
The Basics
33(3)
CStatic Styles
36(1)
CStatic Text Appearance
37(7)
Rectangular Display Modes for CStatic
44(3)
Fonts
47(1)
Conclusion
48(1)
Handling Events
49(18)
Understanding Message Maps
49(1)
The CButton Class
50(2)
Creating a Message Map
52(2)
Sizing Messages
54(3)
Window Messages
57(3)
Scroll Bar Controls
60(4)
Understanding Message Maps
64(1)
Conclusion
64(3)
Simple Applications
67(16)
Designing an Application
67(2)
Implementing the Fahrenheit to Celsius Converter
69(5)
The CEdit Control
74(3)
An Interest Calculator
77(4)
Conclusion
81(2)
Part 2 Visual C++ and MFC Details 83(240)
Resources, Dialogs, and Menus
85(26)
Resources and Resource Files
85(3)
The Icon Resource
88(2)
Creating a Resource File
90(2)
Menus
92(4)
Responding to Menus
96(4)
Dialog Resources
100(6)
String Resources
106(3)
Conclusion
109(2)
Canned Dialogs
111(22)
The Message Box Dialog
111(4)
The File Open/Save Dialog
115(5)
The Font Dialog
120(3)
The Color Dialog
123(1)
The Print Dialog
124(3)
The Find/Replace Dialog
127(5)
Conclusion
132(1)
Edit Controls and Editors
133(30)
Using the CEdit Control in Single-Line Mode
133(3)
Using the CEdit Control in Multi-Line Mode
136(3)
Designing a Simple Text Editor
139(1)
Creating the Editor Application
140(4)
Stubbing in the Menu Handlers
144(6)
Implementing the Editor
150(11)
Conclusion
161(2)
Lists
163(16)
Creating a List Box
163(4)
Alternate Display Formats
167(3)
Getting User Selections
170(4)
Manipulating Items in a List
174(1)
Combo Boxes
175(2)
Conclusion
177(2)
The CWinApp class
179(16)
Member Variables
179(2)
Icons and Cursors
181(4)
Handling Idle Time
185(1)
Application Functionality
186(4)
Initialization Features
190(2)
Miscellaneous Features
192(1)
Conclusion
192(3)
Drawing
195(68)
Introduction to the GDI Library
195(1)
GDI Basics
196(2)
Device Contexts
198(2)
Simple Drawing
200(22)
Using the Mouse with Your Drawings
222(27)
Advanced Drawing Concepts
249(12)
Conclusion
261(2)
Utility Classes
263(36)
Utility Classes
264(14)
Simple Array Classes
278(4)
The CObject class and CObject Arrays
282(9)
List Classes
291(4)
Mapping Classes
295(3)
Conclusion
298(1)
Debugging and Robustness
299(24)
Setting Up
299(1)
Assertions
300(5)
Tracing
305(2)
Dumping
307(2)
Memory State
309(4)
Exceptions
313(8)
Other Debugging Features
321(1)
Conclusion
321(2)
Part 3 Using the Visual C++ Wizards and Tools to Create Applications 323(158)
Understanding the AppWizard and ClassWizard
325(12)
The Goal of the AppWizard
325(2)
Creating a Simple Framework with the AppWizard
327(1)
The AppWizard's Document-Centric Approach
327(2)
Understanding the AppWizard's Files
329(4)
Understanding the ClassWizard
333(2)
Conclusion
335(2)
Creating a Drawing Program
337(54)
The Goal of the Application
337(1)
Creating a Drawing Program
338(9)
Understanding the Drawing Program
347(5)
Creating an MDI Application
352(4)
Scrolling
356(6)
Splitter Windows
362(9)
Adding New Menu Options and Dialogs.
371(9)
Printing
380(8)
Conclusion
388(3)
Creating an Editor with CEditView
391(10)
Creating an MDI Text Editor
391(1)
Understanding the Editor
392(2)
Combining Two Documents and Views in a Single Application
394(3)
Fixing a Subtle Problem
397(1)
Handling Multiple Views on One Document
398(1)
Conclusion
399(2)
Creating a Fahrenheit-to-Celsius Converter
401(12)
Creating the Converter
401(3)
Understanding the Program
404(1)
Using DDX
405(1)
Using the Document Class
406(4)
Using Form Views
410(1)
Conclusion
411(2)
Creating an Address List Application
413(40)
Creating the Application
413(11)
Understanding the Address List Program
424(1)
Understanding DDX and DDV
425(2)
Improving the Application
427(18)
Printing
445(5)
Conclusion
450(3)
Context-Sensitive Help
453(12)
Understanding the AppWizard's Help Framework
453(1)
Understanding and Modifying the Help Files
454(6)
Context-Sensitive Help
460(3)
Aliases
463(1)
Conclusion
463(2)
Common Controls
465(10)
A Simple Example Using the Spin Button, List, and Tree Controls
465(1)
CSpinButtonCtrl
466(1)
CListCtrl
466(3)
CTreeCtrl
469(1)
Property Sheets
470(1)
A Property Sheet Example
470(1)
The CPropertySheet Class
471(2)
Conclusion
473(2)
Creating Explorers
475(6)
Creating the basic framework
476(4)
Conclusion
480(1)
Part 4 Advanced Features 481(96)
Dialog Data Exchange and Validation
483(12)
Understanding DDX
484(1)
Exchange Routines
485(1)
Transfer Direction
486(1)
Understanding DDV
486(1)
An Example
487(6)
Custom Routines
493(1)
Conclusion
494(1)
Understanding MFC
495(10)
What Are Window Handles?
495(2)
The Life of Windows and Objects
497(1)
Initializing Dialogs
498(1)
From HWND to CWnd
499(2)
Permanent and Temporary Associations
501(1)
Handles to Other Objects
501(1)
How Messages Work
501(2)
Subclassing
503(1)
Conclusion
504(1)
Enhancing The Edit Control
505(4)
An Example
505(1)
Understanding the Process
506(1)
Conclusion
507(2)
Self-Managing Controls
509(14)
Owner-Drawing vs. Self-Drawing
510(1)
Owner-Drawn Messages
510(1)
The Self-Drawing Framework
510(1)
Behind the Scenes
511(1)
A General Solution
511(2)
A Self-Drawing Combo Box
513(4)
Drawing Transparent Bitmaps
517(3)
Subclassing the Combo Box
520(1)
Conclusion
521(2)
Another Look---A Self-Drawing List Box
523(10)
Introduction to Font Enumeration
523(1)
Enumerating Font Families
524(3)
Enumerating Font Styles
527(1)
An Example
528(4)
Conclusion
532(1)
Creating A Splash Screen
533(6)
An Example
533(4)
Conclusion
537(2)
Expanding Dialogs
539(8)
The CExpandingDialog Class
539(6)
An Example
545(1)
Conclusion
546(1)
Drawing and Controls
547(8)
Drawing in CStatic Controls
547(3)
Drawing in Dialogs
550(2)
Dialog Controls and the Background
552(1)
Conclusion
552(3)
Dialog Bars
555(6)
An Example
556(2)
Data Exchange
558(1)
Conclusion
559(2)
Dialog and View Idle Command Updating
561(6)
How Idle Updates Work
561(1)
Idle Updating in Views
562(1)
An Example
563(1)
Idle Updating in Dialogs
563(2)
An Example
565(1)
Conclusion
566(1)
Odds and Ends
567(10)
Accepting Files from the File Manager
567(1)
Making an Application the Topmost Window
568(1)
Starting an Application Minimized
569(1)
Modeless Dialog Boxes
569(2)
Mini-Frame Windows
571(3)
Context Popup Menus
574(2)
Modifying the System Menu
576(1)
Conclusion
576(1)
Part 5 Advanced MFC Classes 577(102)
Database Access
579(30)
Understanding Relational Databases
579(3)
Understanding SQL
582(3)
Understanding ODBC
585(1)
Microsoft Query
586(5)
The CRecordSet Class
591(2)
Simple CRecordSet Operations
593(9)
Using the CRecordView Class
602(3)
Adding and Deleting Records
605(2)
Conclusion
607(2)
OLE
609(42)
Understanding OLE
609(5)
An Example
614(3)
OLE as a Vision of the Future
617(1)
Standard OLE Features
618(3)
An Introduction to OLE Containers
621(8)
An Introduction to OLE Servers
629(7)
An Introduction to OLE Automation
636(4)
An Introduction to OLE Controls
640(10)
Conclusion
650(1)
MFC Threads
651(28)
Understanding the Possibilities
651(1)
Understanding Threads
652(3)
MFC Worker Threads
655(2)
Thread Termination
657(3)
Passing Parameters to Threads
660(1)
Suspending and Resuming Threads
661(1)
Thread Priorities
662(5)
Subclassing CWin Thread
667(8)
User Interface Threads
675(2)
Conclusion
677(2)
A Understanding C++: An Accelerated Introduction 679(60)
B Using the Visual C++ Compiler and Tools 739(42)
B.1 Compiling and Executing a Console Program with Visual C++
739(5)
B.2 Debugging
744(3)
B.3 Compiling MFC Programs
747(5)
B.4 The Browser
752(4)
B.5 Resources and resource files
756(5)
B.6 App Wizard Files
761(8)
B.7 Using the Class Wizard
769(6)
B.8 OLE Controls
775(6)
B.9 Conclusion
781(1)
C Contacting the Authors 781(2)
D Using OpenGL with MFC 783(18)
D.1 Writing an OpenGL Program
785(6)
D.2 Simple 2-D Graphics
791(2)
D.3 Transformations and the Matrix Stack
793(3)
D.4 Mouse Action
796(1)
D.5 Double Buffering
797(1)
D.6 A Three Dimensional Cube
798(3)
D.7 Z-Buffering
801(1)
D.8 Conclusion
802
Index 801

Preface

Preface:

Getting Your Bearings

You are probably opening this book because you are new to Windows Programming or because you are new to MFC (Microsoft Foundation Classes) or the Visual C++ programming environment. For example, you might be an experienced UNIX or Macintosh programmer. Or perhaps you have a lot of talent with C programming and command-driven user interfaces on PCs and want to move over to Windows. You may be experienced with Windows programming in C, but have never before used MFC and C++ to develop Windows applications. Regardless of your origin, you will find that as you try to make your transition you are hampered by two problems. The purpose of this book is to quickly solve those problems so that you can begin creating your own professional applications with Visual C++ as quickly as possible.

The first problem is mental: you have to get past the wall that surrounds Visual C++. That wall arises because of the obvious complexity of the Windows and C++ programming environments. When you load Visual C++ from its CD, you notice that there are tens of thousands of pages of documentation, hundreds of sample programs, and untold megabytes of help files. No one has the time to sort through all of this material, but you know that hidden in those megabytes are hundreds of important concepts that you need to master.

The second problem is more pedestrian: you have to pick a place to start. But where should you begin? How do you write a simple Windows application? How do you learn how to write an advanced one?

This book is designed to help you move into the Visual C++ environment rapidly and confidently. The purpose ofthischapter is to help you get your bearings in this new environment. It will introduce you to Visual C++ and then give you a starting point and a direction so that you can become an accomplished Windows programmer very quickly using the most modern tools and techniques available.

What is Visual C++?

The Visual C++ environment is huge and can be extremely intimidating initially. Visual C++ combines a complete suite of powerful tools into a single application development environment, and the first time you face all of these tools it can be very difficult to discern what they all do or how to use them. When you look at the book reader application that comes with the Visual C++ CD-ROM, you face another hurdle: You find thousands and thousands of pages in many different books. The thought of wading through all of these manuals can be daunting.

So letÕs start at the beginning and look at Visual C++ in an organized way. First of all, what is it? Here is a brief summary:

  • Visual C++ is a C++ compiler
  • Visual C++ is a debugging environment
  • Visual C++ is an application framework generator
  • Visual C++ is a project manager
  • Visual C++ is an easy way to design and implement menus, dialogs, and other "resources"
  • Visual C++ is a programmer acceleratorÑseveral tools inside Visual C++ are designed to make you more efficient by making your life as a programmer easier or by reducing the code you must write

In other words, Visual C++ is a complete and extremely powerful application development environment. In order to take full advantage of this environment, you have to become comfortable with all the too to know how they can work together to accelerate your software development cycle.

In its most basic form, Visual C++ is simply a C++ compiler. You can use it to create simple text programs in C or C++. If you would like to try this out, go to Appendix B.1 and work through the example there. You will find that it is extremely easy to write, compile, and debug simple text programs using Visual C++.

Most people who purchase Visual C++ do not want to create text programs, however. They want to create advanced Windows applications that make effective use of the Windows 98 and Windows NT user interface. To do this, you must know C++, and you must understand the MFC hierarchy. MFC is designed make you as productive as possible by encapsulating common Windows code in classes that are already written, tested, and debugged. Once you invest the time to learn MFC, you are greatly rewarded in increased speed, flexibility and robustness.

Part 1 of this book gives you a thorough introduction to MFC. It shows you the basic principles used in every MFC program you write. Part 2 gives a complete overview of all the controls and features that MFC offers. Part 2 contains hundreds of examples that make it easy to understand the different MFC classes.

Once you feel comfortable with MFC, you are ready to begin creating professional Windows applications. Part 3 introduces the AppWizard, the ClassWizard, and the resource editing tools of Visual C++. The AppWizard is your starting point when creating any full-blown Windows application: It helps you by generating a complete file framework that organizes the entire application around a consistent core of MFC classes. The ClassWizard, in combination with the resource editing features that the Visual C++ environment provides, then makes it easy to add to and complete your application by helping you design, create, and install menus, dialog boxes, and other application resources. The ClassWizard also helps you add the code that lets your application respond to user input properly. Using these three toolsÑthe AppWizard, the ClassWizard, and the resource editorsÑtogether with the MFC class hierarchy, it is extremely easy to complete professional applications very quickly. Part 3 contains four different example applications to help demonstrate the process.

Part 4 continues by demonstrating advanced features. It shows you how to use a variety of techniques to create such things as expanding dialogs, property sheets, dialog bars, splash screens, self-drawn controls and bitmapped backgrounds. These techniques add significant utility to your applications when used appropriately. Finally, Part 5 concludes the book by discussing advanced MFC classes for database connectivity, OLE features, and so on.

Available Documentation

The Visual C++ CD-ROM contains over 100 megabytes of on-line documentation covering various aspects of Windows, MFC, and the tools available in Visual C++. It contains many more megabytes of sample code. The MFC class hierarchy contains hundreds of different classes holding thousands of member functions. The Win32 API contains thousands of functions as well. All of this material is documented in on-line help files. Obviously, there is no lack of documentation with this product.

This book, therefore, makes no attempt to replace the documentation. Its goal is to help you wind your way through the Visual C++ forest and find what you need. Using the base you gain from reading this book, you will be able to approach Visual C++ and begin using it in productive ways very quickly.

There are currently seven different types of documentation provided by Microsoft for Visual C++ and MFC:

  1. On-line Books Ð A series of manuals on the CD-ROM that act as the documentation for the system. The collection of books is available from the Contents section of the Help menu. Look at the titles of all the different books and articles available. You will find that there are many. The books cover Visual Studio, the tools within Visual Studio like the compiler, debugger, etc., the many function libraries available, etc
  2. Sample Code Ð The Visual C++ directory on your hard disk may contain a sample directory that contains source code demonstrating a wide variety of techniques. Some of the samples are written in C, while other samples use MFC and C++.
  3. Developer Network CD Ð MicrosoftÕs DeveloperÕs Network CD provides quite a bit of additional sample code, along with books and files containing a variety of valuable information. You receive this CD when you become a member of the Microsoft DeveloperÕs Network.
  4. MicrosoftÕs Web Site - the Microsoft Web site contains knowledge bases and articles that may be helpful.
  5. Internet News Groups - Numerous newsgroups and mailing lists on the Internet bring C++/MFC developers together to share tips and techniques. There are also other web sites available full of sample code and tips.

Using all of these different forms of documen find anything you need to know. The key is understanding where and how to look for what you need. This book will help accelerate that process tremendously.

Road Map

The tools in Visual C++ require a great deal of prior knowledge if you want to use them effectively. For example, when you open the Visual C++ package and load the CD, you may have the impression that you can use the AppWizard to generate any program you like. Unfortunately, the code that the AppWizard generates is virtually worthless unless you know a good bit about MFC already. That is why this book is structured the way it is. The progression presented in this book is exactly the progression you will need to follow if you do not already know MFC. However, different people come into Visual C++ with varying levels of experience and different goals. Here is a road map to guide you through the material so that you can find the best starting point for your particular situation:

  • If you do not know C++, you will need to learn it. Proceed to the accelerated introduction to C++ in Appendix A of this book.
  • If you want to simply try out Visual C++ and compile some simple programs, proceed to Appendix B. It will show you how the compiler works and how to compile and debug simple applications.
  • If you know C++ but have never done any Windows programming of any kind, proceed to Part 1. It will teach you the fundamentals of event-driven programming and then quickly introduce you to MFC programming.
  • If you have experience with Windows programming in C but have never done Windows programming using C++ and MFC, proceed to Part 1. It will quickly introduce you to the MFC class hi programming.
  • If you have used MFC before (for example, if you are familiar with MFC version 1.0) but are unfamiliar with the new application development tools like the AppWizard and the ClassWizard, skim Part 2 and then proceed to Part 3 for a complete introduction to the tools.
  • If you are familiar with Visual C++ and MFC but want to learn about a variety of techniques that can make your applications look more professional, turn to Part 4. It will show you how to create things like splash screens, expanding dialogs, property sheets, and self-drawn controls.
  • If you are a corporate programmer who needs to attach to a client/server database, pay particular attention to Chapter 33 in Part 5.

Common Questions

The goal of this section is to show you how to find answers to the most common questions about Visual C++ and MFC. You may wish to scan this list now and periodically in the future to quickly find answers to your questions.

Part 1

  1. What is MFC? Why does it exist? See Chapter 1
  2. How do I compile and run a simple MFC program? See Appendix B.3 and Chapter 1.
  3. How do I create a simple "Hello World!" program in MFC? What does the code actually mean? See Chapter 2.
  4. I have found the AppWizard, but when I run it I find it generates 15 files that make absolutely no sense to me. What do I do? See the discussion at the beginning of Part 3 of this book.
  5. How do I create a simple MFC control? See Chapter 3.
  6. How do I customize MFC controls and change their styles? See Chapter 3.
  7. How do I create a push button and respond to its events in MFC? See Chapter 4.
  8. What is a message ma scroll bar and respond to its events? See Chapter 4.
  9. How do I create an edit control and respond to its events? See Chapter 5 and Chapter 8.
  10. How do I create simple applications? See Chapter 5.
  11. How do I make a simple application appropriately handle tab keys, accelerators, etc.? See Chapter 5.

Part 2

  1. What is a resource? What is a resource file? What are the advantages of resources? See Chapter 6.
  2. How do I create and use icon, dialog, menu, string table, and accelerator resources? See Chapter 6.
  3. How do I create a message dialog? A File Open dialog? A Font dialog? A Color dialog? A Print dialog? A Find/Replace dialog? See Chapter 7.
  4. What is the difference between modal and modeless dialogs? See Chapter 7.
  5. How do I use an edit control in single and multi-line modes? See Chapter 8.
  6. How do I create a simple text editor? See Chapter 8.
  7. How do I create and use lists, drop down lists, and combo boxes in my applications? See Chapter 9 and Chapter 20.
  8. How do I make multi-column and tabbed lists? See Chapter 9.
  9. How do I load and display system and custom icons? See Chapters 6 and 10.
  10. How do I change the application cursor? See Chapter 10 and 11.5.3.
  11. How do I display a watch cursor? See Chapter 10.
  12. How do I perform background processing while the application is idle? See Chapter 10.
  13. What is a document template? See Chapter 10 and Chapter 16.
  14. How do I create an MRU file list? See Chapter 10.
  15. How do I use INI files with my applications? See Chapter 10.
  16. How do I draw lines, rectangles, circles, et to an application? See Chapter 11.
  17. How do I respond to mouse clicks in a drawing? See Chapter 11.
  18. How do I create rubber-banded lines, rectangles, etc. in a drawing?
  19. How do I create a drawing space larger than the current window? See Chapters 11 and 15.
  20. How do I create animated drawings? See Chapter 11.
  21. How do I work with text and binary files in MFC? See Chapter 12.
  22. How do I work with strings in MFC? See Chapter 12.
  23. How do I work with time values in MFC? See Chapter 12.
  24. Is there an easy way to create arrays, lists and hash tables in MFC? See Chapter 12.
  25. What debugging facilities are built into MFC? How do I make use of the MFC exception handling mechanisms? See Chapter 13.
  26. How do I use TRACE and ASSERT statements? See Chapter 13.
  27. How do I prevent and detect memory leaks in my applications? See Chapter 13.

Part 3

  1. Are there any simple applications in this book showing me how to use the AppWizard and ClassWizard? See the drawing example, the editor example, the form example and the address list example in Part 3 of this book.
  2. What is the AppWizard? What is the ClassWizard? How do I use them to speed up application development? See Chapter 14.
  3. How do I create a simple framework with the AppWizard? See Chapter 14.
  4. What do all of the files generated by the AppWizard do? See Chapter 14.
  5. What is the document/view paradigm? See Chapters 14, 15 and 18.
  6. What do the STDAFX files do? See Chapter 14.
  7. Can you give me a simple example of the AppWizard and ClassWizard in action? See Chapter 14.
  8. How paradigm? See Chapter 15.
  9. What is the difference between an SDI and an MDI application? See Chapter 15.
  10. How do I understand what is going on inside the AppWizard framework? See Section 15.3 and Chapter 21.
  11. How do I add new menus and menu options to an application? See Chapter 15.
  12. How do I add scrolling to a drawing application? How do I use splitter windows? See Chapter 15.
  13. How do I add a new dialog to an AppWizard framework? How do I use DDX and DDV? See Chapters 15 and 18.
  14. How do I add a dialog class with the ClassWizard? See Chapters 15 and 18.
  15. How do I add printing to an application? What do the MFC printing functions do? How do I handle multi-page printing? See Chapters 15 and 18.
  16. How do I create a text editor with the AppWizard? See Chapter 16.
  17. How do I handle multiple document types in a single MDI application? See Chapter 16.
  18. What is a document template? See Chapter 16.
  19. How do I use form views? How do I put controls on the face of an application? See Chapter 17.
  20. Can you give me an example that combines all of these different concepts in a single application? See Chapter 18.
  21. How do I create a resizable tabbed list in a form view? See Chapter 18.
  22. How do I enable and disable menu options? See Chapters 18 and 6.
  23. How do I customize the tool bar and status bar? See Chapter 18.
  24. How do I work with the clipboard in an application? See Chapter 18.
  25. How do I print text information from an application? See Chapter 18.
  26. How do I add context sensitive help to my applications? See Chapter 19.
  27. What is the help compiler and how do I use it? See C applications? See Chapter 20.
  28. How do I create Property sheets (tabbed dialogs) in my applications? See Chapter 20.

Part 4

  1. How do DDX and DDV really work behind the scenes? See Chapter 22.
  2. How do I integrate all of the different types of controls and use DDX to access them? See Chapter 22.
  3. Is there a way to create new DDX functions for different data types? See Chapter 22.
  4. How does MFC really work? What is happening inside of MFC? How does a C++ program using MFC compare to a C program? See Chapter 23.
  5. How does MFC handle window handles? See Chapter 23.
  6. Where is the window procedure in an MFC program? See Chapter 23.
  7. How does subclassing work with Windows controls? See Chapter 23.
  8. How can I take an existing control, like the CEdit control, and enhance its behavior without completely rewriting it? How do I integrate a new control like this into a dialog? See Chapter 24.
  9. How do I create list boxes and combo boxes that contain icons, bitmaps or other graphical elements? See Chapter 26.
  10. How do I handle owner-drawn controls in MFC? See Chapter 26.
  11. How do I enumerate fonts and other resources under Windows? See Chapter 26.
  12. How can I add a splash screen to my applications? See Chapter 27.
  13. How do I add expanding dialogs to my applications? See Chapter 28.
  14. How do I stretch a bitmap over an area? See Chapter 29.
  15. How can I draw onto a CStatic control? See Chapter 29.
  16. How do I add a bitmap or a drawing to the background of a dialog or a window? See Chapter 29.
  17. How do I create my own floating pale Manager? See Chapter 32.
  18. How do I make an application float so that it is "always on top." See Chapter 32.
  19. How do I start an application in a minimized or maximized state? See Chapter 32.
  20. How do I create a modelss dialog box? See Chapter 32.
  21. How do I create a mini-frame window? See Chapter 32.
  22. How do I create a popup menu activated by the right mouse button? See Chapter 32.
  23. How do I customize the system menu? See Chapter 32.

Part 5

  1. How to I access SQL databases from an MFC program? Chapter 33.
  2. What is a relational database? What is SQL? See Chapter 33.
  3. What is ODBC? How do I create ODBC data sources? See Chapter 33.
  4. What is the CRecordset class? How do I access databases with it? See Chapter 33.
  5. How do I retrieve records from a database? How do I add and delete records? See Chapter 33.
  6. What is OLE? How can I use it in my applications? See Chapter 34.
  7. What features does OLE support? See Chapter 34.
  8. What is the registry? What is a class ID? See Chapter 34.
  9. How do I create OLE servers and containers with MFC? See Chapter 34.
  10. How do I create an OLE automation server? How do I access an automation server from a Visual Basic or Visual C++? See Chapter 34.
  11. What is an OCX? How do I create an OLE control? See Chapter 34.
  12. What is a thread? How can I use threads to improve applications? See Chapter 35.
  13. What is the difference between worker and user-interface threads? See Chapter 35.
  14. How do thread priorities work? What are they? See Chapter 35.
  15. What is C++? How do I move from C to C++? Se browser? See Appendix B.
  16. What is OpenGL and how do I use it to create realistic graphical images? See Appendix D.

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews