MFC day06 07 08 09

Source: Internet
Author: User
Tags object serialization

Split Window
Type 1
Dynamic Splitting-when the program is running, the user will drag the dynamic splitting window of the separation bar.
Each View window uses the same view class.
Static splitting-the window splitting has been completed when encoding is created. Each View window
You can use different view classes.
2 related classes
Csplitterwnd class-class for completing window splitting.
# Include <afxext. h> // header file of the extended window
3. Use
3.1 Dynamic Splitting
3.1.1 define the split window object in cmainframe
3.1.2 specify the View class to be used by using the ccreatecontext Structure
3.1.3 create Dynamic Splitting
Csplitterwnd: Create
Note: The number of splits cannot exceed 2*2.
3.2 static splitting
3.1.1 define the split window object in cmainframe
3.1.2 complete splitting in the cmainframe: oncreateclient () function
Cframewnd: oncreateclient-virtual function, will be
Cframewnd: oncreate call to create a framework window
Area object.
1. create static splitting
Csplitterwnd: createstatic
2. Create a view
Csplitterwnd: createview
3.1.3 re-splitting of the split window
1. The parent window of the split object is the previous split object instead of the main window.
2. Set the ID of the splitting object.
Csplitterwnd: idfromrowcol
3. Create a view
4. Obtain the specified view
Csplitterwnd: getpane
5. Set/obtain the current activity View
Csplitterwnd: setactivepane/getactivepane
6. Set the separator position (horizontal/vertical)
Csplitterwnd: setrowinfo
Csplitterwnd: setcolumninfo
Whether it is dynamic splitting or static splitting, each split View window is
Independent.
Document
1. Related Classes
Cdocument class-inherits from the csf-target class, meaning that the document class can also be processed
Command Message.
2. Use (display document data in the View class)
2.1 related functions
2.1.1 cview: oninitialupdate ()-Initialize view object
Called by functions in the Framework Window. After the first document is attached, the View window
Before initialization.
2.1.2 cview: getdocument ()
Obtain the document associated with the view
2.1.3 cframewnd: initialupdateframe ()-initialize the update framework
Call this function after the Framework Window is created, causing all View windows in the framework
Oninitialupdate () function call.
2.2 Implementation steps:
2.2.1 In the app: initinstance () function:
1. Create a framework object new cmainframe
2. Define the struct variable ccreatecontext cxt; Save
The address of the runtime class information, create a document, and save the Document Object address.
3. Call the loadframe function, create the framework window, and set the variable cxt
As a parameter of this function.
4. As the application Main Window, display and update
2.2.2 Add the member variable m_strdata in the document and initialize
2.2.3 rewrite the cview: oninitialupdate () function. In the function,
Display the data in the document to the view.
2.2.4 after the loadframe function, initialupdateframe () is called ()
Function.
2.3 thinking:
1 When is a view object dynamically created?
2. What is the relationship between documents and views? When will a link be established?
2.3.1 in the cframewnd: oncreate () function:
Oncreate-> oncreatehelper-> oncreateclient
-> Execution process of createview () and createview () functions:
// 1 dynamically create a view object
M_pnewviewclass-> Createobject (); 587 rows
// 2 create a View window
Pview-> Create (...); 597 rows
2.3.2 In the cview: oncreate () function :( viewcore. cpp)
Call m_pcurrentdoc-> addview (this );
The execution process of addview () function:
Void cdocument: addview (cview * pview)
{
...
// Save the view Address to the linked list of the Document Object
M_viewlist.addtail (pview );
// Save the document address to the variable of the view object
Pview-> m_pdocument = this;
...
}
View class and document class Save the address of each other. A document can correspond
Multiple Views, but one view can only correspond to one document.
2.4 order of command messages
Activeview-> document-> frame-> app
2.4.1 cframewnd: on1_msg
2.4.2 cview: on1_msg
You can rewrite the on1_msg () function to modify the sequence, but do not modify the sequence.
Three single document view architecture program
MVC Architecture-M: model, model, manage and save data.
V: View, view, and display data
C: controller, controller, receiving and distributing commands.
M: Document, document
V: View
C: frame, framework
1. Related Classes
Cwinapp/cframewnd/cview/cdocument
Document Template Class-A unified method for creating frameworks, views, and document objects.
Cdoctemplate-abstract base class that provides the functions of all document templates.
Csingledoctemplate-Single Document Template Class
Csingledoctemplate (
Uint nidresource, // resource ID
Cruntimeclass * pdocclass, // runtime class information of the document class
Cruntimeclass * pframeclass, // runtime class information of the framework class
Cruntimeclass * pviewclass // runtime class information of the View class
);
Note: Support for dynamic creation is required for defining documents, frameworks, and view classes.

--------------------------------------------------

A single document view Architecture Application
1 concept
You can manage only one document at a time.
2. Implementation Process
2.1 adddoctemplate () function
Void cwinapp: adddoctemplate (cdoctemplate * ptemplate)
{
If (m_pdocmanager = NULL)
M_pdocmanager = new cdocmanager;
M_pdocmanager-> adddoctemplate (ptemplate );
}
Void cdocmanager: adddoctemplate (cdoctemplate * ptemplate)
{
...
// Add the new document template to the document template linked list
M_templatelist.addtail (ptemplate); 534 rows
}
Conclusion:
1. The application did not directly manage the template, but handed it to the document management template.
2. You can use the linked list in the document management class to save document templates and manage multiple templates.
2.2 onfilenew () function
Void cwinapp: onfilenew ()
{
If (m_pdocmanager! = NULL)
M_pdocmanager-> onfilenew ();
}
Void cdocmanager: onfilenew ()
{
...
// Obtain the saved Document Template
Cdoctemplate * ptemplate =
(Cdoctemplate *) m_templatelist.gethead ();
// Call the opendocumentfile () function of the Document Template
Ptemplate-> opendocumentfile (null); 827 rows
}
Cdocument * csingledoctemplate: opendocumentfile (...)
{
// Create a new document object
Pdocument = createnewdocument (); 112 rows
// Create a new framework object and Framework Window
Pframe = createnewframe (pdocument, null); 132 rows
}
Cframewnd * cdoctemplate: createnewframe (...)
{
// Create a new framework object
Cframewnd * pframe = row 264
(Cframewnd *) m_pframeclass-> Createobject ();
// Create a new Framework Window
If (! Pframe-> loadframe (m_nidresource,...); 277 rows
}
Call loadframe to generate the wm_create message. In the message processing function
In oncreate (), call the createview () function to create the view object and
View window.
Wm_create message is generated when the View window is created. In the message processing function
In oncreate (), the addview () function is called, and the document and view are saved to each other.
The address of the peer.
Relationship between Class 3 and Class (Object and object)
Cwinapp
|-> M_pdocmanager (cdocmanager *)
|-> M_templatelist (csingledoctemplate *)
|-> M_ponlydoc (cdocument *)

|-> M_pmainwnd (cframewnd *)
|-> M_pactiveview (cview *)
|-> M_pdocument (cdocument *)
|-> M_viewlist (cview *)
Conclusion:
The relationship between various classes of MFC is generated by including the peer address.
4. Order of command messages
View-> document-> frame-app
2. Multi-document view application
1 concept
Manage multiple documents at the same time
2 related classes
Cwinapp
Cmdiframewnd-main framework class of Multi-document (1 Object)
Cmdichildwnd-Sub-framework class of Multi-document (multiple objects)
Cview/cdocument-View/Document (multiple objects)
Cmultidoctemplate-Multi-Document Template Class
Cmultidoctemplate (
Uint nidresource, // * Resource ID of the sub-Framework Window
Cruntimeclass * pdocclass, // class information during document class Runtime
Cruntimeclass * pframeclass, // * runtime class information of the sub-framework
Cruntimeclass * pviewclass // runtime class information of the View class
);
In multiple documents, the main frame window and the child frame window have their own menus and icons respectively.
The objects and windows of the main framework are created by yourself, while the sub-frameworks, documents, and views are used
Template creation.
In multiple documents, there must be at least two menu items in the main frame window.
3. Create (example of Data Synchronization for multiple views)
3.1
"New View" menu
-Only create a sub-framework and View window. If the document is not created, use the current active view.
The corresponding document. A document corresponds to multiple view Windows.
"New" menu
-Execute the onfilenew () function. All documents, sub-frameworks, and views are created.
3.2
On_control_reflect (en_chage, onenchange) // message ing macro,
Capture the message that the view content changes. In the message processing function:
3.2.1 Save the content of the activity View to the document
3.2.2 notify other views to obtain the latest data from the document
3.3
Rewrite the cview: onupdate () function. In the function, obtain the document data and display
To the current view.
The relationships between the four types are similar to those in a single document...

3. MFC plotting
1. Related Classes
1.1 drawing equipment
CDC-the parent class of the drawing equipment class, which encapsulates General drawing devices, such:
Printers, monitors, etc.
Cwindowdc-the parent class is the CDC class, which encapsulates the specified window. Including window
Customer zone and non-customer zone.
Cclientdc-the parent class is the CDC class, which encapsulates the customer zone of the specified window.
Cpaintdc-the parent class is the CDC class, which encapsulates the customer zone of the specified window. This class
It can only be used in the wm_paint message processing function.
Cmetafiledc-the parent class is the CDC class. Save the drawing command. You can
And re-execute these draw commands.

1.2 Drawing Object Class

2. Use
2.1 use of the CDC class
2.1.1 create
Virtual bool createdc (
Lpctstr lpszdrivername, // name of the device driver
Lpctstr lpszdevicename, // device name
Lpctstr lpszoutput, // null
Const void * lpinitdata // initialization parameter
);
If the device is a display, ("display", null );
2.1.2 drawing or output text
Example: textout () function, output text
2.1.3 Delete
CDC: deletedc ()
2.2 Use of cmetafiledc class
2.2.1 create
Cmetafiledc: Create
2.2.2 plotting
...
2.2.3 close and return the hmetafile handle
Cmetafiledc: Close
2.2.4 re-execute the drawing command
CDC: playmetafile (hmetafile)
2.2.5 Delete
Deletemetafile (hmetafile)

-----------------------------

1. MFC plotting
1. drawing equipment
2. drawing objects
Cgdiobject class-the parent class of all drawing objects, abstracted parent class, not directly used
Cpen class-paint brush
Cbrush-image brush
Cfont-font

Cbitmap class-bitmap
Crgn class-region (areas that can be computed)
Cpalette class-color palette
3. Use
3.1 steps for using paint brushes, paint brushes, and Fonts
3.1.1 create or construct a Drawing Object
3.1.2 select the Drawing Object to the Current Device
3.1.3 use a Drawing Object
3.1.4 restore to the default Drawing Object
3.1.5 delete a Drawing Object
3.2 usage steps of Bitmap
3.2.1 create a DC compatible with the current window DC
CDC: createcompatibledc
3.2.2 load Bitmap (associate a bitmap object with a bitmap Resource)
Cbitmap: loadbitmap
3.2.3 import bitmap objects to compatible DC
CDC: SelectObject
3.2.4 copy bitmap from compatible DC to current DC
CDC: bitblt
CDC: stretchblt-images that can be stretched or compressed
3.2.5 plotting compatible with DC default bitmap objects
3.2.6 Delete DC-compatible and bitmap objects
This function allows you to add background images to a view.

3.3 procedure for using the crgn class
3.3.1 create a geometric Area
Crgn: createxxx
3.3.2 perform operations on the two geometric areas to the complex geometric Areas
Crgn: combinergn
Note: operations can be performed multiple times.
3.3.3 fill the geometric Area
CDC: fillrgn
3.3.4 fill in the border of the geometric Area
CDC: framergn
Application:
Cwnd: setw.wrgn-set irregular window shape

Cpalette class-color palette to reduce the space occupied by bitmap.
RGB (0 ~ 255, 0 ~ 255, 0 ~ 255), 24 real color.
800*600 bitmap, true color bitmap: 800*600*3 bytes
Color Palette: 3*48 48 color bitmap: 800*600*1 (index value in the color table) + 3*48
2. Examples of MFC plotting
1 Analysis
1.1 The image consists of a straight line, a rectangle, and an ellipse. The data required is the start and end points.
1.2 messages processed:
1.2.1 lbuttondown-record the starting coordinate of the image and start to draw a line
1.2.2 mousemove-determine if you start to draw a line, erase the original line
Draw a line in the same position as the screen color.
1.2.3 lbuttonup-end of draw line.
2. Implementation
2.1 How does drawing solve screen flickering?
Method: 1. Use dual-buffer plot 2. Partial refresh instead of full refresh
2.2 OpenGL library, Direct Draw
3. File Operations of MFC
1. Related Classes
Cfile class-Win32 API that encapsulates file handles and related operations.
Cfilefind class-file search.
2 usage of the cfile class
2.1 Open or create a file
Cfile: Open
2.2 file read/write
Cfile: read/write
Note: It is usually placed in the exception handling structure. Pay attention to the current
File pointer.
2.3 close a file
Cfile: Close
2.4 get/set the File status (attribute)
Cfile: getstatus/setstatus
3 use of the cfilefind class
3.1 start searching. The returned value indicates whether to find the file.
Cfilefind: findfile
3.2 search for it. (obtain the information of the first file and return whether the next file exists)
Cfilefind: findnextfile
3.3 obtain and determine file information
Cfilefind: getxxx and isxxx
3.4 close search
Cfilefind: Close
Job:
1. Use the cfilefind class to find and output files and directories under the root directory of the C drive.
2. Use the cfilefind class to search for and output all files and directories in drive C.

------------------------------------------

1. serialization
1 concept
Writes data to or reads data from a binary stream in sequence.
2 related classes
Cfile class
Carchive class-Provides operations to read and write specific files, instead of cfile: read/write
Advantages: 1. You can set the buffer size. 2. It is more convenient to read and write various data types.
3. Use
3.1 open or create a file
3.2 file read/write operations
3.2.1 define carchive class objects
3.2.2 use ">", "<" to read and write files
3.2.3 disable carchive objects
3.3 close a file
Serialization of binary objects (6th mechanisms of MFC). Some books refer to serialization.
1 concept
Serialized object (at least support for runtime class information is required)
Write the class information of the object and the member variables of the object in sequence as a binary stream.
The process of getting to a file is called a serialized object.
Deserialization object (at least support for dynamic creation)
Read the class information from the file in binary mode, create an object, and then read
The saved value initializes the newly created object. This process is called a deserialization object.
2. Implementation
2.1 define classes that support serialization
2.1.1 derived from the cobject class
2.1.2 add serialization declarations and implementation macros
2.1.3 rewrite the cobject: serialize function. In the function
Member variable serialization.
2.2 Use
The serialization process of objects is similar to that of common variables. Note that when reading and writing, ">"
"<" The function parameter is the object address.
3. Implementation Principle
3.1 expand macro
3.1.1 a dynamic macro and a friend Function
3.1.2 The function of the struct type Variable _ init_cstudent is
Save the address of the current class runtime class information to the linked list of the application.
Struct afx_classinit
{
// Constructor
Afx_classinit (cruntimeclass * pnewclass)
{
Afxclassinit (pnewclass );
}
};
Void afxapi afxclassinit (cruntimeclass * pnewclass)
{
// Obtain the module status information of the application
Afx_module_state * pmodulestate =
Afxgetmodulestate ();
Afxlockglobals (crit_runtimeclasslist );
// Save the address of the current class runtime class information to the linked list of the application
Pmodulestate-> m_classlist.addhead (pnewclass );
Afxunlockglobals (crit_runtimeclasslist );
}
3.2 Object serialization Process
Carchive & afxapi operator <(carchive & AR, const cobject * POB)
{
Ar. writeobject (POB );
Return Ar;
}
Ar. writeobject (POB)
{
// 1 obtain the address of the current class runtime class information
Cruntimeclass * pclassref = POB-> getruntimeclass ();
// 2 Write the version number, class name length, and Class Name of the class to the file.
Writeclass (pclassref );
// 3 Write the name and age of the class member variables to the file in sequence
(Cobject *) POB)-> serialize (* This)
{
Cobject: serialize (AR );
If (AR. isstoring ())
{
Ar <m_strname <m_nage;
}
Else
{
Ar> m_strname> m_nage;
}
}
}
Writeclass (pclassref)
{
Pclassref-> store (* This); 252 rows
}
Pclassref-> store (* This)
{
// Obtain the class name String Length
Word nlen = (Word) lstrlena (m_lpszclassname );
// First, write the version and Name Length of the class.
Ar <(Word) m_wschema <nlen;
// Write the class name string
Ar. Write (m_lpszclassname, nlen * sizeof (char ));
}
3.3 deserialization of Objects
Carchive & afxapi operator> (carchive & AR, cstudent * & POB)
{
POB = (cstudent *)
Ar. readobject (runtime_class (cstudent ));
Return Ar;
}
Ar. readobject (runtime_class (cstudent ))
{
// 1 read the class name from the file, and find the runtime information in the traversal table
Cruntimeclass * pclassref = readclass (...); 125 rows
// 2 create an object using the variable of the runtime class information
POB = pclassref-> Createobject (); 150 rows
// 3 read the member variable from the file to initialize the new object
POB-> serialize (* This); 161 rows
{
...
}
}
Cruntimeclass * pclassref = readclass (...)
{
Pclassref = cruntimeclass: load (); row 301
}
Cruntimeclass: load (...)
{
// Read the class version, class name length, and class name from the file in sequence
Ar> wtemp; * pwschemanum = wtemp;
Ar> nlen;

If (nlen> = _ countof (szclassname) |
Ar. Read (szclassname, nlen * sizeof (char ))! = Nlen * sizeof (char ))
{
Return NULL;
}
// Save the class name in the szclassname Array
Szclassname [nlen] = '\ 0 ';
// Obtain the module status information of the application
Afx_module_state * pmodulestate = afxgetmodulestate ();
Afxlockglobals (crit_runtimeclasslist );
// Traverse the linked list of variable addresses in the module state information that saves the class information during running of each class
For (Pclass = pmodulestate-> m_classlist; Pclass! = NULL;
Pclass = Pclass-> m_pnextclass)
{
Name of the comparison class. If it is found in the linked list, the variable address is returned.
If (lstrcmpa (szclassname, Pclass-> m_lpszclassname) = 0)
{
Afxunlockglobals (crit_runtimeclasslist );
Return Pclass;
}
}

}

3. MFC dialog box
1 category
Mode and non-Mode
2 related classes
Cdialog class-the parent class is cwnd, which is essentially a window with its own resources,
Easy drag and drop controls. The parent class of all dialog box classes.
Ccommondialog class-General dialog box class, including file dialog box and color dialog box
Font dialog box, find and replace dialog box, and print Settings dialog box.
Cpropertypage class-property page dialog box.
3. Create a dialog-based application using the MFC class in the Win32 Project
3.1 mode dialog box
3.1.1 create and display
Cdialog: domodal ()
3.1.2 disable
Cdialog: onok/oncancel
3.1.3 dialog box Initialization
Cdialog: oninitdialog
3.2 non-mode dialog box
3.2.1 create and display
The process of creating and displaying a framework window is similar to that of a General Framework Window.
3.2.2 disable
To close the non-mode dialog box, the programmer must handle it.
1 rewrite the cdialog: onok and oncancel virtual functions.
In the function, destroywindow ()
2. Rewrite the cwnd: postncdestroy () virtual function. In the function,
Delete this;
3.2.3 dialog box Initialization
Cdialog: oninitdialog
Think: Why do programmers not need to destroy the mode dialog box?
Tip: Follow the domodal () function execution process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.