Use of an MFC application pointers: gouguijia
1) Get a DOC pointer in View
2) Get the mainframe pointer in the app
3) Get the mainframe pointer in View
4) Get View (established) pointer
5) Get the current document pointer
6) Get the status bar and toolbar pointer
7) Get the variable in the status bar and toolbar
8) Get menu pointer in mainframe
9) Get the application class in any class
10) Take the pointer of the view class from the document class (1)
11) Get a document template pointer in the app
12) Get document pointers from the document template
13) Get a document template pointer in the document class
14) Pointers to the view class from the document class (2)
15) Get another view of the map class from a view class
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 a DOC pointer in View
CyousDidoc * pdoc = getDocument (); one can have a document.
2) Get the mainframe pointer in the app
The m_pmainwnd variable in cwinapp is the pointer of the mainframe.
You can also:
CMAINFRAME * PMAIN = (CMAINFRAME *) AFXGETMAINWND ();
3) Get the mainframe pointer in View
CMAINFRAME * PMAIN = (cmaimframe *) AFXGetApp () -> 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) Get the status bar and toolbar 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 variables in the framework, you can
(CMAINFRAME *) getParent () -> m_wndtoolbar; (cmainframe *) getParent () -> m_wndstatusbar;
8) Get menu pointer in mainframe
CMenu * pmenu = m_pmainwnd-> getMenu ();
9) Get the application class in any class
Use the MFC global function AFXGetApp ().
10) Number of view class from the document class
I am from
http://download.cqcnc.com/soft/program/Article/vc/vc405.html learned, from the document to get the view class pointer to control the positioning of multiple views of the same document, my experience is especially This function is very desirable when the text processing CEDITVIEW is generated when generating multiple view classes. The CDocument class provides two functions for the location of the view 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 just call the two functions to get the CTESTVIEW's pointer as follows (you need to define a Position structure variable to assist the operation):
CTestView * PTestView; Position Pos = getFirstViewPosition (); PTestView = getNextView (POS); This can be reached the pointer PTestView of CTestView class. After the completion of the sentence, the variable pOS = null, because there is no next view class, nature The next view class POSITION. But these statements are too simple, do not have 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. To determine if a class pointer is pointed 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 is referred 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. The implementation is as follows:
CView * CTestDoc :: GetView (CRuntimeClass * pClass) {CView * pView; POSITION pos = GetFirstViewPosition (); while (! Pos = NULL) {pView = GetNextView (pos); (! PView-> IsKindOf (pClass)) if 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 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 that simply understands its role: transform the name of the class into 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 the pointer to other view clauses 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;}}}} {AFXMESSAGEBOX ("Connt Locate The View."); Return Null;} return } 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 the CTestaview, you only need to follow:
Ctestbview * ptestbview = (ctestview *) getView (runtime_class (ctestbview));
11) For a single document, multiple document templates can also be added, but general development uses MDI mode to develop multi-document templates, the method is very close to the acquisition method of the above view, here is explained, if not, please check MSDN (The following four content (11, 12, 13, 14) Source:
http://sanjianxia.myrice.com/vc/vc45.htm)
You can use CWINAPP :: GetFirstDocTemPsPost to get the location of the first document template for the application;
Use this value to call the CWINAPP :: GetNextDocTemplate function 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. 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 * 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 this document template. The function 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 relevant views. CDocument :: AddView will connect a view to the document, add the view to the list of documents, and point the viewed document pointer to the 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; 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 Returns the view of the specified location and place the RPOSION value as the Position value of the next view in the list. If the last looks found in the list, RPSITION is set to NULL.
15) Get another view of the map class from a view class
This application is a lot of views in multi-view applications. Generally, if you do a good job in the main program or the main frame, you can also get, and more general is to transfer with document class, with a document class. , Get another view class. 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.