The Microsoft Foundation Class Library Application Framework 10252020
The Microsoft Foundation Class Library Application Framework 10/25/2020 Department of IT 1
The Microsoft Foundation Class Library Application Framework Application framework “An integrated collection of object-oriented software components that offers all that's needed for a generic application. “ An Application Framework vs. a Class Library - An application framework is a superset of a class library. - An ordinary library is an isolated set of classes designed to be incorporated into any program, but an application framework defines the structure of the program itself. 10/25/2020 Department of IT 2
Why Use the Application Framework? My. App. h header file for the MYAPP application: The MFC library is the C++ Microsoft Windows API. Application framework applications use a standard structure. Application framework applications are small and fast. The Visual C++ tools reduce coding drudgery The MFC library application framework is feature rich 10/25/2020 Department of IT 3
An Application Framework Example source code for the header and implementation files for our MYAPPapplication. // application class CMy. App : public CWin. App { public: virtual BOOL Init. Instance(); }; 10/25/2020 Department of IT 4
// frame window class CMy. Frame : public CFrame. Wnd { public: CMy. Frame(); protected: // "afx_msg" indicates that the next two functions are part // of the MFC library message dispatch system afx_msg void On. LButton. Down(UINT n. Flags, CPoint point); afx_msg void On. Paint(); DECLARE_MESSAGE_MAP() }; 10/25/2020 Department of IT 5
My. App. cpp - implementation file for the MYAPP application: #include <afxwin. h> // MFC library header file declares base classes #include "myapp. h" CMy. App the. App; // the one and only CMy. App object BOOL CMy. App: : Init. Instance() { m_p. Main. Wnd = new CMy. Frame(); m_p. Main. Wnd->Show. Window(m_n. Cmd. Show); m_p. Main. Wnd->Update. Window(); return TRUE; } BEGIN_MESSAGE_MAP(CMy. Frame, CFrame. Wnd) ON_WM_LBUTTONDOWN() ON_WM_PAINT() END_MESSAGE_MAP() 10/25/2020 Department of IT 6
My. App. cpp (continued…) CMy. Frame: : CMy. Frame() { Create(NULL, "MYAPP Application"); } void CMy. Frame: : On. LButton. Down(UINT n. Flags, CPoint point) { TRACE("Entering CMy. Frame: : On. LButton. Down - %lx, %dn", (long) n. Flags, point. x, point. y); } void CMy. Frame: : On. Paint() { CPaint. DC dc(this); dc. Text. Out(0, 0, "Hello, world!"); } Full Code: 10/25/2020 Department of IT 7
The program elements: The Win. Main function Windows requires your application to have a Win. Main function. You don't see Win. Main here because it's hidden inside the application framework. The CMy. App class An object of class CMy. App represents an application. The program defines a single global CMy. App object, the. App. The CWin. App base class determines most of the. App's behavior. Application startup When the user starts the application, Windows calls the application framework's built-in Win. Main function, and Win. Main looks for your globally constructed application object of a class derived from CWin. App. In a C++ program global objects are constructed before the main program is executed. 10/25/2020 Department of IT 8
The program elements: The CMy. App: : Init. Instance member function When the Win. Main function finds the application object, it calls the virtual Init. Instance member function, which makes the calls needed to construct and display the application's main frame window. You must override Init. Instance in your derived application class because the CWin. App base class doesn't know what kind of main frame window you want. The CWin. App: : Run member function The Run function is hidden in the base class, but it dispatches the application's messages to its windows, thus keeping the application running. Win. Main calls Run after it calls Init. Instance. The CMy. Frame class An object of class CMy. Frame represents the application's main frame window. When the constructor calls the Create member function of the base class CFrame. Wnd, Windows creates the actual window structure and the application framework links it to the C++ object. The Show. Window and Update. Window functions, also member functions of the base class, must be called in order to display the window. 10/25/2020 Department of IT 9
The program elements: The CMy. Frame: : On. LButton. Down function MFC library's message-handling capability. The function invokes the MFC library TRACE macro to display a message in the debugging window. The CMy. Frame: : On. Paint function - The application framework calls this important mapped member function of class CMy. Frame every time it's necessary to repaint the window: at the start of the program, when the user resizes the window, and when all or part of the window is newly exposed. - The CPaint. DC statement relates to the Graphics Device Interface (GDI) and is explained in later chapters. The Text. Out function displays "Hello, world!" Application shutdown - The user shuts down the application by closing the main frame window. - This action initiates a sequence of events, which ends with the destruction of the CMy. Frame object, the exit from Run, the exit from Win. Main, and the destruction of the CMy. App object. 10/25/2020 Department of IT 10
MFC Library Message Mapping The MFC library application framework doesn't use virtual functions for Windows messages. Instead, it uses macros to "map" specified messages to derived class member functions Why the rejection of virtual functions? What about message handlers for menu command messages from button clicks? An MFC message handler requires a function prototype, a function body, and an entry (macro invocation) in the message map. BEGIN_MESSAGE_MAP(CMy. Frame, CFrame. Wnd) ON_WM_LBUTTONDOWN() ON_WM_PAINT() END_MESSAGE_MAP() 10/25/2020 Department of IT 11
Documents and Views Typically, MFC application will contain application and frame classes plus two other classes that represent the "document" and the "view. " This document-view architecture is the core of the application framework The document-view architecture separates data from the user's view of the data. One obvious benefit is multiple views of the same data. 10/25/2020 Department of IT 12
The Visual C++ Components 10/25/2020 13
The Visual C++ Components Microsoft Visual C++ is two complete Windows application development systems in one product. You can develop C-language Windows programs using only the Win 32 API. You can use many Visual C++ tools, including the resource editors, to make low -level Win 32 programming easier. Components: Ø The Project Ø The Resource Editors—Workspace Resource. View Ø The C/C++ Compiler Ø The Source Code Editor Ø The Resource Compiler Ø The Linker Ø The Debugger Ø App. Wizard Ø Classwizard overview of the Visual C++ application build process. 10/25/2020 14
Microsoft Visual C++ 6. 0 and the Build Process Visual C++ 6. 0 in action. what is a project? • A project is a collection of interrelated source files that are compiled and linked to make up an executable Windows-based program or a DLL. • Source files for each project are generally stored in a separate subdirectory. • A project depends on many files outside the project subdirectory too, such as include files and library files. A makefile stores compiler and linker options and expresses all the interrelationships among source files. A make program reads the makefile and then invokes the compiler, assembler, resource compiler, and linker to produce the final output, which is generally an executable file. 10/25/2020 15
Contd… In a Visual C++ 6. 0 project, there is no makefile (with an MAK extension) unless you tell the system to export one. A text-format project file (with a DSP extension) serves the same purpose. A separate text-format workspace file (with a DSW extension) has an entry for each project in the workspace. It's possible to have multiple projects in a workspace, but all the examples in this book have just one project per workspace. To work on an existing project, you tell Visual C++ to open the DSW file and then you can edit and build the project. 10/25/2020 16
VC++ Project Files Visual C++ creates some intermediate files too File Extension APS BSC CLW DEP DSP *DSW *MAK NCB OPT PLG Description Supports Resource. View Browser information file Supports Class. Wizard Dependency file Project file Workspace file External makefile Supports Class. View Holds workspace configuration Builds log file * Do not delete or edit in a text editor. 10/25/2020 17
The Resource Editors— Workspace Resource. View Each project usually has one text-format resource script (RC) file that describes the project's menu, dialog, string, and accelerator resources. The RC file also has #include statements to bring in resources from other subdirectories. These resources include project-specific items, such as bitmap (BMP) and icon (ICO) files, and resources common to all Visual C++ programs, such as error message strings. Editing the RC file outside the resource editors is not recommended. The resource editors can also process EXE and DLL files, so you can use the clipboard to "steal" resources, such as bitmaps and icons, from other Windows applications. 10/25/2020 18
The C/C++ Compiler The Visual C++ compiler can process both C source code and C++ source code. It determines the language by looking at the source code's filename extension. A C extension indicates C source code, and CPP or CXX indicates C++ source code. The compiler is compliant with all ANSI standards, including the latest recommendations of a working group on C++ libraries, and has additional Microsoft extensions. Templates, exceptions, and runtime type identification (RTTI) are fully supported in Visual C++ version 6. 0. The C++ Standard Template Library (STL) is also included, although it is not integrated into the MFC library. 10/25/2020 19
The Other Components The Source Code Editor Visual C++ 6. 0 includes a sophisticated source code editor that supports many features such as dynamic syntax coloring, auto-tabbing, keyboard bindings The Resource Compiler The Visual C++ resource compiler reads an ASCII resource script (RC) file from the resource editors and writes a binary RES file for the linker. The Linker The linker reads the OBJ and RES files produced by the C/C++ compiler and the resource compiler, and it accesses LIB files for MFC code, runtime library code, and Windows code. It then writes the project's EXE file. 10/25/2020 20
The Debugger The Visual C++ debugger has been steadily improving, but it doesn't actually fix the bugs yet. The debugger works closely with Visual C++ to ensure that breakpoints are saved on disk. The Visual C++ debugger window. 10/25/2020 21
App. Wizard Ø App. Wizard is a code generator that creates a working skeleton of a Windows application with features, class names, and source code filenames that you specify through dialog boxes. Ø App. Wizard code is minimalist code; the functionality is inside the application framework base classes. Ø App. Wizard gets you started quickly with a new application. 10/25/2020 22
Class. Wizard Ø Class. Wizard is a program (implemented as a DLL) that's accessible from Visual C++'s View menu. Ø Class. Wizard takes the drudgery out of maintaining Visual C++ class code. Ø Need a new class, a new virtual function, or a new messagehandler function? ü Class. Wizard writes the prototypes, the function bodies, and (if necessary) the code to link the Windows message to the function. ü Class. Wizard can update class code that you write, so you avoid the maintenance problems common to ordinary code generators. 10/25/2020 23
Basic Event Handling, Mapping Modes, and a Scrolling View- T 8
Basic Event Handling, Mapping Modes, and a Scrolling View The Message Handler: void CMy. View: : On. LButton. Down(UINT n. Flags, CPoint point) { // event processing code here } The Message Map: BEGIN_MESSAGE_MAP(CMy. View, CView) ON_WM_LBUTTONDOWN() // entry specifically for On. LButton. Down // other message map entries END_MESSAGE_MAP() Finally, your class header file needs the statement DECLARE_MESSAGE_MAP()
Invalid Rectangle Theory Invalidate. Rect triggers a Windows WM_PAINT message, which is mapped in the CView class to call to the virtual On. Draw function. If necessary, On. Draw can access the "invalid rectangle" parameter that was passed to Invalidate. Rect. Your On. Draw function could call the CDC member function Get. Clip. Box to determine the invalid rectangle, and then it could avoid drawing objects outside it. On. Draw is being called not only in response to your Invalidate. Rect call but also when the user resizes or exposes the window. Thus, On. Draw is responsible for all drawing in a window, and it has to adapt to whatever invalid rectangle it gets.
The Window's Client Area A window has a rectangular client area that excludes the border, caption bar, menu bar, and any toolbars. The CWnd member function Get. Client. Rect supplies you with the client-area dimensions. Normally, you're not allowed to draw outside the client area, and most mouse messages are received only when the mouse cursor is in the client area. CRect, CPoint, and CSize Arithmetic The CRect, CPoint, and CSize classes are derived from the Windows RECT, POINT, and SIZE structures, and thus they inherit public integer data members as follows: CRect left, top, right, bottom CPoint x, y CSize cx, cy
Mapping Modes – T 8 • The device context has the default mapping mode, MM_TEXT, assigned to it. The statement • p. DC->Rectangle(CRect(0, 0, 200)); • Windows provides a number of other mapping modes, or coordinate systems, that can be associated with the device context. • Coordinates in the current mapping mode are called logical coordinates.
If you assign the MM_HIMETRIC mapping mode, for example, a logical unit is 1/100 millimeter (mm) instead of 1 pixel. In the MM_HIMETRIC mapping mode, the y axis runs in the opposite direction to that in the MM_TEXT mode: y values decrease as you move down. Thus, a 4 -by-4 -cm square is drawn in logical coordinates this way: p. DC->Rectangle(CRect(0, 0, 4000, -4000));
The MM_TEXT Mapping Mode • In MM_TEXT, coordinates map to pixels, values of x increase as you move right, and values of y increase as you move down, • But you're allowed to change the origin through calls to the CDC functions Set. Viewport. Org and Set. Window. Org. • Here's some code that sets the window origin to (100, 100) in logical coordinate space and then draws a 200 -by-200 -pixel square offset by (100, 100). • The logical point (100, 100) maps to the device point (0, 0). A scrolling window uses this kind of transformation. void CMy. View: : On. Draw(CDC* p. DC) { p. DC->Set. Map. Mode(MM_TEXT); p. DC->Set. Window. Org(CPoint(100, 100));
The Fixed-Scale Mapping Modes • One important group of Windows mapping modes provides fixed scaling • In the MM_HIMETRIC mapping mode, x values increase as you move right and y values decrease as you move down. • The only difference among the fixed mapping modes is the actual scale factor, listed in the table shown here. Mapping Mode MM_LOENGLISH MM_HIENGLISH MM_LOMETRIC MM_HIMETRIC MM_TWIPS Logical Unit 0. 01 inch 0. 001 inch 0. 1 mm 0. 01 mm 1/ 1440 inch
The Variable-Scale Mapping Modes Ø Windows provides two mapping modes, MM_ISOTROPIC and MM_ANISOTROPIC, that allow you to change the scale factor as well as the origin. Ø With these mapping modes, your drawing can change size as the changes the size of the window. user Ø Also, if you invert the scale of one axis, you can "flip" an image about the other axis and you can define your own arbitrary fixedscale factors. Ø With the MM_ISOTROPIC mode, a 1: 1 aspect ratio is always preserved. In other words, a circle is always a circle as the scale factor changes. Ø With the MM_ANISOTROPIC mode, the x and y scale factors can independently. Circles can be squished into ellipses. change
Here's an On. Draw function that draws an ellipse that fits exactly in its window: void CMy. View: : On. Draw(CDC* p. DC) { CRect rect. Client; Get. Client. Rect(rect. Client); p. DC->Set. Map. Mode(MM_ANISOTROPIC); p. DC->Set. Window. Ext(1000, 1000); p. DC->Set. Viewport. Ext(rect. Client. right, -rect. Client. bottom); p. DC->Set. Viewport. Org(rect. Client. right / 2, rect. Client. bottom / 2); p. DC->Ellipse(CRect(-500, 500)); } The functions Set. Window. Ext and Set. Viewport. Ext work together to set the scale, based on the window's current client rectangle returned by the
• The resulting window size is exactly 1000 -by-1000 logical units. • The Set. Viewport. Org function sets the origin to the center of the window. Thus, a centered ellipse with a radius of 500 logical units fills the window exactly. • If you substitute MM_ISOTROPIC for MM_ANISOTROPIC in the preceding example, the "ellipse" is always a circle • It expands to fit the smallest dimension of the window rectangle.
The EX 04 B Example—Converting to the MM_HIMETRIC Mapping Mode 1. Use Class. Wizard to override the virtual On. Prepare. DC function. Class. Wizard can override virtual functions for selected MFC base classes, including CView. It generates the correct function prototype in the class's header file and a skeleton function in the CPP file. Select the class name CEx 04 a. View in the Object IDs list, and then double-click on the On. Prepare. DC function in the Messages list. Edit the function as shown here: void CEx 04 a. View: : On. Prepare. DC(CDC* p. DC, CPrint. Info* p. Info) { p. DC->Set. Map. Mode(MM_HIMETRIC); CView: : On. Prepare. DC(p. DC, p. Info); }
2 Edit the view class constructor. You must change the coordinate values for the ellipse rectangle. That rectangle is now 4 -by-4 centimeters instead of 200 -by-200 pixels. Note that the y value must be negative; otherwise, the ellipse will be drawn on the "virtual screen" right above your monitor! Change the values as shown here: CEx 04 a. View: : CEx 04 a. View() : m_rect. Ellipse(0, 0, 4000, -4000) { m_n. Color = GRAY_BRUSH; }
3. Edit the On. LButton. Down function. This function must now convert the ellipse rectangle to device coordinates in order to do the hit-test. Change the function as shown in the following code: void CEx 04 a. View: : On. LButton. Down(UINT n. Flags, CPoint point) { CClient. DC dc(this); On. Prepare. DC(&dc); CRect rect. Device = m_rect. Ellipse; dc. LPto. DP(rect. Device); if (rect. Device. Pt. In. Rect(point)) { if (m_n. Color == GRAY_BRUSH) { m_n. Color = WHITE_BRUSH; } else { m_n. Color = GRAY_BRUSH; }
4. Build and run the EX 04 B program. The output should look similar to the output from EX 04 A, except that the ellipse size will be different. If you try using Print Preview again, the ellipse should appear much larger than it did in EX 04 A.
Basic Event Handling, Mapping Modes, and a Scrolling View- T 8
Basic Event Handling, Mapping Modes, and a Scrolling View The Message Handler: void CMy. View: : On. LButton. Down(UINT n. Flags, CPoint point) { // event processing code here } The Message Map: BEGIN_MESSAGE_MAP(CMy. View, CView) ON_WM_LBUTTONDOWN() // entry specifically for On. LButton. Down // other message map entries END_MESSAGE_MAP() Finally, your class header file needs the statement DECLARE_MESSAGE_MAP()
Invalid Rectangle Theory Invalidate. Rect triggers a Windows WM_PAINT message, which is mapped in the CView class to call to the virtual On. Draw function. If necessary, On. Draw can access the "invalid rectangle" parameter that was passed to Invalidate. Rect. Your On. Draw function could call the CDC member function Get. Clip. Box to determine the invalid rectangle, and then it could avoid drawing objects outside it. On. Draw is being called not only in response to your Invalidate. Rect call but also when the user resizes or exposes the window. Thus, On. Draw is responsible for all drawing in a window, and it has to adapt to whatever invalid rectangle it gets.
The Window's Client Area A window has a rectangular client area that excludes the border, caption bar, menu bar, and any toolbars. The CWnd member function Get. Client. Rect supplies you with the client-area dimensions. Normally, you're not allowed to draw outside the client area, and most mouse messages are received only when the mouse cursor is in the client area. CRect, CPoint, and CSize Arithmetic The CRect, CPoint, and CSize classes are derived from the Windows RECT, POINT, and SIZE structures, and thus they inherit public integer data members as follows: CRect left, top, right, bottom CPoint x, y CSize cx, cy
Mapping Modes – T 8 • The device context has the default mapping mode, MM_TEXT, assigned to it. The statement • p. DC->Rectangle(CRect(0, 0, 200)); • Windows provides a number of other mapping modes, or coordinate systems, that can be associated with the device context. • Coordinates in the current mapping mode are called logical coordinates.
If you assign the MM_HIMETRIC mapping mode, for example, a logical unit is 1/100 millimeter (mm) instead of 1 pixel. In the MM_HIMETRIC mapping mode, the y axis runs in the opposite direction to that in the MM_TEXT mode: y values decrease as you move down. Thus, a 4 -by-4 -cm square is drawn in logical coordinates this way: p. DC->Rectangle(CRect(0, 0, 4000, -4000));
The MM_TEXT Mapping Mode • In MM_TEXT, coordinates map to pixels, values of x increase as you move right, and values of y increase as you move down, • But you're allowed to change the origin through calls to the CDC functions Set. Viewport. Org and Set. Window. Org. • Here's some code that sets the window origin to (100, 100) in logical coordinate space and then draws a 200 -by-200 -pixel square offset by (100, 100). • The logical point (100, 100) maps to the device point (0, 0). A scrolling window uses this kind of transformation. void CMy. View: : On. Draw(CDC* p. DC) { p. DC->Set. Map. Mode(MM_TEXT); p. DC->Set. Window. Org(CPoint(100, 100));
The Fixed-Scale Mapping Modes • One important group of Windows mapping modes provides fixed scaling • In the MM_HIMETRIC mapping mode, x values increase as you move right and y values decrease as you move down. • The only difference among the fixed mapping modes is the actual scale factor, listed in the table shown here. Mapping Mode MM_LOENGLISH MM_HIENGLISH MM_LOMETRIC MM_HIMETRIC MM_TWIPS Logical Unit 0. 01 inch 0. 001 inch 0. 1 mm 0. 01 mm 1/ 1440 inch
The Variable-Scale Mapping Modes Ø Windows provides two mapping modes, MM_ISOTROPIC and MM_ANISOTROPIC, that allow you to change the scale factor as well as the origin. Ø With these mapping modes, your drawing can change size as the changes the size of the window. user Ø Also, if you invert the scale of one axis, you can "flip" an image about the other axis and you can define your own arbitrary fixedscale factors. Ø With the MM_ISOTROPIC mode, a 1: 1 aspect ratio is always preserved. In other words, a circle is always a circle as the scale factor changes. Ø With the MM_ANISOTROPIC mode, the x and y scale factors can independently. Circles can be squished into ellipses. change
Here's an On. Draw function that draws an ellipse that fits exactly in its window: void CMy. View: : On. Draw(CDC* p. DC) { CRect rect. Client; Get. Client. Rect(rect. Client); p. DC->Set. Map. Mode(MM_ANISOTROPIC); p. DC->Set. Window. Ext(1000, 1000); p. DC->Set. Viewport. Ext(rect. Client. right, -rect. Client. bottom); p. DC->Set. Viewport. Org(rect. Client. right / 2, rect. Client. bottom / 2); p. DC->Ellipse(CRect(-500, 500)); } The functions Set. Window. Ext and Set. Viewport. Ext work together to set the scale, based on the window's current client rectangle returned by the
• The resulting window size is exactly 1000 -by-1000 logical units. • The Set. Viewport. Org function sets the origin to the center of the window. Thus, a centered ellipse with a radius of 500 logical units fills the window exactly. • If you substitute MM_ISOTROPIC for MM_ANISOTROPIC in the preceding example, the "ellipse" is always a circle • It expands to fit the smallest dimension of the window rectangle.
The EX 04 B Example—Converting to the MM_HIMETRIC Mapping Mode 1. Use Class. Wizard to override the virtual On. Prepare. DC function. Class. Wizard can override virtual functions for selected MFC base classes, including CView. It generates the correct function prototype in the class's header file and a skeleton function in the CPP file. Select the class name CEx 04 a. View in the Object IDs list, and then double-click on the On. Prepare. DC function in the Messages list. Edit the function as shown here: void CEx 04 a. View: : On. Prepare. DC(CDC* p. DC, CPrint. Info* p. Info) { p. DC->Set. Map. Mode(MM_HIMETRIC); CView: : On. Prepare. DC(p. DC, p. Info); }
2 Edit the view class constructor. You must change the coordinate values for the ellipse rectangle. That rectangle is now 4 -by-4 centimeters instead of 200 -by-200 pixels. Note that the y value must be negative; otherwise, the ellipse will be drawn on the "virtual screen" right above your monitor! Change the values as shown here: CEx 04 a. View: : CEx 04 a. View() : m_rect. Ellipse(0, 0, 4000, -4000) { m_n. Color = GRAY_BRUSH; }
3. Edit the On. LButton. Down function. This function must now convert the ellipse rectangle to device coordinates in order to do the hit-test. Change the function as shown in the following code: void CEx 04 a. View: : On. LButton. Down(UINT n. Flags, CPoint point) { CClient. DC dc(this); On. Prepare. DC(&dc); CRect rect. Device = m_rect. Ellipse; dc. LPto. DP(rect. Device); if (rect. Device. Pt. In. Rect(point)) { if (m_n. Color == GRAY_BRUSH) { m_n. Color = WHITE_BRUSH; } else { m_n. Color = GRAY_BRUSH; }
4. Build and run the EX 04 B program. The output should look similar to the output from EX 04 A, except that the ellipse size will be different. If you try using Print Preview again, the ellipse should appear much larger than it did in EX 04 A.
The Modal Dialog and Windows Common Controls 10/25/2020 54
Dialog : Using Appwizard and Classwizard 10/25/2020 55
The Modal Dialog and Windows Common Controls Ø The two kinds of dialogs are modal and modeless. Ø The CDialog base class supports both modal and modeless dialogs Modal Dialog Box: The user cannot work elsewhere in the same application (more correctly, in the same user interface thread) until the dialog is closed. Example: Open File dialog Modeless Dialog The user can work in another window in the application while the dialog remains on the screen Example: Microsoft Word's Find and Replace dialog is a good example of a modeless dialog; you can edit your document while the dialog is open. Controls. A dialog contains a number of elements called controls. Dialog controls include edit controls, buttons, list boxes, combo boxes, static text, tree views, progress indicators, sliders, and so forth. 10/25/2020 56
Programming a Modal Dialog 1. Use the dialog editor to create a dialog resource that contains various controls. -The dialog editor updates the project's resource script (RC) file to include your new dialog resource, and it updates the project's resource. h file with corresponding #define constants. 2. Use Class. Wizard to create a dialog class that is derived from CDialog and attached to the resource created in step 1. -Class. Wizard adds the associated code and header file to the Microsoft Visual C++ project. 3. Use Class. Wizard to add data members, exchange functions, and validation functions to the dialog class. 4. Use Class. Wizard to add message handlers for the dialog's buttons and other event-generating controls. 10/25/2020 57
Programming a Modal Dialog (Contd…) 5. Write the code for special control initialization (in On. Init. Dialog) and for the message handlers. Be sure the CDialog virtual member function On. OK is called when the user closes the dialog (unless the user cancels the dialog). (Note: On. OK is called by default. ) 6. Write the code in your view class to activate the dialog. This code consists of a call to your dialog class's constructor followed by a call to the Do. Modal dialog class member function. Do. Modal returns only when the user exits the dialog window. 10/25/2020 58
Programming a Modal Dialog (Contd…) In the CPP file, the constructor implementation looks like this: CMy. Dialog: : CMy. Dialog(CWnd* p. Parent /*=NULL*/) : CDialog(CMy. Dialog: : IDD, p. Parent) { // initialization code here } The use of enum IDD decouples the CPP file from the resource IDs that are defined in the project's resource. h 10/25/2020 59
The Windows Common Dialogs Windows provides a group of standard user interface dialogs, and these are supported by the MFC library classes. All the common dialog classes are derived from a common base class, CCommon. Dialog. Class Purpose CColor. Dialog Allows the user to select or create a color CFile. Dialog Allows the user to open or save a file CFind. Replace. Dialog Allows the user to substitute one string for another CPage. Setup. Dialog Allows the user to input page measurement parameters CFont. Dialog Allows the user to select a font from a list of available fonts CPrint. Dialog Allows the user to set up the printer and print a document 10/25/2020 60
Using the CFile. Dialog Class Directly The following code opens a file that the user has selected through the dialog: CFile. Dialog dlg(TRUE, "bmp", "*. bmp"); if (dlg. Do. Modal() == IDOK) { CFile file; VERIFY(file. Open(dlg. Get. Path. Name(), CFile: : mode. Read)); } The first constructor parameter (TRUE) specifies that this object is a "File Open" dialog instead of a "File Save" dialog. The default file extension is bmp, and *. bmp appears first in the filename edit box. The CFile. Dialog: : Get. Path. Name function returns a CString object that contains the full pathname of the selected file. 10/25/2020 61
BITMAPS
Bitmaps Windows bitmaps are arrays of bits mapped to display pixels. There are two kinds of Windows bitmaps: GDI bitmaps and DIBs. GDI bitmap objects are represented by the Microsoft Foundation Class (MFC) Library version 6. 0
Color Bitmaps and Monochrome Bitmaps Many color bitmaps are 16 -color. A standard VGA board has four contiguous color planes, with 1 corresponding bit from each plane combining to represent a pixel. The 4 -bit color values are set when the bitmap is created. With a standard VGA board, bitmap colors are limited to the standard 16 colors. Windows does not use dithered colors in bitmaps. A monochrome bitmap has only one plane. Each pixel is represented by a single bit that is either off (0) or on (1). The CDC: : Set. Text. Color function sets the "off" display color, and Set. Bk. Color sets the "on" color. You can specify these pure colors individually with the Windows RGB macro.
Code to load a Bitmap void On. Paint() { CBitmap mybm; CPaint. DC d(this); mybm. Load. Bitmap(IDB_BITMAP 1); CBrush mybrush; mybrush. Create. Pattern. Brush(&mybm); d. Select. Object(&mybrush); d. Rectangle(100, 300, 300); }
GDI Bitmaps and Device-Independent Bitmaps GDI Bitmaps There are two kinds of Windows bitmaps: GDI bitmaps and DIBs. GDI bitmap objects are represented by the Microsoft Foundation Class (MFC) Library version 6. 0 CBitmap class. The GDI bitmap object has an associated Windows data structure, maintained inside the Windows GDI module, that is device-dependent. Your program can get a copy of the bitmap data, but the bit arrangement depends on the display hardware. GDI bitmaps can be freely transferred among programs on a single computer, but because of their device dependency, transferring bitmaps by disk or modem doesn't make sense.
Device-Independent Bitmaps DIBs offer many programming advantages over GDI bitmaps. Since a DIB carries its own color information, color palette management is easier. DIBs also make it easy to control gray shades when printing. Any computer running Windows can process DIBs, which are usually stored in BMP disk files or as a resource in your program's EXE or DLL file. The wallpaper background on your monitor is read from a BMP file when you start Windows. The primary storage format for Microsoft Paint is the BMP file, and Visual C++ uses BMP files for toolbar buttons and other images. Other graphic interchange formats are available, such as TIFF, GIF, and JPEG, but only the DIB format is directly supported by the Win 32 API.
- Slides: 67