Usage of pointers in the MFC application view Framework Document Status Bar

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 in the app
The m_pmainwnd variable in cwinapp is the mainframe pointer.
Alternatively, you can: 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 (afx_idw_status_bar );
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
Obtain it using the global function afxgetapp () of MFC.

10) obtain the pointer of the View class from the document class
I'm from the preview class pointer. Generally, in order to control the positioning of multiple views in the same document, my experience, especially when the Text Processing ceditview generates multiple view classes, this function is very required.
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, you can access the ptestview pointer of the ctestview class. 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 refers to the 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/n http://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, no view class exists;
2. pview has met 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.
Use this function in the following format (taking the ctestview pointer as an example): ctestview * ptestview = (ctestview *) getview (runtime_class (ctestview ));
Runtime_class is a macro. It can be understood simply as a function of converting 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 adds the document class pointer before getfirstviewposition () and 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 also be added to a single document. However, in general development, multiple document templates can be developed using the MDI method. The method is very similar to the method for obtaining the preceding view, for details, refer to msdn (the following four sources are as follows (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 first cdoctemplate object pointer. Position getfirstdoctemplate () const;
Cdoctemplate * getnextdoctemplate (Position & Pos) const;

The second function returns the document template identified by the POS. 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.

 

 

 

How can I obtain their respective pointers in the document window framework?

First, there is a getdocument () function in the View class, and the document pointer can be obtained through this function in an attempt.
For a multi-document program, it has several sub-window view Windows and multiple corresponding document classes.
Obtain the pointer in the main window of the application and save the pointer in the cwindthread: m_pmainwnd. You can call afxgetmainwnd to implement

Afxgetapp () points to a single cwinapp Object Pointer of the program. The returned pointer can be used to access application information, such as the master message scheduling code and top-level window.
Hinstance AfxGetInstanceHandle () returns the hinstance value of the current instance of the application. If it is called from the DLL connected to the usrdll version of MFC, the return value represents the DLL.

This function allows you to obtain the instance handle of the current application,

The afxgetresourcehandle () application calls the hinstance part of the default resource instance.
Use the hinstance handle returned by this function to directly access the application handle, for example, when findresource is called

If the primary frame pointer is obtained in the MDI live SDI application, it is easier to obtain other useful information about the window pointer,

Cmainframe: getactivedocument () Get the pointer to the current activity document

Positon Pos = getfirstviewpostion () in the doc class ()

M_pmainwnd-> getactivedocument ()

Getfirstdoctemplate getnextdoctemplate in the application to obtain the document template object
The document list is obtained by the getfirstdocposition getnextdoc function managed by the cdoctemplate class.
View list cdocument class to manage, getfirstviewposition getnextview function to obtain

Use afxgetmainwnd () in the cwinapp or directly use the m_pmainwnd pointer in the application.
Scientific research obtains cchildframe through afxgetmainwnd ()-> getmdiactive or-> getactiveframe
Single-document scientific research obtains document pointers through afxgetmainwnd ()-> getactiveview ()-> getdocument ()
In multiple documents, use afxgetmainwnd ()-> mdigetactive ()-> getactiveview ()-> getdocument
That is, first obtain the framework pointer, then obtain the view pointer from the Framework pointer, and then obtain the document pointer from the view pointer.

For scientific research in cmainframe, obtain theapp pointer through afxgetapp. MDI obtain the sub-frame pointer through mdigetactive or getactiveframe.
If SDI uses getactivevie-> getdocument MDI mdigetactive ()-> getactiveview ()-> getdocument ()
SDI directly getactiveview get view pointer MDI mdigretactive-> getactiveview

Get theapp getparentframe () in cchildframe afxgetapp to get the main framework getactiveview

Cdocument afxgetapp afxgetmainwnd

6) obtain 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 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.

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.