MFC Single document Add multiple views

Source: Internet
Author: User
Tags textout

Ext.: http://ffwmxr.blog.163.com/blog/static/66372722201001605539213/

Multi-View is one of the most commonly used technologies in VC development, and there are two ways to implement multiple views of single document:

1) using the Technology of view segmentation (implemented with CSplitterWnd ), split the window into multiple parts, each showing a different view, this technology is relatively simple to implement, and the relevant information is also many.

2) Multiple views are associated with a document, and the window displays the entire view.

The second implementation is more complex than the first one, and here is a detailed implementation method.

Step 1: Use VC 6.0 to create a new project named: MultiView. In addition to selecting Single document properties , everything uses the "default" method. So you can get five classes:CMainFrame ,cmultiviewapp,cmultiviewdoc,Cmultiviewview, and CAboutDlg;

Step 2: Create a new view, add a new MFC Class (Insert->new Class), the base class is CView (or CView derived subclasses, such as CEditView, etc.). The name of the class is Canotherview, which is the new view, and the implementation of the GetDocument is added for canotherview :

cmultiviewdoc* canotherview::getdocument ()

{

Return (cmultiviewdoc*) m_pdocument;

}

Step 3: Add member variables in Cmultiviewapp to record these two views:

Private

cview* M_pfirstview;

cview* M_panotherview;

Add a menu item "View" to the Program menu idr_mainframe , the menu item has two submenus "view One" and "View Two", add the corresponding function (void Cmultiviewapp:: Onshowfirstview () and void Cmultiviewapp:: Onshowsecondview ());

Step 4: Create a new view: Add code in BOOL cmultiviewapp:: InitInstance ():

.......

To create a new view

cview* M_pactiveview = ((cframewnd*) m_pMainWnd)->getactiveview ();

M_pfirstview = M_pactiveview;

M_panotherview = new Canotherview ();

Implementing Document and view associations

cdocument* M_pdoc = ((cframewnd*) m_pMainWnd)->getactivedocument ();

CCreateContext context;

Context.m_pcurrentdoc = M_pdoc;

To create a view implementation

UINT M_idforanotherview = Afx_idw_pane_first + 1;

CRect rect;

M_panotherview->create (Null,null,ws_child,rect,m_pmainwnd,

M_idforanotherview,&context);

......

Step 5: The view has now been created and is already associated with the document. Now all you have to do is convert between views. Add the implementation code in void Cmultiviewapp:: Onshowfirstview ():

void Cmultiviewapp::onshowfirstview ()

{

Todo:add your command handler code here

UINT temp =:: GetWindowLong (M_panotherview->m_hwnd, gwl_id);

:: SetWindowLong (M_panotherview->m_hwnd, gwl_id,:: GetWindowLong (M_pfirstview->m_hwnd, GWL_ID));

:: SetWindowLong (M_pfirstview->m_hwnd, gwl_id, temp);

M_panotherview->showwindow (Sw_hide);

M_pfirstview->showwindow (Sw_show);

((cframewnd*) m_pMainWnd)->setactiveview (M_pfirstview);

((cframewnd*) m_pMainWnd)->recalclayout ();

M_pfirstview->invalidate ();

}

Add the implementation code in void Cmultiviewapp:: Onshowsecondview ():

void Cmultiviewapp::onshowsecondview ()

{

Todo:add your command handler code here

UINT temp =:: GetWindowLong (M_panotherview->m_hwnd, gwl_id);

:: SetWindowLong (M_panotherview->m_hwnd, gwl_id,:: GetWindowLong (M_pfirstview->m_hwnd, GWL_ID));

:: SetWindowLong (M_pfirstview->m_hwnd, gwl_id, temp);

M_pfirstview->showwindow (Sw_hide);

M_panotherview->showwindow (Sw_show);

((cframewnd*) m_pMainWnd)->setactiveview (M_panotherview);

((cframewnd*) m_pMainWnd)->recalclayout ();

M_panotherview->invalidate ();

}

Step 6: In order to demonstrate, here are different views given a tag, add the following code separately in the OnDraw method of Cmultiviewview and canotherview :

Pdc->textout (400,300, "first View");

Pdc->textout (400,320,pdoc->gettitle ());

And

Pdc->textout (400,300, "another View");

Pdc->textout (400,320,pdoc->gettitle ());

This is done, but there are 4 points in the implementation process:

1) in the implementation due to the use of the relevant classes, so where necessary to include the relevant header files, omitted here;Canotherview 's default constructor is protected and needs to be changed to public, or to provide a the method of the Canotherview object (because you want to create a view object);

2) Here is a sample code, the actual development can be achieved by reference to achieve the specific application you want to achieve (for example, the different view classes, the number of different, more importantly, the different implementation of business logic, etc.);

MFC Single document Add multiple views

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.