The relationship between the cfiledialog of MFC and several classes

Source: Internet
Author: User

After learning MFC for a while, I used to imitate other people's programs. I don't know why, but these problems must be solved later. It is always impossible to get through it in a confused way, so I searched some functions on the Internet and sorted them out based on the understanding between them. The main idea is to sort out the ideas.

I. cfiledialog

Reference: http://blog.sina.com.cn/s/blog_4618b1720100edq8.html

1: Construction

Cfiledialog is a commonly used dialog box class for opening graphics. Its General constructor form is as follows:

Cfiledialog (boolBopenfiledialog, LpctstrLpszdefext= NULL, lpctstrLpszfilename= NULL, DWORDDwflags= Ofn_hidereadonly | ofn_overwriteprompt, lpctstrLpszfilter= NULL,
Cwnd *Pparentwnd= NULL );

The following describes the meanings of some parameters:

When bopenfiledialog is set to true, the dialog box is displayed. If it is set to false, the dialog box is saved.

The second lpszdefext is the default suffix, and lpszfilename is the default file name, which can constitute the default open and save objects. Of course, their separate combinations will be more diversified.

The third dwflags parameter is a very important parameter. It can be set to various methods for opening a document. The most common one is to select multiple documents or single documents.

The fourth lpszfilter is also an important parameter, which indicates the document to be filtered.

The fifth pparentwnd is not commonly used. The default value is null.

2: Modify

You can use the m_ofn struct to modify the created cfliedialog. It has a very important parameter lpstrfile, which is used to save some cache information other than the dialog itself. Because the built-in File Cache length of cfiledialog is only 200, a file cache is set to ensure the effectiveness of the cache (Note: The value must be initialized to 0 first.). Other commonly used tools include nmaxfile, which can be obtained through the vassitx prompt.

3: Application

Cfiledialog defines many useful functions, which can be obtained through the vassitx prompt. Note thatWhat he defined here or is commonly used is expressed in bold..

Common functions include:

Cstring cfiledialog: getpathname () to obtain the complete file name, including the directory name and extension c: \ test \ test1.txt

Cstring cfiledialog: getfilename () to obtain the complete file name, including the extension test1.txt.
Cstring cfiledialog: getextname () to obtain the complete file extension, for example, txt
Cstring cfiledialog: getfiletitle () to get the complete file name, excluding the directory name and extension such as test1
Position cfiledialog: getstartposition () gets the first file location when multiple files are selected.
Cstring cfiledialog: getnextpathname (Position & Pos) gets the next file location when multiple files are selected, and returns the current file name at the same time. However, you must have already called position cfiledialog: getstartposition () to obtain the initial position variable.

Note: getstartposition () and getnextpathname () must be performed only when multiple documents are selected for dwflags. Their meaning is to get the location of the first and next documents selected. If it is a single document, there will be no getnextpathname.

Ii. Relationship between several classes in MFC

Reference: http://www.vckbase.com/index.php/wv/459

1: big relationship
The first app class and cmainframe class are the first level, the following cchildframe class is the second level, and the doc class and View class are the third level. However, the app class is not a system with several other classes. It is the boss of overall management.The top-down relationship between level 1 and level 3 must pass through level 2, but the top-down relationship is unnecessary because global functions are used.
1) View class and Doc class
A: Get the doc class in the View class.

Cyousdidoc * pdoc = getdocument (); one view can only have one document.
Note: getdocument is called by the current activity view by default. If there are multiple views in the framework, you can call the getdocument function by selecting the view. In short, this is actually a member function of the View class.Understanding the relationship between classes and objectsVery important.

B: Get the View class in the doc class.

The cdocument class provides two functions for positioning the View class: getfirstviewposition () and getnextview ()

Getfirstviewposition () is used to return the position of the first view (not a view pointer, but a position value). getnextview () has two functions: return the pointer of the next View class and use the reference call method to change the value of the input position type parameter. Obviously, there is only one view class in the test program. Therefore, you only need to call these two functions once to obtain the ctestview pointer as follows (you need to define a position structure variable to assist in the operation ):
Ctestview * ptestview;
Position Pos = getfirstviewposition ();
Ptestview = getnextview (POS );
In this way, you can access the ptestview pointer of the ctestview class. after executing a few sentences, the variable Pos = NULL, because there is no next View class, naturally there is no position for the next View class. however, these statements are too simple to have strong versatility and security features. As mentioned earlier, when you want to return a pointer to a specified class in multiple views, we need to traverse all view classes until the specified class is found. When judging whether a class Pointer Points to an instance of a class, you can use the iskindof () member function to perform a row check, for example:
Pview-> iskindof (runtime_class (ctestview ));
You can check whether pview refers to the ctestview class.
With the above foundation, we can get any class pointer from the document class. For convenience, we use it as a member function of the document class. It has a parameter that indicates the class pointer to be obtained. The implementation is as follows:

CView* CTestDoc::GetView(CRuntimeClass* pClass){CView* pView;POSITION pos=GetFirstViewPosition();while(pos!=NULL){pView=GetNextView(pos);if(!pView->IsKindOf(pClass))break;}if(!pView->IsKindOf(pClass)){AfxMessageBox("Connt Locate the View.\r\nhttp://www.VCKBASE.com");return NULL;}return pView;}

Two View class member functions iskindof () are used to determine whether to exit the while loop. There are three possible reasons:
1. If POS is null, no view class exists;
2. pview has met the requirements.
1 and 2 are the same. This is because the getnextview () function is to change the current view pointer to the position of a view and return the current view pointer. Therefore, POS is the position of the next View class of pview, it is possible that both Pos = NULL and pview meet the requirements. When the required view is the last view and is the last view class, it is cited as follows. Therefore, two judgments are required.
Use this function in the following format (taking the ctestview pointer as an example ):
Ctestview * ptestview = (ctestview *) getview (runtime_class (ctestview ));
Runtime_class is a macro. It can be understood simply as a function of converting the class name to cruntimeclass as a pointer.
As for forced type conversion, it is also for the sake of security, because the pointer types from the same base class are mutually compatible. This forced type conversion may not be necessary, but it can avoid some possible troubles.

C: View class to obtain View class

From a View class to get another view class pointer synthesis 1 and 2, it is easy to obtain the View class mutual pointer acquisition method: IsUse document classes for transitFirst, get the pointer of the document class using the method 1, and then use the method 2 to get another view class using the view locating function of the document class. Similarly, you can implement a function:
(Assume that you want to obtain a pointer to other view classes from ctestaview)

Cview * ctestaview: getview (cruntimeclass * Pclass) {ctestdoc * pdoc = (ctestdoc *) getdocument (); // the preceding document is used for intermediate transfer. Cview * pview; position Pos = pdoc-> getfirstviewposition (); While (Pos! = NULL) {pview = pdoc-> getnextview (POS); If (! Pview-> iskindof (Pclass) break;} If (! Pview-> iskindof (Pclass) {afxmessagebox ("connt locate the view."); return NULL;} return pview ;}

Compared with getview () in 2, this function adds the first sentence to get the document class pointer, and adds the document class pointer before getfirstviewposition () and getnextview, to indicate that they are document class member functions.
With this function, to obtain the ctestbview pointer from ctestaview, you only need:

Ctestbview * ptestbview = (ctestview *) getview (runtime_class (ctestbview ));


2): View class and mainframe class

A: View to obtain mainframe

This is where the lower-level gets the upper-level. Generally, you can only use global functions. Here we use cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd;

Or cmainframe * pmain = (cmainframe *) afxgetmainwnd ();

B: Obtain the view in mainframe.

Here, the upper-level gets the lower-level, and you can use the upper-level functions. M_pmainwnd is used here.

Cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd; // obtain the first level

Required ichildwnd * pchild = pmainframe-> mdigetactive (); // obtain Level 2

Cyouview * pview = (cyouview *) pchild-> getactiveview (); // obtain level 3


3): Doc class and mainframe class

A: Obtain the mainframe class in the doc class.

Similarly, you can only use global functions. Here cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd;

Cmainframe * pmain = (cmainframe *) afxgetmainwnd ();

B: Obtain the doc class in the mainframe class.

Cdocument * pcurrentdoc = (cframewnd *) m_pmainwnd-> mdigetactive ()-> getactivedocument ();


4): obtain the application

Obtained by using the global function afxgetapp () of MFC

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.