Use of pointers in MFC applications

Source: Internet
Author: User
Tags function prototype

Original address: http://www.vckbase.com/index.php/wv/459

1) Get doc pointer in view

2) Get mainframe pointer in app

3) Get mainframe pointer in view

4) Get the view (established) pointer

5) Get the current document pointer

6) Get the status bar with the tool bar pointer

7) Get the status bar and toolbar variables

8) Get the menu pointer in mainframe

9) Get the application class in any class

10) Pointer to the view class from the document class (1)

11) Get the document template pointer in the app

12) Get the document class pointer from the document template

13) Get the document template pointer in the document class

14) Pointer to the view class from the document Class (2)

15) Get a pointer to another view class from one view class

VC programming for students who have just begun to learn, the biggest obstacle and problem is the message mechanism and pointer acquisition and operation. In fact, these content is basically every VC learning reference book must be said, and through MSDN a lot of problems can be solved.

The following text is mainly personal in the programming of the use of pointers to some of the experience, said the wrong place please correct me.

Generally we use the framework of the VC provided by the wizard-generated MFC APP Wizard (EXE) framework, whether it is a multi-document or single document, there are pointer acquisition and operational issues.

The following section is mainly about the general framework and then the use of pointers in multi-threading. The class to use requires a header file that contains the response. First of all generally get this class (see, Document, dialog box support) instance pointer this, for the purpose of this, mainly through the function in the class to other classes or functions to send pointers, in order to operate in non-class and use the functions in this class.

1) Get Doc pointer view source print in view? 1. Cyousdidoc *pdoc=getdocument (); One view can only have one document.

2) Get mainframe pointer in app

The m_pMainWnd variable in CWINAPP is the pointer to mainframe.

can also: View source print? 1. CMainFrame *pmain = (CMainFrame *) AfxGetMainWnd ();

3) Get mainframe pointer view source print in view? 1. CMainFrame *pmain= (Cmaimframe *) AfxGetApp ()->m_pmainwnd;

4) Get view (established) pointer view source print? 1. CMainFrame *pmain= (Cmaimframe *) AfxGetApp ()->m_pmainwnd; 2. Cyouview *pview= (Cyouview *) Pmain->getactiveview ();

5) Get the current document pointer to view source print? 1. CDocument * Pcurrentdoc = (CFRAMEWND *) m_pmainwnd->getactivedocument ();

6) Get the status bar with the toolbar pointer view source print? 1. CStatusBar * pstatusbar= (CStatusBar *) AfxGetMainWnd ()->getdescendantwindow (Afx_idw_status_bar); 2. CToolBar * ptoolbar= (CToolBar *) AfxGetMainWnd ()->getdescendantwindow (Afx_idw_toolbar);

7) If the frame joins the toolbar and the status bar variable can also view source print? 1. (CMainFrame *) GetParent ()->m_wndtoolbar; 2. (CMainFrame *) GetParent ()->m_wndstatusbar;

8) In mainframe get menu pointer view source print? 1. CMenu *pmenu=m_pmainwnd->getmenu ();

9) Get the application class in any class

obtained by using the MFC global Function AfxGetApp ().

10) Get a pointer to the view class from the document class

I was from http://download.cqcnc.com/soft/program/article/vc/. Vc405.html learned, getting the view class pointer from a document the purpose is generally to control the positioning of multiple views of the same document, my experience especially the word processing ceditview when producing multiple view classes, this feature is very much needed.

The CDocument class provides two functions for locating a view class:

GetFirstViewPosition () and GetNextView () View source print? 1. Virtual POSITION getfirstviewposition () const; 2. Virtual cview* GetNextView (position& rposition) const;

Note: The parameters in GetNextView () are referenced in parentheses, so the post-execution value may change.

GetFirstViewPosition () returns the first view position (not a pointer to the view class, but a position type value), GetNextView () There are two functions: Returns a pointer to the next view class and changes the value of the passed-in position type parameter in the same way as a reference call. Obviously, in the test program, there is only one view class, so just call these two functions once to get Ctestview pointers as follows (you need to define a position structure variable to assist the operation): View source print? 1. ctestview* Ptestview; 2. POSITION pos=getfirstviewposition (); 3. Ptestview=getnextview (POS);

In this way, the pointer to the Ctestview class is ptestview. 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. But these statements are too simple to have too much commonality and security features; When you want to return a pointer to a specified class in multiple views as previously stated, we need to traverse all view classes until the specified class is found. When you determine whether a class pointer points to an instance of a class, you can use the IsKindOf () member function to check the rows, such as: View source print? 1. Pview->iskindof (Runtime_class (Ctestview));

You can check whether the PView refers to the Ctestview class.

With the above basics, we can already get pointers to any class from the document class. For convenience, we use it as a member function of a document class, and it has a parameter that represents the pointer to which class to get. Implemented as follows: View source print? cview* Ctestdoc::getview (cruntimeclass* pClass) 02. {    cview* PView;     position pos=getfirstviewposition ();    06.     while (pos!=null) {        pview=getnextview ( POS);         if (!pview->iskindof (pClass)) 09.         break;     } 11.    12.     if (!pview->iskindof (PClass)) {         AfxMessageBox ("Connt Locate the view.\r\n http://www.VCKBASE.com");         return NULL;     } 16.    17.     return PView; .}

This is determined by using the member function IsKindOf () of the view class two times, because there are three possible exits while loops:

1.pos is null, that is, the next view class does not exist for operation;

2.pView has met the requirements.

1 and 2 are satisfied. This is because the function of GetNextView () is to change the current view pointer to the position of a view and return the current view pointer, so POS is the position of the next view class for PView, and it is entirely possible that both Pos==null and PView meet the needs. When the desired view is the last view is the last view class as cited. Two judgments are therefore required.

Use this function in the following format (to get the Ctestview pointer as an example): View source print? 1. ctestview* ptestview= (ctestview*) GetView (Runtime_class (Ctestview));

Runtime_class is a macro that simply understands what it does: Convert the name of the class to CRuntimeClass as a pointer.

Coercion type conversions are also considered for security reasons, because pointer types from the same base class are mutually compatible. This type of coercion may not be necessary, but it can avoid some of the problems that may arise.

3. From one view class to get another view class pointer synthesis 1 and 2, it is easy to draw the view class to get pointers to each other method: is the document class as a relay, first use 1 method to get the pointer of the document class, and then 2 method, the document Class View positioning function to obtain another view class. Again, it can be implemented as a function:

(Suppose you want to get a pointer to another view class from Ctestaview) 01. cview* Ctestaview::getview (cruntimeclass* pClass) 02. {    ctestdoc* pdoc= (ctestdoc*) getdocument ();     cview* PView;     position pos=pdoc->getfirstviewposition ();     while (pos!=null) {        pView=pDoc-> GetNextView (POS);         if (!pview->iskindof (pClass)) 09.         break;     } 11.     if (!pview->iskindof (PClass)) {         AfxMessageBox ("Connt Locate the View.");         return NULL;     } 15.    16.     return PView; .}

This function compares to the GetView () in 2, one is the first sentence to get the document class pointer, and the second is the document class pointer before GetFirstViewPosition () and GetNextView () to indicate that they are the document class member functions.

With this function, when you want to get the Ctestbview pointer from Ctestaview, just the following: 1. ctestbview* ptestbview= (ctestview*) GetView (Runtime_class (Ctestbview));

11) for a single document can also be added to multiple document templates, but the general development of the use of MDI to develop a multi-document template, the method and the above view is very close to the approach, here a little explanation, if not clear, please review MSDN, (the following four (11, 12, 13, 14) Source: 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 get the first CDocTemplate object pointer.  1. POSITION getfirstdoctemplate () const; 2. CDocTemplate *getnextdoctemplate (POSITION & POS) const;

The second function returns a document template identified by the Pos. Position is a value that is defined by MFC for iterative or object pointer retrieval. With these two functions, the application can traverse the entire list of document templates. If the document template being retrieved is the last one in the list of templates, the POS parameter is set to NULL.

12) A document template can have multiple documents, and each document template retains and maintains a list of pointers to all corresponding documents.

Use the Cdoctemplate::getfirstdocposition function to get the position of the first document in the document collection associated with the document template and use the position value as the CDocTemplate: Getnextdoc parameters to iterate through the list of documents related to the template. The prototype function is: 1.  Viaual POSITION getfirstdocposition () const = 0; 2. Visual CDocument *getnextdoc (POSITION & rpos) const = 0;

If the list is empty, RpoS is set to null.

13) In the document, you can call cdocument::getdoctemplate to get a pointer to the document template. The function prototype is as follows: 1. CDocTemplate * Getdoctemplate () const;

If the document is not part of the document template management, the return value is null.

14) A document can have multiple viewports. Each document retains and maintains a list of all related viewports. CDocument::AddView connect a view to the document, add the view to the list of the view that the document is linked to, and point the document pointer to the document as it is viewed. MFC automatically calls this function when a newly created view object is connected to a document with File/new, File/open, windows/new, or Window/split commands, and the framework connects the document and the view through the document/view structure. Of course, programmers can also call this function according to their own needs.  1. Virtual POSITION getfirstviewposition () const; 2. Virtual CView * GetNextView (POSITION &rposition) cosnt;

An application can call cdocument::getfirstviewposition to return the position of the first view in the list of viewports associated with the calling document, and call Cdocument::getnextview to return the view at the specified position, and set the value of Rpositon to the position value of the next view in the list. If the found is treated as the last view in the list, the rposition is set to null.

15) Get a pointer to another view class from one view class

This application in the multi-vision application Many see, generally if oneself in the main program or the main frame in the variable notation can also be obtained, there is more common is to use the document class as a relay, the document Class View traversal positioning, to obtain another view class. This feature is available from the 10th item in this article.

Most of this information is obtained from the Web and MSDN, and the document is written so that you don't have to look for it, list titles, and be more actionable.

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.