The use of pointers in the MFC application

xiaoxiao2021-03-06  71

:: Home >> Document Center >> Online Magazine >> Multi-Document Interface (MDI) [Online Magazine No. 21]

Document Code Tool

  Original documentation This article is suitable for primary readers have read 8905 times]

Use of the application pointer MFC: gouguijia1) obtained Doc pointer in View 2) to obtain MainFrame pointer App 3) obtained MainFrame pointer in View 4) obtaining View (established) pointer 5) to obtain the current document pointer 6) Get Status Bar and Toolbar Pointer 7) Get Status Bar and Tool Variables 8) Getting Menu Pointer 9) In any class, get application class 10) Pointer (1) 11) from the document class. (1) 11) in APP A Document Template Pointer 12) Obtaining Document Type Pointer 13 from the Document Template) Gets a Document Template Pointer 14 in the document class) Pointer (2) 15 from the document class) Pointer (2) 15) From a view class to the pointer of the other Programming in VC For classmates who have just started learning, the biggest obstacles and questions are message mechanisms and pointers acquisition and operation. In fact, these contents are basically a must-talkive content per VC learning tool book, and it can be solved by MSDN. The following text is mainly some of the experiences used by the individual in the programming, please correct it. Generally, the framework we use is the Wizard-generated MFC App Wizard (EXE) framework provided by VC, whether it is a multi-document or a single document, there is a pointer acquisition and operation problem. The following content is mainly a general framework, and then use the pointer in multi-thread. The use of the class needs to include the header file of the response. First, this class (depending on, document, dialog box is usually obtained) instance pointer this, with this purpose, mainly can be made to other classes or functions to other classes or functions in non-books in non-books. The functions in this class. 1) Get the DOC pointer in ViewDidoc * pdoc = getDocument (); one can have a document. 2) obtaining MainFrame pointer CWinApp the m_pMainWnd variable is MainFrame pointer may be in the App: CMainFrame * pMain = (CMainFrame *) AfxGetMainWnd (); 3) obtained MainFrame pointer CMainFrame * pMain = (CmaimFrame *) AfxGetApp in View ( ) -> m_pmainwnd; 4) Get View (established) pointer cmainframe * pmain = (cmaimframe *) AFXGetApp () -> m_pmainwnd;

CyouView * pView = (CyouView *) pMain-> GetActiveView (); 5) to obtain the current file pointer CDocument * pCurrentDoc = (CFrameWnd *) m_pMainWnd-> GetActiveDocument (); 6) obtained with the toolbar Status bar pointer CStatusBar * pStatusBar = ( CSTATUSBAR *) AFXGETMAINWND () -> getDescendantWindow (AFX_IDW_STATUS_BAR);

CToolBar * ptoolbar = (ctoolbar *) AFXGETMAINWND () -> getDescendantWindow;

7) If you join the toolbar and status bar variable in the framework (cMAINFRAME *) getParent () -> m_wndtoolbar;

(Cmainframe *) getParent () -> m_wndstatusbar;

8) Get menu pointer cmenu * pMenu = m_pmainwnd-> getMenu (); 9) get an application class MFC global function AFXGetApp () obtained in any class. 10) From the document class, I get the pointer of the view class. I learned from http://download.cqcnc.com/soft/program/Article/vc/vc405.html, from the document to get the view class pointer to control the same document Multiple views of multiple views, my experience, especially text, CEDITVIEW, when generating multiple view classes, this feature is very needed. The CDocument class provides two functions for viewing class: getFirstViewPosition () and getNextView () Virtual position getfirstViewPosition () Const; Virtual CView * getnextView (position & rposition) const;

Note: The parameters in GetNextView () Brackets are referenced, so the execution rate may change. GetFirstViewPosition () is used to return the first view location (the returned non-view class pointer, but a position value), getNextView () has two functions: Returns the next view class pointer and change with reference calls. The value of the incoming Position type parameter. Obviously, in the Test program, there is only one view class, so you can get the two functions to get the CTestView pointer (you need to define a Position structure variable to assist the action): CTestView * PTestView;

Position POS = getFirstViewPosition ();

PTestView = GetNextView (POS);

In this way, you can go to the CTestView class pointer PTestView. After the completion of the sentence, the variable POS = null, because there is no next view class, there is no next view class Position. But these few statements are too simple, not too Strong versatility and security features; when you want to return a pointer to a specified class in multiple views, we need to traverse all view classes until you find the specified class. When it is determined if a class pointer points to an instance of a class, you can use the iskindof () member function to check, such as: pView-> iskindof (runtime_class (ctestView)); you can check if the PVIEW refers to whether it is a CTestView class. With the above foundation, we can already get any type of pointer from the document class. For convenience, we use a member function of a document class, which has a parameter that means which type of pointer to get. Implementation 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/n http://www.vckbase.com");

Return NULL;

}

Return PVIEW;

} Among the two views of the membership function iskindof () is used to determine because there are three possibilities that exit the While loop: 1.Pos is NULL, that is, there is no next view class 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 location of a view, and then return the current view pointer, so POS is the top of the PVIEW's top view class, which is all possible to be both pOS == null is PVIEW. . When the desired view is that the last view is the last view class. Therefore, two judgments are required. Use this function to follow the following format (to get the CTestView pointer as an example): ctestView * PTestView = (ctestView *) GetView (runtime_class (ctestView)); runtime_class is a macro, you can simply understand its role: Transform the name of the class For CruntimeClass as a pointer. As for the mandatory type conversion is also for security features, since the pointer type between the same base class is compatible with each other. This mandatory type conversion may not be necessary, but can avoid some cumbersome possible. 3. Number of pointers 1 and 2 from one view class, it is easy to obtain a method of obtaining a pointer between the view classes: It is to transfer the document class with a document class. Use 2 method to obtain another view class with the view positioning function of the document class. Again, you can implement a function: (Suppose you want to get from CTestaview points to other view clauses) 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;

} This function is more than the first sentence in this function and the first sentence to obtain a document pointer, and the other is before getFirstViewPosition () and GetNextView () add a document class pointer to indicate that they are document classes. Member function. With this function; when you want to get the CTestbView pointer from CTestaview, you just need to follow: ctestbview * ptestbview = (ctestView *) GetView (runtime_class (ctestbview)); 11) You can also add multiple document templates for single documentation, However, the general development uses the MDI mode to develop multi-document templates, which is very close to the acquisition method of the above view. Here is a slight explanation. If it is unclear, please refer to MSDN (11, 12, 13, 14) Source: http://sanjianxia.myrice.com/vc/vc45.htm) You can get the location of the first document template for the application with cwinapp :: getFirstDoCtemplatePost; use this value to call the cwinapp :: getnextDonommentemplate function, get The first CDOCTemplate object pointer. Position getFirstDoCtemplate () const; cdoctemplate * getnextdoctemplate (position & pos) const;

The second function returns a document template identified by the POS. Position is an MFC defined value for iteration or object pointer retrieval. Through these two functions, the application can traverse the entire document template list. If the retrieved document template is the last one in the template list, the POS parameter is set to NULL. 12) A document template can have multiple documents, each of which retains and maintains a pointer list of all correspondence documents. Get the location of the first document in the document collection related to the document template with the cdoctemplate :: getFirstDocposition function, and use the POSITION value as the parameters of CDOCTemplate :: getNextDoc to repeat the list of documents related to the template. The function of the function is: viaual position getfirstdocposition () const = 0;

Visual CDocument * getNextdoc (Position & RPOS) const = 0;

If the list is empty, RPOS is set to null. 13) You can call CDocument :: getDocTemplate in the document to get pointers to the document template. The function is as follows: cdoCtemplate * getDoctemplate () const; if the document does not belong to the document template management, the return value is NULL. 14) A document can have multiple views. Each document retains and maintains a list of all relevant views. CDocument :: AddView The view is connected to the document, and the view is added to the list of documents, and the document pointer will point to this document. When there is file / new, file / open, windows / new or window / split commands to connect a newly created object to the document, the MFC will automatically call the function, the framework will document through the document / depending structure. And look up. Of course, programmers can call this function according to their needs. Virtual position getfirstviewPosition () const;

Virtual CView * getNextView (position & rposition) cosnt;

The application can call CDocument :: getFirstViewPosition Returns the location of the first view in the list of views connected to the document, and calls cdocument :: getNextView to return to the specified location, and place the rpositon's value as the next one. The position value of the view. If the last looks found in the list, the RPSITION is set to null. 15) Get another view of another view class from a view class This application is a lot of views in the multi-view application, generally if yourself in the main program Or do a variable mark in the main frame, or it can be obtained, and more common is to transfer with document classes, and the document class is traversed, and another view class is obtained. This feature can be obtained from item 10 of this article. Most of these materials are obtained from online and MSDN. Write this document is to let everyone need to find, list titles, and more operability. Latest Reviews [Published Reviews] [Article Submission] See all comments Recommend to friends print

TO: How do you feel a little problem if (! Pview-> iskindof (pclass)) Break; should be if (pView-> iskindof (pclass)); (Ququs Posted 2004-9-28 14:53:00) Good work ! Thanks! (Musicfan Published on 2003-5-12 8:56:00) Create a constructor with document pointer parameters in the dialog box. (Uyeye Published on 2003-5-8 9:42:00) CView * ctestdoc :: getView (cruntimeclass * pclass) {cView * pView; position pos = getFirstViewPosition (); while (pos! = null) {pView = getNextView POS); if (! pView-> iskindof (pclass)) Break;}}}} (! pView-> iskindof (pclass)) {afxMessageBox ("Connt Locate the view./r/n http://www.vckbase.com "); return null;} Return PView;} How to think that there is a bit of problem with (! pView-> iskindof (pclass) Break ;? ? ? (StainLESSDW Published on 2003-5-5 22:42:00) This is not simple, you can create, you can pass this View / DOC pointer to the dialog box. Even if it is not a parameter, it can be obtained. Because the mainfrm pointer is globally: AFXGETMAINWND (), you can get a View / DOC pointer (Whaoye Published on 2003-5-5-3 21:18:00) WOUGUIJIA: How to Create the Mode dialog created in View in View Get a DOC's pointer? I hope to enlighten me, thank you! (SKY2002 Posted on 2003-5-3 11:22:00) ................................. .................. More ...

Copyright © 2004 VC Knowledge Base

转载请注明原文地址:https://www.9cbs.com/read-87823.html

New Post(0)