How to obtain each other between document, view, window, and custom classes

Source: Internet
Author: User

1) Get the doc pointer in the view

2) obtain the mainframe pointer in the app

3) obtain the mainframe pointer in the view.

4) obtain the view (created) pointer

5) Get the pointer to the current document

6) Get the status bar and toolbar pointer

7) Get the status bar and toolbar Variables

8) obtain the menu pointer in mainframe.

9) obtain the application class from any class

10) Get the pointer of the View class from the document class (1)

11) Get the document template pointer in the app

12) obtain the document class pointer from the document template

13) Get the document template pointer in the document class

14) Get the pointer of the View class from the document class (2)

15) obtain the pointer of another view class from one view class

 

In VC programming, the biggest obstacle and problem for students who have just started learning is the message mechanism and pointer acquisition and operation. In fact, these contents are basically required for every VC learning tool book, and can be solved through many msdn problems. The following text mainly refers to some of my experiences in Pointer usage in programming. please correct me if it is inappropriate. Generally, the framework we use is the MFC App Wizard (exe) Framework generated by Wizard provided by VC. There are pointer acquisition and Operation Problems in both multi-document and single-document. The following section describes the general framework and then the usage of pointers in multithreading. The class to be used must contain the response header file. First, we generally obtain the instance pointer this, which is supported by the class (view, document, and dialog box). The purpose of this is to send a pointer to other classes or functions through functions in the class, this allows you to operate and use functions in a non-class.

1) Get the doc pointer cyousdidoc * pdoc = getdocument () in the view. Only one view can have one document.

2) obtain the mainframe pointer from the app. The m_pmainwnd variable in the cwinapp is the pointer to the mainframe. You can also: cmainframe * pmain = (cmainframe *) afxgetmainwnd ();

3) obtain the mainframe pointer cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd in the view;

4) obtain the view (created) pointer cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd; cyouview * pview = (cyouview *) pmain-> getactiveview ();

5) obtain the current document pointer cdocument * pcurrentdoc = (cframewnd *) m_pmainwnd-> getactivedocument ();

6) obtain the status bar and toolbar pointer cstatusbar * pstatusbar = (cstatusbar *) afxgetmainwnd ()-> getdescendantwindow (rows); ctoolbar * ptoolbar = (ctoolbar *) afxgetmainwnd () -> getdescendantwindow (afx_idw_toolbar );

7) if the toolbar and status bar variables are added to the framework, you can also (cmainframe *) getparent ()-> m_wndtoolbar; (cmainframe *) getparent ()-> m_wndstatusbar;

8) obtain the menu pointer cmenu * pmenu = m_pmainwnd-> getmenu () in mainframe ();

9) obtain the application class from any class using the global function afxgetapp () of MFC.

10) the pointer to the View class obtained from the document class is.
The cdocument class provides two functions for positioning the View class: getfirstviewposition () and getnextview () virtual position getfirstviewposition () const; virtual cview * getnextview (Position & rposition) const; note: the parameters in the getnextview () Brackets use the reference method, so the value may change after execution. 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, the pointer ptestview of the ctestview class can be obtained. 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 is a 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. Implementation: 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, the next View class does not exist. 2. pview meets 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. This function should follow the following format (taking the ctestview pointer as an example): ctestview * ptestview = (ctestview *) getview (runtime_class (ctestview); runtime_class is a macro, you can simply understand its function: Convert 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.

3. the integration of pointers 1 and 2 from one view class to the other, it is easy to obtain the method of mutual pointer acquisition between view classes: it is to use the document class as a transit, first use the method 1 to get the pointer to the document class, 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 you want to obtain a pointer to other view classes from ctestaview) cview * ctestaview: getview (cruntimeclass * Pclass) {ctestdoc * pdoc = (ctestdoc *) getdocument ();
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 the second is in getfirstviewposition () add a document class pointer before getnextview () to indicate that they are document class member functions. With this function, to obtain the ctestbview pointer from ctestaview, you only need to: ctestbview *
Ptestbview = (ctestview *) getview (runtime_class (ctestbview); 11) multiple document templates can be added to a single document. However, in general development, multiple document templates are developed using the MDI method, the method is very similar to the method used to obtain the preceding view. For details, refer to msdn (the following four sources (11, 12, 13, and 14: http://sanjianxia.myrice.com/vc/vc45.htm)

You can use cwinapp: getfirstdoctemplatepostion to obtain the location of the first document template registered by the application. Use this value to call the cwinapp: getnextdoctemplate function to obtain the pointer of the first cdoctemplate object. Position getfirstdoctemplate () const; cdoctemplate * getnextdoctemplate (Position & Pos) const; the second function returns
The specified document template. Position is a value defined by MFC for iteration or object pointer retrieval. With these two functions, the application can traverse the entire document template list. If the retrieved document template is the last in the template list, the POs parameter is set to null.

12) A document template can have multiple documents. Each document template retains and maintains a pointer list for all corresponding documents. Use the cdoctemplate: getfirstdocposition function to obtain the position of the first document in the document set related to the document template, and use the position value as the cdoctemplate :: getnextdoc parameters are used to repeatedly traverse the list of template-related documents. Function prototype: viaual position getfirstdocposition () const = 0; Visual
Cdocument * getnextdoc (Position & rpos) const = 0; if the list is empty, rpos is set to null.

13) in this document, you can call cdocument: getdoctemplate to obtain the pointer to this document template. The original function form is as follows: cdoctemplate * getdoctemplate () const; if the document does not belong to document template management, the return value is null.

14) a document can have multiple views. Each document retains and maintains a list of all associated views. Cdocument: addview connects a View to the document, adds the view to the list of associated views of the document, and points the document pointer to the document. When a file/New, file/open, Windows/new, or window/split command connects a newly created object to the document, MFC automatically calls this function, the framework associates the document with the view through the document/view structure. Of course, programmers can also call this function according to their own needs.
Virtual position getfirstviewposition () const; virtual cview * getnextview (Position & rposition) cosnt; the application can call cdocument :: getfirstviewposition returns the position of the first view in the list of views associated with the call document, and CALLS cdocument: getnextview to return the view at the specified position, set the value of rpositon to the position value of the next view in the list. If the result is regarded as the last view in the list, the rposition is set to null.

15) getting pointers from one view class to another. This application is widely seen in multi-view applications. Generally, if you do a variable mark in the main program or main framework, it can also be obtained. What is more common is to use the document class as a transit, and traverse and locate the document Class View to obtain another view class. This function can be obtained from the 10th items in this article.

Most of these materials are obtained from the Internet and msdn. This document is written so that you do not have to search for them, list titles, and make them more operable.

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.