Create multiple views in a single document interface with VC + +

Source: Internet
Author: User

There are multiple views in a single document interface, and you can dynamically switch views as needed, which is the current, more popular interface style, which can meet the needs of many users in terms of operation and display. The main representative of this interface style is Outlook Express. With VC + + to achieve this style of interface has a certain degree of difficulty, the author of this issue has been studied, and summed up two kinds of implementation methods (these codes are in VC + + 6.0 debugging through), the use of key attention to the steps and implementation of ideas, do not rigidly adhere to the form of code

Method One: Static Create Toggle Method

Step Description:

1. Create all the view objects that you want to switch before the window is displayed, except for the view that is first displayed, and set to invisible when you create them.

cmywinapp::initinstance ()
{...
M_pviews[0] = pView1;
M_pviews[1] = (cview*) new CView2;
cdocument* Pcurrentdoc = (cframewnd*) m_pmainwnd)->getactivedocument ();
//Initialize create context-related pointers
CCreateContext newcontext
Newcontext.m_pnewviewclass = NULL;
Newcontext.m_ Pnewdoctemplate = NULL;
Newcontext.m_plastview = NULL;
Newcontext.m_pcurrentframe = NULL;
Newcontext.m_pcurrentdoc = Pcurrentdoc;
//Initial activation depending on the ID of Afx_idw_pane_first,
//Add this value to the newly created view, note that CSplitterWnd cannot be used in this way
UINT viewid[2];
Viewid[1] = afx_ Idw_pane_first + 1;
CRect rect (0, 0, 0, 0);
for (int nview=1; nview<numviews; nview++) {
//Create a new view, the created view will persist in the application until the application exits, and the
//application automatically deletes the newly created view
M_ Pviews[nview]->create (null, NULL,
(Afx_ws_default_view & ~ws_visible),
//afx_ws_default_view representative (Ws_border | ws_visible | ws_child)
Rect, m_pMainWnd, Viewid[nview], &newcontext);
}
////When the document template creates a view, the Wm_initialupdate message is automatically sent,
//So for the view we created ourselves, we need to manually send thisMessage
((cform2*) m_pviews[1])->oninitialupdate ();
((cvswapview*) m_pviews[2])->oninitialupdate ();
.....
}

2. View Toggle

CView* CMyWinApp::SwitchView( UINT nIndex )
{
ASSERT( nIndex >=0 && nIndex < NUMVIEWS );
CView* pNewView = m_pViews[nIndex];
CView* pActiveView =((CFrameWnd*) m_pMainWnd)->GetActiveView();
if ( !pActiveView ) // 当前没有激活的视图
return NULL;
if ( pNewView == pActiveView ) // 当前视图和需要切换的视图相同
return pActiveView;
// 交换视图的窗口ID,使RecalcLayout()可以工作
UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
::SetWindowLong(pActiveView->m_hWnd, GWL_ID, ::GetWindowLong(pNewView->m_hWnd, GWL_ID));
::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
// 显示新的视图,隐藏前一个视图
pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
((CFrameWnd*) m_pMainWnd)->RecalcLayout();
pNewView->Invalidate();
return pActiveView;
}

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.