visual cpp and mfc programming
TRANSCRIPT
-
8/8/2019 Visual Cpp and MFC Programming
1/737
Visual C++ and MFC Fundamentals Table of Contents
FunctionX, Inc. 1
an action creates a document and this document must reside somewhere.
Visual C++ and MFC
Programming
-
8/8/2019 Visual Cpp and MFC Programming
2/737
Table of Contents Visual C++ and MFC Fundamentals
2 FunctionX, Inc.
-
8/8/2019 Visual Cpp and MFC Programming
3/737
Visual C++ and MFC Fundamentals Table of Contents
FunctionX, Inc. 3
Table of Contents
TABLE OF FIGURES ...............................................................................................13
CHAPTER 1: INTRODUCTION TO MICROSOFT VISUAL C++...........15
1.1 The Microsoft Visual C++ Environment.....................................................161.1.1 Introduction..............................................................................................161.1.2 The Integrated Development Environment ........................................171.1.3 The Tit le Bar............................................................................................171.1.4 The Main Menu.......................................................................................181.1.5 The Toolbars ............................................................................................211.1.6 The Studio Windows..............................................................................23
1.2 Floatable and Dockable Windows ................................................................261.2.1 Description ...............................................................................................26
1.3 Visual C++ Projects and Files .......................................................................271.3.1 Creating a New Project ..........................................................................271.3.2 Creating Files ...........................................................................................281.3.3 Adding Existing Files to a Project .......................................................291.3.4 Adding Classes ........................................................................................301.3.5 Opening Files ...........................................................................................311.3.6 Opening Existing Projects .....................................................................33
1.4 Getting Help .....................................................................................................341.4.1 Online Help ..............................................................................................341.4.2 Other Help Types ....................................................................................35
CHAPTER 2: INTRODUCTION TO MFC .......................................................37
2.1 The Microsoft Foundation Class Library ....................................................38
2.1.1 Introduction..............................................................................................382.1.2 CObject, the Ancestor............................................................................382.1.3 The Basic Application............................................................................39
2.2 Frames Fundamentals .....................................................................................422.2.1 Introduction..............................................................................................432.2.2 Reference to the Main Window............................................................442.2.3 Introduction to Macros...........................................................................472.2.4 Windows Styles.......................................................................................482.2.5 Windows Location..................................................................................512.2.6 Windows Size ..........................................................................................532.2.7 Windows Dimensions............................................................................542.2.8 Windows Parents ....................................................................................57
2.3 Message Boxes.................................................................................................58
2.3.1 Definition .................................................................................................582.3.2 Message Box Creation ...........................................................................592.3.3 Message Box Implementation ..............................................................592.3.4 The Box Message..................................................................................602.3.5 The Message Title .................................................................................622.3.6 Message Box Options ............................................................................632.3.7 The Messages Return Value................................................................66
CHAPTER 3: WINDOWS RESOURCES ..........................................................69
-
8/8/2019 Visual Cpp and MFC Programming
4/737
Table of Contents Visual C++ and MFC Fundamentals
4 FunctionX, Inc.
3.1 Introduction to Resources ..............................................................................703.1.1 Introduction..............................................................................................703.1.2 Converting a Resource Identifier .........................................................71
3.2 Icons...................................................................................................................733.2.1 Icons Overview .......................................................................................733.2.2 Icons Design ............................................................................................74
3.3
Menu Fundamentals ........................................................................................803.3.1 Overview..................................................................................................80
3.3.2 The Main Menu.......................................................................................803.3.3 Main Menu Design.................................................................................81
3.4 Toolbars.............................................................................................................853.4.1 Overview..................................................................................................853.4.2 Creating a Toolbar..................................................................................85
3.5 Accelerators ......................................................................................................853.5.1 Access Keys.............................................................................................863.5.2 Shortcuts ...................................................................................................863.5.3 Accelerator Table ....................................................................................87
3.6 Version Information ........................................................................................883.6.1 Overview..................................................................................................883.6.2 The Version Information Editor...........................................................88
3.7 Cursors ..............................................................................................................893.7.1 Overview..................................................................................................893.7.2 Creating and Using Cursors ..................................................................90
3.8 The String Table ..............................................................................................923.8.1 Description ...............................................................................................923.8.2 Creating and Using a String Table .......................................................92
3.9 Other Techniques of Creating Windows .....................................................943.9.1 Window Registration and Standard Resources..................................943.9.2 Window Registration and Custom Resources....................................953.9.3 Frame Loading ........................................................................................97
CHAPTER 4: MESSAGES AND EVENTS ......................................................101
4.1
Introduction to Messages ............................................................................1024.1.1 Overview...............................................................................................102
4.1.2 A Map of Messages .............................................................................1024.2 Windows Messages ......................................................................................104
4.2.1 Window Creation .................................................................................1054.2.2 Window's Showing State ....................................................................1064.2.3 Window Activation .............................................................................1084.2.4 Window Painting .................................................................................1124.2.5 Window Sizing.....................................................................................1144.2.6 Window Moving ..................................................................................1164.2.7 Window Destruction ...........................................................................119
4.3 Command Messages ....................................................................................1194.3.1 Definition ..............................................................................................1204.3.2 Creating a Command Message..........................................................120
4.4 Keyboard Messages .....................................................................................1204.4.1 Introduction........................................................................................... 1204.4.2 The Key Down Effect ......................................................................... 1224.4.3 The Key Up Effect ...............................................................................124
4.5 Mouse Messages...........................................................................................1244.5.1 Introduction........................................................................................... 1244.5.2 Mouse-Down Messages...................................................................... 1244.5.3 The Mouse-Up Messages ...................................................................1274.5.4 The Double-Click Message................................................................129
-
8/8/2019 Visual Cpp and MFC Programming
5/737
Visual C++ and MFC Fundamentals Table of Contents
FunctionX, Inc. 5
4.5.5 Mouse Moving.....................................................................................1304.6 Anytime Messages ....................................................................................... 130
4.6.1 Introduction........................................................................................... 1304.6.2 Sending Messages ................................................................................131
CHAPTER 5: THE DOCUMENT/VIEW ARCHITECTURE....................133
5.1 Overview of t he Document/View Architecture .......................................1345.1.1 Introduction........................................................................................... 1345.1.2 The View...............................................................................................1345.1.3 The Document ......................................................................................1345.1.4 The Frame .............................................................................................1345.1.5 The Document/View Approach......................................................... 135
5.2 The Single Document Interface (SDI) ......................................................1365.2.1 Overview...............................................................................................1365.2.2 Creating a Single Document Interface .............................................137
5.3 SDI Improvements ....................................................................................... 1435.3.1 SDI Improvements: The Application ...............................................1435.3.2 SDI Improvements: The Document..................................................1465.3.3 SDI Improvements: The Frame .........................................................150
5.3.4 SDI Improvements: The View...........................................................1515.4 The Multiple Document Interface (MDI).................................................1515.4.1 Overview...............................................................................................1515.4.2 Creating a Multiple Do cument Interface .........................................1545.4.3 The Visual C++ AppWizard ..............................................................157
5.5 The AppWizard .............................................................................................1575.5.1 An SDI With AppWizard ...................................................................1585.5.2 An MDI With AppWizard ..................................................................161
CHAPTER 6: THE GRAPHICAL DEVICE INTERFACE ........................163
6.1 Introduction to the GDI ...............................................................................1646.1.1 The Device Context .............................................................................1646.1.2 Grabbing the Device Context ............................................................164
6.2 The Process of Drawing ..............................................................................1656.2.1 Getting a Device Context ...................................................................1656.2.2 Starting a Device Context's Shape ....................................................165
6.3 GDI Lines and Shapes .................................................................................1676.3.1 Lines ....................................................................................................... 1676.3.2 Polylines ................................................................................................1706.3.3 Multiple Polylines................................................................................1736.3.4 Polygons................................................................................................1756.3.5 Multiple Polygons................................................................................1766.3.6 Rectangles and Squares ......................................................................1776.3.7 A Rectangle With Edges.....................................................................1796.3.8 Ellipses and Circles .............................................................................1806.3.9 Round Rectangles and Round Squares ............................................183
6.3.10 ..Pies .........................................................................................................1846.3.11 ..Arcs ........................................................................................................1856.3.12 ..The Arc's Direction .............................................................................1876.3.13 ..Angular Arcs ........................................................................................ 1906.3.14 ..Chords ...................................................................................................1916.3.15 ..Bzier Curves ....................................................................................... 192
CHAPTER 7: GDI ACCESSORIES AND TOOLS .......................................197
7.1 Colors .............................................................................................................198
-
8/8/2019 Visual Cpp and MFC Programming
6/737
Table of Contents Visual C++ and MFC Fundamentals
6 FunctionX, Inc.
7.1.1 Overview...............................................................................................1987.1.2 The Color as a Data Type ...................................................................1997.1.3 Color Palettes........................................................................................200
7.2 Drawing With Colors ...................................................................................2017.2.1 Coloring a Pixel....................................................................................2017.2.2 Rectangles With 3-D Effect ...............................................................2017.2.3
Drawing Text ........................................................................................ 202
7.3 Bitmaps...........................................................................................................2057.3.1 Introduction........................................................................................... 2057.3.2 Bitmap Creation ...................................................................................206
7.4 Fonts ...............................................................................................................2097.4.1 Introduction........................................................................................... 2097.4.2 Font Selection ....................................................................................... 2097.4.3 Font Creation ........................................................................................ 2097.4.4 Font Retrieval ....................................................................................... 215
7.5 Pens.................................................................................................................2167.5.1 Introduction........................................................................................... 2167.5.2 The Fundamentals of a Pen ................................................................2167.5.3 Creating and Selecting a Pen .............................................................2167.5.4 Retrieving a Pen ...................................................................................220
7.6 Brushes ...........................................................................................................2207.6.1 Introduction........................................................................................... 2207.6.2 Solid Brushes ........................................................................................ 2217.6.3 Hatched Brushes ..................................................................................2257.6.4 Patterned Brushes ................................................................................2277.6.5 Logical Brushes....................................................................................229
CHAPTER 8: GDI ORIENTATION AND TRANSFORMATIONS ........231
8.1 The Default Coordinate System .................................................................2328.1.1 Introduction........................................................................................... 2328.1.2 Changing the Coordinate System......................................................234
8.2 The Mapping Modes ....................................................................................2388.2.1
Mapping Mode Choices......................................................................238
8.2.2 Unit and Coordinate Systems Options .............................................243
CHAPTER 9: STRINGS .........................................................................................249
9.1 Fundamentals of Strings.............................................................................. 2509.1.1 Null-Terminated Strings .....................................................................2509.1.2 The Standard string Class...................................................................2519.1.3 The Length of a String ........................................................................2529.1.4 String Formatting .................................................................................253
9.2 Operations of Strings...................................................................................2559.2.1 String Copy ...........................................................................................2559.2.2 String Concatenation...........................................................................257
9.3 The Characters of a String .......................................................................... 259
9.3.1 Access to Characters ...........................................................................2599.3.2 Sub-Strings............................................................................................2609.4 The CString Class.........................................................................................260
9.4.1 Introduction........................................................................................... 2609.4.2 String Initialization ..............................................................................2619.4.3 The String and its Length ...................................................................261
9.5 Working with Individual Characters .........................................................2639.5.1 Character Indexing ..............................................................................2639.5.2 Character Insertion ..............................................................................2649.5.3 Finding a Character .............................................................................264
-
8/8/2019 Visual Cpp and MFC Programming
7/737
Visual C++ and MFC Fundamentals Table of Contents
FunctionX, Inc. 7
9.5.4 Character Identification ......................................................................2659.5.5 Removing Characters .......................................................................... 265
9.6 Sub Strings.....................................................................................................2669.6.1 Introduction........................................................................................... 2669.6.2 Finding a Sub String ............................................................................2679.6.3 Character and String Removal........................................................... 2689.6.4
Replacing String Occurrences ...........................................................268
9.6.5 String Formatting.................................................................................2699.7 Operations on Strings...................................................................................270
9.7.1 About Constructing a String ..............................................................2709.7.2 String Assignment ...............................................................................2709.7.3 String Copy ...........................................................................................2719.7.4 Strings and Their Cases ......................................................................271
9.8 String Comparisons......................................................................................2729.8.1 Case Sensitivity....................................................................................2729.8.2 Case Insensitivity.................................................................................273
CHAPTER 10: CHARACTERISTICS OF A WINDOW'S FRAME........277
10.1 Introduction to Win32 Library ..................................................................278
10.1.1 ..Overview............................................................................................... 27810.1.2 ..The Framework....................................................................................27910.1.3 ..A Window's Instance .......................................................................... 28010.1.4 ..The Command Line.............................................................................28110.1.5 ..Frame Display Options.......................................................................28210.1.6 ..Window Class Initialization ..............................................................28210.1.7 ..Window Registration .......................................................................... 291
10.2 Window Creation.........................................................................................29210.2.1 ..The Main Window ..............................................................................29210.2.2 ..The Window Class Name...................................................................29410.2.3 ..The Window Name .............................................................................29610.2.4 ..Windows Styles ...................................................................................29710.2.5 ..Window's Location and Size .............................................................29810.2.6
..Window's Parenting ............................................................................305
10.2.7 ..The Window's Menu...........................................................................30610.2.8 ..Window Display..................................................................................30910.2.9 ..Considering Window's Messages .....................................................310
10.3 The Mini Frame Window...........................................................................31410.3.1 ..Introduction ..........................................................................................31410.3.2 ..Creation of a Miniframe Window.....................................................314
CHAPTER 11: INTRODUCTION TO WINDOWS CONTROLS ............317
11.1 Controls Fundamentals ...............................................................................31811.1.1 ..Introduction ..........................................................................................31811.1.2 ..The Parent-Child Window Relationship..........................................320
11.2 Parent Controls .............................................................................................321
11.2.1 ..Definition ..............................................................................................32111.2.2 ..Parent Windows Styles .......................................................................32111.3 Windows Controls ....................................................................................... 321
11.3.1 ..Introduction ..........................................................................................32211.3.2 ..Control Creation Options ...................................................................32211.3.3 ..The Control's Class Name ..................................................................32511.3.4 ..The Control's Window Name ............................................................ 327
11.4 Controls Styles and Common Properties .................................................32711.4.1 ..Childhood..............................................................................................32711.4.2 ..Visibility................................................................................................328
-
8/8/2019 Visual Cpp and MFC Programming
8/737
Table of Contents Visual C++ and MFC Fundamentals
8 FunctionX, Inc.
11.4.3 ..Availability ...........................................................................................32911.4.4 ..Borders ..................................................................................................33011.4.5 ..Tab Sequence....................................................................................... 331
11.5 Extended Styles............................................................................................33211.5.1 ..Introduction ..........................................................................................33211.5.2 ..Left Text Alignment............................................................................33311.5.3
..Right Text Alignment ......................................................................... 333
11.5.4 ..Extended Borders ................................................................................33411.5.5 ..Controls Location and Dimensions ..................................................33611.5.6 ..Parenthood............................................................................................33911.5.7 ..Control Identification..........................................................................339
CHAPTER 12: DIALOG-BASED WINDOWS ...............................................343
12.1 Dialog Boxes ................................................................................................34412.1.1 ..Overview............................................................................................... 34412.1.2 ..Dialog Box Creation ...........................................................................34612.1.3 ..Dialog Box Location ...........................................................................34712.1.4 ..Dialog Box Dimensions .....................................................................34812.1.5 ..Windows Styles for a Dialog Box....................................................350
12.1.6 ..Dialog Box Styles................................................................................35512.1.7 ..Extended Windows Styles for a Dialog Box..................................35612.1.8 ..Creating the Dialog Resource File ....................................................35912.1.9 ..Creating a Class for the Dialog .........................................................36012.1.10 Dialog Box Methods...........................................................................361
12.2 Modal Dialog Boxes ...................................................................................36212.2.1 ..Dialog-Based Applications................................................................36212.2.2 ..The MFC Wizard for a Dialog-Based Application........................36412.2.3 ..A Modal Dialog Box in an Application...........................................366
12.3 Property Sheets and Wizards.....................................................................36712.3.1 ..Introduction to Property Pages ..........................................................36712.3.2 ..Creating Property Pages .....................................................................36812.3.3 ..Property Sheets ....................................................................................37412.3.4
..Wizard Pages ........................................................................................ 378
CHAPTER 13: CONTROL DESIGN ..................................................................381
13.1 Forms .............................................................................................................38213.1.1 ..Introduction ..........................................................................................38213.1.2 ..Form Creation ......................................................................................382
13.2 Dialog Box Messages and Events .............................................................38413.2.1 ..The Dialog Box as a Control Initializer...........................................38413.2.2 ..Other Dialog-Based Windows Messages ........................................38813.2.3 ..Control-Related Messages .................................................................389
13.3 Floating Windows ....................................................................................... 38913.3.1 ..Introduction ..........................................................................................39013.3.2 ..The Modeless Dialog Box..................................................................390
13.3.3 ..Modeless Property Sheets ..................................................................39513.4 Control Design .............................................................................................39913.4.1 ..Controls Selection and Addition.......................................................39913.4.2 ..Controls Location and Size Using Grids........................................40013.4.3 ..Controls Location and Size Without Grids....................................40113.4.4 ..Selecting Controls on a Parent Window..........................................40413.4.5 ..Controls Resizing ................................................................................40613.4.6 ..Controls Positions................................................................................40813.4.7 ..Tab Ordering ........................................................................................ 409
-
8/8/2019 Visual Cpp and MFC Programming
9/737
Visual C++ and MFC Fundamentals Table of Contents
FunctionX, Inc. 9
CHAPTER 14: CONTROLS FUNCTIONALITY .........................................411
14.1 Handling Controls ....................................................................................... 41214.1.1 ..Introduction ..........................................................................................41214.1.2 ..Control's Control Variables:..............................................................41414.1.3 ..The Controls Data Exchange ...........................................................41714.1.4 ..Controls Value Variables ..................................................................418
14.1.5 ..Controls Event Handlers ....................................................................42014.2 Controls Management.................................................................................421
14.2.1 ..Controls Identification ......................................................................42114.2.2 ..The Client Area....................................................................................42414.2.3 ..The Screen and Client Coordinates ..................................................42814.2.4 ..The Window: Its Location and Dimensions ...................................43214.2.5 ..The Handle or Pointer to a Window.................................................43514.2.6 ..The Text of a Control.......................................................................... 43614.2.7 ..Controls Values Update......................................................................44114.2.8 ..Windows Focus..................................................................................44314.2.9 ..The Windows Visibility ....................................................................44414.2.10 The Windows Availability ...............................................................446
14.3 Access to a Controls Instance and Handle ..............................................447
14.3.1 ..The Instance of an Application .........................................................44714.3.2 ..The Handle to a Window ...................................................................44714.4 Getting Access to a Control.......................................................................448
14.4.1 ..Retrieving Control Information......................................................... 44914.4.2 ..Changing Control Information .......................................................... 450
CHAPTER 15: FUNDAMENTAL CONTROLS ............................................453
15.1 Static Controls ..............................................................................................45415.1.1 ..Introduction ..........................................................................................45415.1.2 ..Static Control Properties ....................................................................45415.1.3 ..The Picture Control .............................................................................456
15.2 Animation Controls .....................................................................................45815.2.1 ..Overview............................................................................................... 458
15.2.2 ..Animation Control and Properties ....................................................45915.2.3 ..Animation Methods............................................................................. 460
15.3 Group Boxes .................................................................................................46315.3.1 ..Introduction ..........................................................................................46315.3.2 ..Group Box Properties and Data Exchange......................................463
15.4 Command Buttons ....................................................................................... 46415.4.1 ..Overview............................................................................................... 46415.4.2 ..Command Buttons Properties and Methods....................................46415.4.3 ..Buttons Messages ................................................................................467
15.5 Property Sheet and Wizard Buttons.........................................................47115.5.1 ..Property Sheet Buttons.......................................................................47115.5.2 ..Wizard Buttons....................................................................................483
15.6 Bitmap Buttons ............................................................................................485
15.6.1 ..Overview............................................................................................... 48615.6.2 ..Bitmap Button Implementation......................................................... 486
CHAPTER 16: TEXT-BASED CONTROLS ...................................................491
16.1 Labels ............................................................................................................49216.1.1 ..Overview............................................................................................... 49216.1.2 ..Drawn Labels ....................................................................................... 49416.1.3 ..Static Labels .........................................................................................496
16.2 Edit Controls ................................................................................................498
-
8/8/2019 Visual Cpp and MFC Programming
10/737
Table of Contents Visual C++ and MFC Fundamentals
10 FunctionX, Inc.
16.2.1 ..Introduction ..........................................................................................49816.2.2 ..Edit Control Characteristics...............................................................50016.2.3 ..Multiline Edit Boxes ...........................................................................50916.2.4 ..Edit Control Messages........................................................................510
16.3 The Rich Edit Control.................................................................................51416.3.1 ..Overview............................................................................................... 51416.3.2
..A Rich Edit Control ............................................................................514
16.3.3 ..Rich Edit Properties ............................................................................516
CHAPTER 17: TRACK-BASED CONTROLS ...............................................523
17.1 Spin Button...................................................................................................52417.1.1 ..Overview............................................................................................... 52417.1.2 ..Creating a Spin Button .......................................................................52617.1.3 ..The Spin Button Properties ................................................................52817.1.4 ..Methods of Managing an UpDown Control ...................................52917.1.5 ..The Spin Button Events......................................................................535
17.2 The UpDown Control .................................................................................53817.2.1 ..Overview............................................................................................... 53817.2.2 ..Using an UpDown Control ................................................................540
17.2.3 ..The UpDown Control Events ............................................................ 54317.3 Slider Controls .............................................................................................54517.3.1 ..Overview............................................................................................... 54517.3.2 ..Slider Creation .....................................................................................55017.3.3 ..Slider Properties ...................................................................................55117.3.4 ..Slider Methods.....................................................................................55417.3.5 ..Slider Events ........................................................................................ 557
CHAPTER 18: PROGRESS-BASED CONTROLS .......................................559
18.1 Timers............................................................................................................56018.1.1 ..Overview............................................................................................... 56018.1.2 ..The Timer Control...............................................................................56118.1.3 ..The Timer Messages and Methods...................................................56218.1.4 ..The Tick Counter.................................................................................564
18.2 Progress Controls .........................................................................................56718.2.1 ..Overview............................................................................................... 56718.2.2 ..Progress Bar Properties ......................................................................56818.2.3 ..Progress Control Methods and Events .............................................569
18.3 Progress Bars ................................................................................................57218.3.1 ..Introduction ..........................................................................................57218.3.2 ..Creating Progress Bars .......................................................................57218.3.3 ..Progress Bars Methods and Events ..................................................575
18.4 Scroll Bars ....................................................................................................57718.4.1 ..Introduction ..........................................................................................57718.4.2 ..Creating Scroll Bars on Views and Dialog Boxes .........................57818.4.3 ..Creating a Scroll Bar Control ............................................................ 583
18.4.4 ..ScrollBar Properties ............................................................................58618.4.5 ..Scroll Bar Methods..............................................................................58918.4.6 ..Scroll Bar Events .................................................................................593
18.5 Flat Scroll Bars ............................................................................................59618.5.1 ..Overview............................................................................................... 59618.5.2 ..Flat Scroll Bar Properties ...................................................................59618.5.3 ..Flat Scroll Bar Methods and Events.................................................598
CHAPTER 19: SELECTION-BASED CONTROLS .....................................599
-
8/8/2019 Visual Cpp and MFC Programming
11/737
Visual C++ and MFC Fundamentals Table of Contents
FunctionX, Inc. 11
19.1 Radio Buttons............................................................................................... 60019.1.1 ..Introduction ..........................................................................................60019.1.2 ..Creating Radio Buttons......................................................................60219.1.3 ..Radio Button Properties .....................................................................60419.1.4 ..Radio Buttons Methods ......................................................................60719.1.5 ..Radio Buttons Events .......................................................................... 611
19.2
Check Boxes .................................................................................................61819.2.1 ..Introduction ..........................................................................................618
19.2.2 ..Check Box Properties ......................................................................... 62019.2.3 ..Check Box Methods............................................................................62319.2.4 ..Check Box Events ...............................................................................627
CHAPTER 20: LIST-BASED CONTROLS .....................................................633
20.1 List Boxes ..................................................................................................... 63420.1.1 ..Overview............................................................................................... 63420.1.2 ..List Box Fundamentals .......................................................................63520.1.3 ..List Box Methods ................................................................................63620.1.4 ..List Box Messages and Events..........................................................643
20.2 Combo Boxes ...............................................................................................650
20.2.1 ..Overview............................................................................................... 65020.2.2 ..Combo Box Properties ........................................................................65220.2.3 ..Combo Box Methods..........................................................................65420.2.4 ..Combo Box Messages and Events ....................................................656
20.3 Image Lists ...................................................................................................65720.3.1 ..Introduction ..........................................................................................65720.3.2 ..Image List Creation.............................................................................658
CHAPTER 21: TREE AND LIST CONTROLS .............................................663
21.1 The Tree Control .........................................................................................66421.1.1 ..Overview............................................................................................... 66421.1.2 ..Tree List Properties .............................................................................66721.1.3 ..Tree Controls Methods .......................................................................66921.1.4 ..Tree Control Messages .......................................................................67521.1.5 ..Tree Control With Bitmaps or Icons................................................679
21.2 The Tree View .............................................................................................68121.2.1 ..Overview............................................................................................... 68121.2.2 ..Tree View Implementation ................................................................681
21.3 The List Control...........................................................................................68221.3.1 ..Overview............................................................................................... 68221.3.2 ..List Control Creation .......................................................................... 68321.3.3 ..Items of a List Control........................................................................68721.3.4 ..The Report View.................................................................................68921.3.5 ..Views Transition..................................................................................69321.3.6 ..List Control and Icons ........................................................................694
21.4 The List View...............................................................................................696
21.4.1 ..Overview............................................................................................... 69721.4.2 ..List View Implementation .................................................................697
CHAPTER 22: CUSTOM LIBRARIES ............................................................705
22.1 Introduction to Libraries.............................................................................70622.1.1 ..Overview............................................................................................... 70622.1.2 ..Libraries Characteristics .....................................................................706
22.2 General Static Libraries ..............................................................................70622.2.1 ..Introduction ..........................................................................................706
-
8/8/2019 Visual Cpp and MFC Programming
12/737
Table of Contents Visual C++ and MFC Fundamentals
12 FunctionX, Inc.
22.2.2 ..Creation of a Static Library ...............................................................70722.3 MFC Static Libraries ...................................................................................713
22.3.1 ..Introduction ..........................................................................................71322.3.2 ..Creation of an MFC Static Library ...................................................71322.3.3 ..MFC Static Library Test ....................................................................716
22.4 Win32 DLL ..................................................................................................71922.4.1
..Introduction ..........................................................................................719
22.4.2 ..Fundamentals of a DLL......................................................................72022.4.3 ..Win32 DLL Test..................................................................................722
22.5 DLL Module-Definition Files ...................................................................72522.5.1 ..Definition File Fundamentals ............................................................ 72522.5.2 ..Usage of a Definition File DLL ........................................................728
INDEX ..........................................................................................................................733
-
8/8/2019 Visual Cpp and MFC Programming
13/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 13
Table of Figures
Figure 1: Microsoft Visual Studio IDE.......................................................................................17Figure 2: Open Workspace...........................................................................................................19
Figure 3: Customizing the IDE Resources.................................................................................22Figure 4: The Workspace Window..............................................................................................24Figure 5: The Resource View.......................................................................................................25
Figure 6: The ResourceView tab of the Worskpace.................................................................25Figure 7: The Source Code Editor..............................................................................................26Figure 8: Moving a Dockable Window ......................................................................................27
Figure 9: Creating a New Project - Exercise1...........................................................................28Figure 10: Win32 Application Wizard - Exercise1..................................................................28Figure 11: Adding a C++ Class....................................................................................................31
Figure 12: File Opening in Visual Studio ..................................................................................32Figure 13: Opening a File With the Open Dialog Box............................................................33Figure 14: A Microsoft Visual C++ Message Box...................................................................33
Figure 15: The Options Dialog Box............................................................................................34Figure 16: New Project - Windows Fundamentals ...................................................................40Figure 17: Win32 Application Wizard - Windows Fundamentals .........................................41
Figure 18: Window Frame Property Pages ................................................................................41Figure 19: ADd New Item - Windows Fundamentals ..............................................................42Figure 20: Microsoft Development Environment - Message Box.........................................42
Figure 21: Window Illustration....................................................................................................43
Figure 22: A Window Frame with Title Bar..............................................................................44Figure 23: A Window Frame with no Caption..........................................................................44
Figure 24: Windows Application.................................................................................................46Figure 25: The Title Bar of a Frame ...........................................................................................49Figure 26: A Frame with System Menu .....................................................................................49
Figure 27: A Frame with a Minimize Button ............................................................................50Figure 28: A Frame with a Maximize Button............................................................................50Figure 29: Windows Application - Resizing .............................................................................51
Figure 30: Illustration of Window Origin ..................................................................................52Figure 31: Illustration of Window Location ..............................................................................53Figure 32: Illustration of Window Origin and Location .........................................................55
Figure 33: Illustration of Window Location, Origin, and Size ...............................................56Figure 34: A Simple Message Box..............................................................................................58
Figure 35: An Elaborate Message Box.......................................................................................58
Figure 36: Components of a Message Box................................................................................59Figure 37: A Simple Message Box with MessageBox() ..........................................................61Figure 38: A Message's Box on Various Lines .........................................................................61
Figure 39: Creating a Multiline Message Box...........................................................................62
-
8/8/2019 Visual Cpp and MFC Programming
14/737
Chapter 1: Introduction to Visual C++ Visual C++ and MFC Fundamentals
14 FunctionX, Inc.
Figure 40: A Message Box with Various Buttons ....................................................................63Figure 41: A Message Box with an Icon....................................................................................64
Figure 42: Creating a Message Box............................................................................................65Figure 43: Creating an Elaborate Message Box........................................................................66Figure 44: Add Resource - Icon...................................................................................................70
Figure 45: Add Resource - Icon...................................................................................................75Figure 46: Simple Menu................................................................................................................84Figure 47: Version Table ...............................................................................................................89
Figure 48: Resource Symbols .......................................................................................................93Figure 49: New Symbol................................................................................................................93Figure 50: Notepad as an SDI ...................................................................................................137
Figure 51: New Project - DocView1........................................................................................ 138Figure 52: Win32 Application Wizard - DocView1 .............................................................139Figure 53: DocView1 Property Pages......................................................................................139
Figure 54: Adding an Icon Resource........................................................................................ 140
Figure 55: Icon Design - DocView1 ........................................................................................ 140
Figure 56: Icon Design - DocView2 ........................................................................................ 140Figure 57: A Tree List With One Root....................................................................................664Figure 58: A Tree List With Various Roots............................................................................664Figure 59: A Newly added Tree Control .................................................................................666
Figure 60: A Tree List With All Items As Roots ...................................................................670
-
8/8/2019 Visual Cpp and MFC Programming
15/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 15
Chapter 1:
Introduction to Microsoft
Visual C++
? The: Microsoft Visual C++ Environment
? Floatable and Dockable Windows
? Visual C++ Projects and Files
? Help
-
8/8/2019 Visual Cpp and MFC Programming
16/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
16 FunctionX, Inc.
1.1 The Microsoft Visual C++ Environment
1.1.1 IntroductionMicrosoft Visual C++ is a programming environment used to create applications for the
Microsoft Windows operating systems. To use this ebook, you must have installed eitherMicrosoft Visual C++ 6.0 or Microsoft Visual Studio 6.0.
Although Microsoft Visual C++ .Net provides two programming environments for theprice of one, in this book, we will use Microsoft Visual Studio 6.0 and we will cover onlyMicrosoft Foundation Class (MFC) library programs.
After installing it, to use the programming environment, you must first open it. To dothat, you would click Start -> (All) Programs -> Microsoft Visual Studio 6.0 -> Microsoft
Visual Studio 6.0.
If you were using version 6, to open it, you would click Start -> (All) Programs ->Microsoft Visual Studio 6.0 -> Microsoft Visual C++ 6.0
This book uses the -> arrow for the menu requests.
From now on, in this book,
Request Example Means
Edit -> Copy Click Edit then click Copy
View -> Toolbars -> Custom Click View position the mouse on Toolbars, and thenclick Custom
Practical Learning: Launching Microsoft Visual C++
?? To start Microsoft Visual C++ or Visual Studio, on the Taskbar, click Start (All)Programs -> Microsoft Visual Studio 6.0 -> Microsoft Visual Studio 6.0
-
8/8/2019 Visual Cpp and MFC Programming
17/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 17
Figure 1: Microsoft Visual Studio IDE
1.1.2 The Integrated Development Environment
After Microsoft Visual Studio has been opened, the screen you look at is called anIntegrated Development Environment or IDE. The IDE is the set of tools you use tocreate a program. The IDE design of Microsoft Visual C++ 6 and Microsoft Visual
Studio .Net is significantly different. While version 6 had its unique design as comparedto Microsoft Visual Basic, the programming environments of Microsoft share the samelook in the 2002 and 2003 releases.
1.1.3 The Title Bar
1. The system icon is used to identify the application that you are using. Almost
every application has its own system icon. The system icon holds its own list ofactions; for example, it can be used to move, minimize, maximize or close (whendouble-clicked) a window.
To see its list of actions, click it
2. To see an example, while the system menu is displaying, click Minimize. To bringback the IDE, on the Task bar, click Microsoft Visual C++
-
8/8/2019 Visual Cpp and MFC Programming
18/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
18 FunctionX, Inc.
3. The main section of the title bar displays Microsoft Visual C++. This starting titledisplays in the beginning until you decide to create a particular type of application,
then the title changes. You will experience it once we start some programmingassignments.
4. The main section of the title bar is also used to move, minimize, maximize the top
section of the IDE, or to close Visual Studio. On the right section of the title bar,
there are three system buttons with the following roles
Button Role
Minimizes the window
Maximizes the window
Restores the window
Closes the window
1.1.4 The Main Menu
Under the title bar, there is a range of words located on a gray bar. This is called the
menu or main menu.
In this ebook, the expression Main Menu refers to the menu on top of the IDE.MSVC means Microsoft Visual C++
MSVC 6 means Microsoft Visual C++ 6.0MSVC 7 means Microsoft Visual C++ 2003
To use a menu, you click one of its words and the menu expands.
If an item is missing from the main menu, you can customize it.
1 Click File. There are four main types of menus you will encounter.
When clicked, the behavior of a menu that stands alonedepends on the actions prior to clicking it. Under the File menu, examples includeClose, Save All, or Exit. For example, if you click Close, Microsoft Visual Studio
will find out whether the current file had been saved already. If it has been, the filewould be closed; otherwise, you would be asked whether you want to save it beforeclosing it
2 To see an example, click Exit.
3 Start Microsoft Visual C++ the same way we did earlier
4 A menu that is disabled is not accessible at the moment.
This kind of menu depends on another action or the availability of something else.To see an example, one the main menu, click Window:
-
8/8/2019 Visual Cpp and MFC Programming
19/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 19
5 A menu with three dots means that an intermediary
action is required in order to apply its assigned behavior. Usually, this menu wouldcall a dialog box where the user would have to make a decis ion.As an example, on the main menu, position the mouse on File and click OpenWorkspace...
6 On the dialog box, locate the folder that has your downloaded exercises. Locate anddisplay the Exercise1 folder in the Look In combo box:
Figure 2: Open Workspace
7 Click Exercise1 and click Open.
8 A menu with an arrow holds a list of menu
items under it. A menu under another menu is called a submenu. To use such amenu, you would position the mouse o n it to display its submenu.
-
8/8/2019 Visual Cpp and MFC Programming
20/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
20 FunctionX, Inc.
For example, on the main menu, click Project and position the mouse on Add ToProject...
9 To dismiss the menu, click Project
10 Notice that, on the main menu (and any menu), there is one letter underlined on eachword. Examples are F in File, E in Edit, V in View, etc. The underlined letter is
called an access key. It allows you to access the same menu item using the keyboard.
In order to use an access key, the menu should have focus first. The menu is givenfocus by pressing either the Alt or the F10 keys.
11 To see an example, press Alt
12 Notice that one of the items on the menu, namely File, has its borders raised. Thismeans that the menu has focus.
13 Press t and notice that the Tools menu is expanded.
14 When the menu has focus and you want to dismiss it, you can press Esc.
For example, press Esc.
15 Notice that the Tools menu has collapsed but the menu still has focus.
16 Press f then press o. Notice that the Open dialog box displays.
17 To dismiss the Open dialog box, press Esc
18 On some menu items, there is a key or a combination of keys we call a shortcut. Thiskey or this combination allows you to perform the same action on that menu usingthe keyboard.
If the shortcut is made of one key only, you can just press it. If the shortcut is madeof two keys, press and hold the first one, while you are holding the first, press the
second key once and release the first key. Some shortcuts are a combination of threekeys.
To apply an example, press and hold Ctrl, then press o, and release Ctrl.
19 Notice that the Open dialog box opens. To dismiss it, press Esc
From now on, in this book,
Press MeansT Press the T key
Alt, G Press and release Alt. Then press G
Ctrl + H Press and hold Ctrl. While you are still holding Ctrl, press H once.
Then release Ctrl
Ctrl + Shift + E Press and hold Ctrl. Then press and hold Shift. Then press E once.Release Ctrl and Shift
-
8/8/2019 Visual Cpp and MFC Programming
21/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 21
One of the differences between Microsoft Visual C++ 6 and Microsoft Visual Studio.Net is that, on version 6, the menu bar can be moved to any section on the IDE. To do
this, you can click and hold on the small vertical grab bars on the left side of the Filemenu, then drag to any location of your choice. In the 2002 and 2003 versions, themenu bar cannot be moved.
In all versions, the main menu is customizable. This means that you can add and remove
items from the menu.
1.1.5 The Toolbars
A toolbar is an object made of buttons. These buttons provide the same features youwould get from the (main) menu, only faster. Under the main menu, the IDE is equipped
with an object called the Standard toolbar. For example, to create a new project, on themain menu, you could click File -> New -> Project On the other hand, the Standardtoolbar is equipped with a button to perform the same action a little faster.
By default, the Standard toolbar is positioned under the main menu but you can positionit anywhere else on the IDE. Like the menu, the toolbars can be customized.
1. Click and drag the gripper on the Standard toolbar down and right:
2. Notice that the toolbar has moved.
3. Once moved, you can resize the toolbar. To do that, position the mouse on the rightborder of the toolbar. Click and drag in the left direction:
4. To restore the toolbar to its previous position, double-click its title bar.5. You can get a list of the toolbars that are available if you right-click any button on
any toolbar or menu.
For example, right-click a toolbar and notice the list
6. To dismiss the menu, press Esc.
In this book, every toolbar is referred to by its name
7. A toolbar is equipped with buttons that could be unfamiliar. Just looking at one is not
obvious. The solution into knowing what a button is used for is to position the mouseon top of it. A tool tip will come up and display for a few seconds.As an example, position the mouse (do not cl ick) on the second button from left on
the Standard toolbar:
8. Without clicking, move the mo use to another button and to other buttons
From now on, each button on any toolbar will be named after its tool tip. Thismeans that, if a tool tip displays "Hungry", its button will be called the HungryButton. If a tool tip display "Exercises and Assignments", its button will be called
the Exercises and Assignments button. If you are asked to click a button, position
-
8/8/2019 Visual Cpp and MFC Programming
22/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
22 FunctionX, Inc.
your mouse on different buttons until one displays the referred to name.
9. To use a toolbars button, you click it. For example, click the Open button .Notice that the action calls the Open dialog box.
10. Press Esc to dismiss the New Project dialog box.
11. Some buttons present an arrow on their right side. This arrow represents a menu.
To see an example, position the mouse on the Wizard Bar Actions button and clickthe arrow on the right side. Observe the menu:
12. Press Esc to dismiss the menu.
13. To customize the Standard toolbar by adding buttons to it, right-click anything on themenu bar or the toolbar and click Customize...
14. On the Customize dialog box, click the Commands tab.
15. In the Categories list, select File
16. Under the Buttons combo box, click and drag the New button. Position it to be the
most left button on the Standard toolbar
Figure 3: Customizing the IDE Resources
17. Release the mouse
18. To add another toolbar button, while the Customize dialog box is still displaying, onthe main menu, click File
-
8/8/2019 Visual Cpp and MFC Programming
23/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 23
19. Press and hold Ctrl. Click and hold Open Workspace Then drag it and position itanywhere on the Standard toolbar
20. Release Ctrl and click File to dismiss its menu
21. On the Standard toolbar, click and drag Open Workspace to position it to the rightof the New button
22. On the Standard toolbar, right-click Open Workspace and click ButtonAppearance
23. Click the Image Only radio button. In the Images section, click the icon on 2nd column 4th row
24. Click OK on the Button Appearance dialog box
25. Click the Close button on the Customize dialog box
1.1.6 The Studio Windows
When creating your applications, you will use a set of windows that each accomplishes aspecific purpose. In some windows, information is organized in a tree list equipped with+ or buttons. To expand a list, you can click its + button. To collapse a list, click its
sign
The workspace is divided into individual windows that, by default, are docked together as
an ensemble:
-
8/8/2019 Visual Cpp and MFC Programming
24/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
24 FunctionX, Inc.
Figure 4: The Workspace Window
The FileView tab of is used to display the project name and its categories of files. The
categories are organized as folders and include the Source Files, the Header Files and theResource Files. They can also display other dependent files.
The Class View is used to display the classes, independent functions and variables usedby the project:
-
8/8/2019 Visual Cpp and MFC Programming
25/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 25
Figure 5: The Resource View
The Resource View displays an organized tree list of the resources used on a project:
Figure 6: The ResourceView tab of the Worskpace
Practical Learning: Using the Studio Windows
1. In the Workspace, click the ClassView tab and, if necessary, click the + button of
Exercise to expand it
2. Notice that the names of classes start with C
3. Double-click CMainFrame
4. Notice that this displays the contents of the CMainFrame class in the Code Editor
5. Also notice that the name of the file displays on the title bar, is MainFrm.h
6. To show the source file of the CMainFrame class, expand the class and double-click
OnCreate to display its listing in the Code Editor:
-
8/8/2019 Visual Cpp and MFC Programming
26/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
26 FunctionX, Inc.
Figure 7: The Source Code Editor
1.2 Floatable and Dockable Windows
1.2.1 Description
An object, called a window, is referred to as dockable when it can assume different
"gluable" or floating positions on the screen. This means that such a window can bemoved and glued to another or it can simply be placed independently of other windows ofthe same application. Windows that are dockable indicate this by the presence of
"grippers", which are 3-D lines or bars on the left side of the window.
To move a window, find its gripper(s). Click and drag it in the desired direction. To dock
a window is to glue it either to one side of the IDE or to another window. To do this, dragits gripper(s) to the desired side or the necessary window and release the mouse.
Practical Learning: Docking and Floating Windows
1. Click and hold the mouse on top of the WorkspaceDrag to the center of the window
-
8/8/2019 Visual Cpp and MFC Programming
27/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 27
Figure 8: Moving a Dockable Window
2. To position the window to its previous position, double-click its title bar
1.3 Visual C++ Projects and Files
1.3.1 Creating a New Project
Microsoft Visual C++ allows creating various types of projects ranging from regularapplications to complex libraries, from Win32 applications to communications modules
or multimedia assignments.
Various options are available to create a project:
?? On the main menu, you can click File -> New
?? The shortcut to create a new project is Ctrl + N
Any of these actions displays the New Project dialog box from where you can select thetype of project you want.
Practical Learning: Creating a Microsoft Visual C++ Project
1. On the main menu, click File -> New
2. In the New dialog box, click the Projects tab
3. In the list, click Win32 Console Application
4. In the Location edit box, replace the text with C:\MSVC
5. In the Name edit box, type Exercise2
-
8/8/2019 Visual Cpp and MFC Programming
28/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
28 FunctionX, Inc.
Figure 9: Creating a New Project - Exercise1
6. Click OK
7. In the Win32 Console Application Step 1 of 1, click An Empty Project
Figure 10: Win32 Application Wizard - Exercise1
8. Click Finish and click OK
1.3.2 Creating Files
Although the most popular files used in Visual C++ are header and source files, this IDE
allows you to create various other types of files that are not natively C++ types.
To create a file, on the main menu, you can click File -> New This would open the
New dialog box from where you can select the desired type of file and click Open. Bydefault, the new file would open in the Source Code Editor. If you want to open the file
-
8/8/2019 Visual Cpp and MFC Programming
29/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 29
otherwise, after selecting it in the New dialog box, click the arrow of the Open As combobox
Practical Learning: Creating a C++ File
1. On the main menu, click File -> New
2. Make sure the Files tab is selected. Click C++ Source File
3. In the Name edit box, type Main and press Enter
4. In the empty file, type the following:
#include
#include using namespace std;
int main()
{cout Add Existing Item
?? On the main menu, you can also click Project -> Add Existing Item
?? In the Solution Explorer, you can right-click the name of the project, position
your mouse on Add and click Add Existing Item
Any of these actions would open the Add Existing Item dialog box. This requires you to
locate the item and select it. Once the item is ready, to add it to the project, click the
Open button. If you want to open it first, click the arrow of the Open button and specifyhow you want to open the file.
Practical Learning: Adding a File to a Project
1. On the main menu, click Project -> Add To Project -> Files
2. Locate the folder that contains the exercises that accompany this book and display itin the Look In combo box.
-
8/8/2019 Visual Cpp and MFC Programming
30/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
30 FunctionX, Inc.
3. In the list of files, clickMain.h and click Open
4. In the FileView tab of the Workspace, if necessary, expand the Header Files node.Double-click Main.h to display it in the Source Code Editor
1.3.4 Adding ClassesTo add a class to your project, you have various options. You can separately create aheader file then create a source file. You can also import an existing header file and asource file from any valid path.
Microsoft Visual C++ makes it easy to create a C++ class. When doing this, a header anda source files are prepared. A (default) constructor and a destructor are added. You can
delete or completely change the supplied constructor and/or the destructor. Also, VisualC++ includes the name of the header file in the source file so you would not haveforgotten to do this.
Most classes in Visual C++ have their names starting with the letter C. Although this isonly a suggestion, and any valid class name would work fine, to keep some harmony, in
this book, we will follow this convention.
To create a new class and add it to your project:
?? On the main menu, you can click Insert -> New Class
?? In the ClassView tab of the Workspace, you can right-click the name of theproject and click New Class
Any of these actions would open the New Class dialog box. From here, you can specifythe type of class you want to create and click Open. You will then be asked to provide aname for the class.
Practical Learning: Creating a Class
1. To create a new class, in the ClassView tab, right-click Exercise2 and click NewClass
2. In the New Class dialog box, in the Name edit box, type CPart
NoteIn this book, we will
not follow thefamous hungariannaming conventionbecause nobodyseems to be able tofollow or keep upwith it, not evenprogrammers from
Microsoft.
-
8/8/2019 Visual Cpp and MFC Programming
31/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 31
Figure 11: Adding a C++ Class
3. Click OK
4. To save everything, on the Standard toolbar, click Save All
1.3.5 Opening Files
A file is a series of computer bits that are aligned in the computer memory as one entity
to constitute a document. To open a file, simply display the Open dialog box and proceedas you would for any other computer application that can be used to open a file.
If you had previously created or opened a file, Visual C++ keeps a list of the mostrecently used (MRU) file under its File menu. To control the maximum number of filesthat can be listed:
?? If you are using MSVC 6, display the Options dialog box and access its Workspaceproperty page. Then change the value of the Recent File List Contains edit box
?? If you are using MSVC 7, open the Options dialog box and, under the Environmentnode, click the General link. Then, change the value of the second Display line
Its ability to open various types of files allows you to view a word processing document,a spreadsheet or a presentation.
When opening a file that is not a native C++ or Visual C++ file, the main menu andtoolbar of its parent application take over the top area of the Visual C++ environment.
This gives you all the features of the application. For example, you can open a word
-
8/8/2019 Visual Cpp and MFC Programming
32/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
32 FunctionX, Inc.
processing document and use it as if you were working from the application in which thedocument was created. Here is a Microsoft Word document opened in Visual C++:
Figure 12: File Opening in Visual Studio
This means that C++ files are not the only files you are able to view in Visual C++.Although you can open various types of document, this does not imply that any of themwould display easily. Some of them may have rules to follow. That is why the Open
dialog box of Visual C++ is equipped with the Open As combo box. For example, whenopening a file that has the .rc extension, which is a resource file, you have the option ofopening it automatically or as a text file and the result would be different depending on
the selection option .
Practical Learning: Opening Files
1. To open a new file, on the main menu, click File -> Open
2. Locate the exercises that accompany this book and display the Exercise1 folder inthe Look In combo box
3. Make sure the Files of Type is set to C++ Files (.c;, cpp:, .cxx;, .tli;, .h;, .tlh;, .inl;,
.rc) or All Files.Click Exercise1View.cpp and click Open
4. To open another file, on the Standard toolbar, click the Open button
5. Click Exercise1.rc
6. Click the arrow of the Open As combo box and select Text
-
8/8/2019 Visual Cpp and MFC Programming
33/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 33
Figure 13: Opening a File With the Open Dialog Box
7. Click Open
8. Notice that the file was opened as text although it is a resource file, just like theprevious one
9. To close the file, using the main menu, click Window -> Close
10. To close the project, on the main menu, click File -> Close Workspace
11. When asked whether you want to close all documents, click Yes. Also, agree to savethe Main.cpp file
1.3.6 Opening Existing Projects
A project is made of various files and subject to the environment in which it was created.
A project itself is a file but it is used to connect all the other files that compose itsparticular application.
There is a limit on the types of projects you can open in Visual C++. This is not ananomaly. Every computer-programming pro ject is created using a particular environmentand each environment configures and arranges its project as its judges it necessary. For
example, the files necessary to create a Borland Delphi project are not the same for aVisual C++ project and Visual C++ would not need to use such a project. Therefore, anyattempt to open an unrecognizable project would produce an error. Here is a message box
resulting from trying to open a Delphi project:
Figure 14: A Microsoft Visual C++ Message Box
-
8/8/2019 Visual Cpp and MFC Programming
34/737
Chapter 2: Introduction to MFC Visual C++ and MFC Fundamentals
34 FunctionX, Inc.
Visual C++ is configured to easily open its own Visual C++ projects that have the .dsw or.mdp extensions. Additionally, you can open a Win32 project that was created using
Visual C++ (you may not be able to open a Win32 project created using Borland C++Builder). You can also open Visual C++ and Win32 projects created with previousversions of Visual C++.
To open a project, if you have used it as one of the previous 8 projects, when VisualStudio opens and displays the Start Page, you should see a list of the last 8 projects used.
You can then click it to open it. As done with files, Visual Studio keeps a list of theprevious projects on its File menu. Therefore, to open one of the previously used projects,you can click File -> Recent Projects. If you see the project you are looking for, you can
click it. If you write a lot of projects regularly, you may want to increase the list of filesand/or projects that the file menu keeps. To do this, on the main menu, you can clickTools -> Options In the Options dialog box, click the General node under Environment
and set the number desired for the Display edi t boxes:
Figure 15: The Options Dialog Box
Practical Learning: Opening a Project
1. To open a project, on the main menu, click File
2. Position the mouse on Recent Workspaces and click Exercise2
1.4 Getting Help
1.4.1 Online Help
While working on your projects, you will usually need to figure out how something can
be done and sometimes why it is done like that. This is where you will need help. Thereare three primary types of help you can use: online, Microsoft, and others.
-
8/8/2019 Visual Cpp and MFC Programming
35/737
Visual C++ and MFC Fundamentals Chapter 1: Introduction to Visual C++
FunctionX, Inc. 35
When Microsoft Visual C++ 6 gets installed, it asks you whether you want to install the
MSDN library. This is because, besides the CDs that hold Visual C++ or Visual Studioinstallation files or the DVD, additional CDs are provided to you just for the help files. Insome cases, the help files are packed in one an additional DVD. If you decide to install
the MSDN library, it would be installed as an independent program but would beinternally linked to Visual Studio. Because this library is particularly huge, youcan/should keep it on the CDs or the DVD and access it only when needed. The help files
that are