Audience - MFC Tutorial

Transcription

Microsoft Foundation ClassesAbout the TutorialThe Microsoft Foundation Class (MFC) library provides a set of functions, constants, datatypes, and classes to simplify creating applications for the Microsoft Windows operatingsystems. In this tutorial, you will learn all about how to start and create Windows-basedapplications using MFC.AudienceThis tutorial is designed for all those developers who are keen on developing best-in-classapplications using MFC. The tutorial provides a hands-on approach with step-by-stepprogram examples, source codes, and illustrations that will assist the developers to learnand put the acquired knowledge into practice.PrerequisitesTo gain advantage of this tutorial you need to be familiar with programming for Windows.You also need to know the basics of programming in C and understand thefundamentals of object-oriented programming.Disclaimer & Copyright Copyright 2018 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.com.i

Microsoft Foundation ClassesTable of ContentsAbout the Tutorial . iAudience. iPrerequisites. iDisclaimer & Copyright . iTable of Contents. ii1. MFC — OVERVIEW . 1Prerequisites. 1What is MFC? . 1MFC Framework . 1Why MFC? . 22. MFC — ENVIRONMENT SETUP . 33. MFC — VC PROJECTS. 84. MFC — GETTING STARTED . 9Create Project Using Project Templates . 9Create Project from Scratch . 145. MFC — WINDOWS FUNDAMENTALS. 19Window Creation . 24Main Window . 25Windows Styles . 27Windows Location . 29Windows Size . 30Windows Dimensions . 31Windows Parents . 32ii

Microsoft Foundation Classes6. MFC — DIALOG BOXES . 34Dialog Box Creation . 40Dialog Location . 43Dialog Box Dimensions . 45Dialog Box Methods. 45Modal Dialog Boxes . 46Dialog-Based Applications . 497. MFC — WINDOWS RESOURCES. 58Identifiers . 59Icons . 60Menus . 63Toolbars. 68Accelerators. 758. MFC — PROPERTY SHEETS . 869. MFC — WINDOWS LAYOUT. 97Adding controls . 97Control Grid . 99Controls Resizing . 100Controls Positions . 101Tab Ordering . 10710.MFC — CONTROLS MANAGEMENT. 111Control Variable/Instance . 111Control Value Variable . 116Controls Event Handlers . 118Controls Management . 121iii

Microsoft Foundation Classes11.MFC — WINDOWS CONTROLS . 128Static Control . 128Animation Control . 128Button . 134Bitmap Button . 137Command Button . 139Static Text . 142List Box . 144Combo Boxes . 152Radio Buttons . 157Checkboxes . 162Image Lists . 176Edit Box . 180Rich Edit . 185Group Box. 194Spin Button . 195Managing the Updown Control . 197Progress Control . 202Progress Bars . 206Timer . 209Date & Time Picker . 213Picture . 218Image Editor . 220Slider Controls . 223Scrollbars . 230Tree Control. 232List Control . 240iv

Microsoft Foundation Classes12.MFC — MESSAGES AND EVENTS . 249Overview . 249Map of Messages . 249Windows Messages . 253Command Messages . 259Keyboard Messages . 259Mouse Messages . 26213.MFC — ACTIVEX CONTROL . 26514.MFC - FILE SYSTEM. 271Drives . 271Directories . 275File Processing . 27915.MFC — STANDARD I/O. 28316.MFC — DOCUMENT VIEW . 289View . 289Document . 289Frame . 289Single Document Interface (SDI) . 289Multiple Document Interface (MDI) . 29117.MFC - STRINGS . 295Create String . 297Empty String . 298String Concatenation . 300String Length. 301String Comparison . 303v

Microsoft Foundation Classes18.MFC — CARRAY . 305Create CArray Object . 306Add items . 306Retrieve Items . 306Add Items in the Middle . 308Update Item Value . 310Copy Array . 312Remove Items . 31419.MFC — LINKED LISTS. 317Singly Linked List. 317Doubly Linked List . 317CList Class . 318Create CList Object . 319Add items . 319Retrieve Items . 319Add Items in the Middle . 321Update Item Value . 323Remove Items . 32520.MFC — DATABASE CLASSES . 328CDatabase . 328Insert Query. 330Retrieve Record . 332Update Record . 337Delete Record . 34121.MFC - SERIALIZATION . 346vi

Microsoft Foundation Classes22.MFC — MULTITHREADING . 35323.MFC — INTERNET PROGRAMMING . 36224.MFC - GDI. 376Drawing . 376Lines . 382Polylines . 385Rectangles . 386Squares . 388Pies . 389Arcs. 391Chords . 393Colors . 394Fonts. 396Pens . 398Brushes . 39925.MFC — LIBRARIES . 402Static Library . 402Dynamic Library . 416vii

1. MFC — OverviewThe Microsoft Foundation Class (MFC) library provides a set of functions, constants, datatypes, and classes to simplify creating applications for the Microsoft Windows operatingsystems. In this tutorial, you will learn all about how to start and create windows basedapplications using MFC.PrerequisitesWe have assumed that you know the following: A little about programming for Windows. The basics of programming in C . Understand the fundamentals of object-oriented programming.What is MFC?The Microsoft Foundation Class Library (MFC) is an "application framework" forprogramming in Microsoft Windows. MFC provides much of the code, which are requiredfor the following: Managing Windows. Menus and dialog boxes. Performing basic input/output. Storing collections of data objects, etc.You can easily extend or override the basic functionality the MFC framework in you C applications by adding your application-specific code into MFC framework.MFC Framework The MFC framework provides a set of reusable classes designed to simplifyWindows programming. MFC provides classes for many basic objects, such as strings, files, and collectionsthat are used in everyday programming. It also provides classes for common Windows APIs and data structures, such aswindows, controls, and device contexts. The framework also provides a solid foundation for more advanced features, suchas ActiveX and document view processing. In addition, MFC provides an application framework, including the classes thatmake up the application architecture hierarchy.1

Microsoft Foundation ClassesWhy MFC?The MFC framework is a powerful approach that lets you build upon the work of expertprogrammers for Windows. MFC framework has the following advantages. It shortens development time. It makes code more portable. It also provides tremendous support without reducing programming freedom andflexibility. It gives easy access to "hard to program" user-interface elements andtechnologies. MFC simplifies database programming through Data Access Objects (DAO) andOpen Database Connectivity (ODBC), and network programming throughWindows Sockets.2

2. MFC — Environment SetupMicrosoft Foundation ClassesMicrosoft Visual C is a programming environment used to create applications for theMicrosoft Windows operating systems. To use MFC framework in your C application,you must have installed either Microsoft Visual C or Microsoft Visual Studio. MicrosoftVisual Studio also contains the Microsoft Visual C environment.Microsoft provides a free version of visual studio which also contains SQL Server and itcan be downloaded from oadvisual-studio-vs.aspx.Following are the installation steps.Step 1: Once Visual Studio is downloaded, run the installer. The following dialog box willbe displayed.Step 2: Click Install to start the installation process.3

Microsoft Foundation Classes4

Microsoft Foundation ClassesStep 3: Once Visual Studio is installed successfully, you will see the following dialog box.Step 4: Close this dialog box and restart your computer if required.5

Microsoft Foundation ClassesStep 5: Open Visual studio from the Start menu, which will open the following dialog box.It will take some time for preparation, while starting for the first time.6

Microsoft Foundation ClassesStep 6: Next, you will see the main window of Visual Studio.Step 7: You are now ready to start your application.7

3. MFC — VC ProjectsMicrosoft Foundation ClassesIn this chapter, we will be covering the different types of VC projects. Visual Studioincludes several kinds of Visual C project templates. These templates help to createthe basic program structure, menus, toolbars, icons, references, and include statementsthat are appropriate for the kind of project you want to create. Following are some of thesalient features of the templates. It provides wizards for many of these project templates and helps you customizeyour projects as you create them. Once the project is created, you can build and run the application. You don't have to use a template to create a project, but in most cases it's moreefficient to use project templates. It's easier to modify the provided project files and structure than it is to createthem from scratch.In MFC, you can use the following project templates.Project TemplateMFC ApplicationMFCControlActiveXMFC DLLDescriptionAn MFC application is an executable application for Windows thatis based on the Microsoft Foundation Class (MFC) Library. Theeasiest way to create an MFC application is to use the MFCApplication Wizard.ActiveX control programs are modular programs designed to givea specific type of functionality to a parent application. Forexample, you can create a control such as a button for use in adialog, or toolbar or on a Web page.An MFC DLL is a binary file that acts as a shared library of functionsthat can be used simultaneously by multiple applications. Theeasiest way to create an MFC DLL project is to use the MFC DLLWizard.Following are some General templates which can also be used to create MFC application:Project TemplateEmpty ProjectCustom WizardDescriptionProjects are the logical containers for everything that's needed tobuild your application. You can then add more new or existingprojects to the solution if necessary.The Visual C Custom Wizard is the tool to use when you needto create a new custom wizard. The easiest way to create a customwizard is to use the Custom Wizard.8

4. MFC — Getting StartedMicrosoft Foundation ClassesIn this chapter, we will look at a working MFC example. To create an MFC application, youcan use wizards to customize your projects. You can also create an application fromscratch.Create Project Using Project TemplatesFollowing are the steps to create a project using project templates available in VisualStudio.Step 1: Open the Visual studio and click on the File - New - Project menu option.Step 2: You can now see that the New Project dialog box is open.Step 3: From the left pane, select Templates - Visual C - MFCStep 4: In the middle pane, select M

Microsoft Visual C is a programming environment used to create applications for the Microsoft Windows operating systems. To use MFC framework in your C application, you must have installed either Microsoft Visual C or Microsoft Visual Studio. Microsoft Visual Studio also contai