Method for obtaining various types of pointers in the MFC

zhaozj2021-02-16  90

Method for obtaining various types of pointers in the MFC

To get a control pointer in another Form View class, first get the view pointer is a difficult point, the view pointer is easy to use the getDLGITEM function to get the control pointer

A variety of view pointers are obtained as follows 1) Get a DOC pointer 2 in the view) Get the mainframe pointer in the app to obtain the mainframe pointer 4 in the view) Get the VIEW (established) pointer 5) Get the current document pointer 6) Get the status bar Getting Status Bar Pointer 7) Getting Status Bar and Tool Variables 8) Getting Application Class 10 in any class 10) Pointers (1) 11) from the document class to get a document in the APP Template Pointer 12) Getting Document Type Pointer 13 from Document Template) A Document Template Pointer 14 in the document class) A pointer (2) 15 that acquires the view class from a document class. Pointer of each view in the segmentation view

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) Get the current document 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 (AFX_IDW_TOOLBAR);

7) If you join the toolbar and status bar variable in the framework, you can do this (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. CDocument class provides two functions for the positioning of the view class: GetFirstViewPosition () and GetNextView () virtual POSITION GetFirstViewPosition () const; virtual CView * GetNextView (POSITION & rPosition) const; Note: GetNextView () parentheses are parameters Reference mode, therefore 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 only need to call the two functions to get the CTestView pointer as follows (you need to define a Position structure variable to assist the operation): CTestView * PtestView; position pos = getFirstViewPositionPosition (); 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;}) Break;

IF (! pView-> iskindof (pclass) {AFXMessageBox ("Connt Locate the view./r/n http://www.vckbase.com"); return null;}

Return PView;

Among them, the members of the two view classes are judged because there are three possibilities for exiting the While loop:

1.pos is NULL, that is, there is no future view class for operation; 2. PVIEW has already 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. Similarly, it can be implemented as a function: (Supplementation to get the 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;} This function is more than the first sentence than the first sentence to obtain a document pointer. Second, add a document pointer before getFirstViewPosition () and getNextView () to represent them. Is a document class 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 use CWINAPP :: getFirstDoCTemPDStion to get the location of the first document template for the application; use this value to call the cDoctemplate object pointer with this value to get the first CDOCTemplate object pointer. Position getFirstDoCtemplate () const; cdoctemplate * getNextDocTemplate (Position & POS) const; second function returns a document template identified by 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.

Take me:

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 is: viaual position getfirstdocposition () const = 0; Visual CDocument * getnextddoc (position & rpos) const = 0;

If the list is empty, the RPOS is set to NULL.

13) You can call CDocument :: getDocTemplate in the document to get pointers to this 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, RPSITION is set to NULL.

15) Note from one view class to the pointer of another view class This application has many views in the multi-view application. Generally, if you do a good job in the main program or the main frame, you can also get, there is more general purpose. Using a document class, it is translated by the document class, and another view class is obtained. This feature can be obtained from item 10 of this article.

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

New Post(0)