如何在拆分窗口中动态切换视图?

birth_chen 2002-05-14 01:54:49
我建了一个SDI程序,垂直拆分窗口为两部分,左边为列表试图,右边为表单视图,现在想在程序中动态切换右边的表单视图,即在不同的情况下显示不同的表单视图,应该怎么实现呢?最好有源代码,要不提示也行。分不够可以再加。
...全文
286 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
birth_chen 2002-05-16
  • 打赏
  • 举报
回复
问题已经解决,3ks
蒋晟 2002-05-14
  • 打赏
  • 举报
回复
旧版本的是.mak
pcontext用来描述创建文档视图框架的上下文,比如重用已有文档
INFO: Switching Views in a Single Document Interface Program
Q99562
NOTE: These steps assume that the name of the CWinApp-derived object is CMyWinApp; that CMyWinApp is declared and defined in MYWINAPP.H and MYWINAPP.CPP, respectively; that CNewView is the name of the new, CView- derived object; and that CNewView is declared and defined in NEWVIEW.H and NEWVIEW.CPP, respectively. Substitute these with your own class and file names as needed.



Add the following members to the declaration of CMyWinApp in MYWINAPP.H:

CView* m_pOldView;
CView* m_pNewView;
CView* SwitchView(CView* pNewView);



If CNewView was created with ClassWizard, modify the CNEWVIEW.H to change the access specifier for the constructor, destructor, and OnInitialUpdate() function from protected to public.


Add "#include <AFXPRIV.H>" (without the quotation marks) to the include section of MYWINAPP.CPP. This is required to define the WM_INITIALUPDATE message to be added in step 4.


Create a new view and attach it to the document. The following code fragment creates a new view in the InitInstance() member of the CMyWinApp object. In this way, both new and existing views persist for the lifetime of the application; however, the application could just as easily create the new view dynamically.

This code requires the main frame window, document, and default view to exist already. In Visual C++ for Windows and Visual C++ 32-bit Edition, versions 1.0 through 2.x, insert the following code into CMyWinApp::InitInstance() after the call to OnFileNew(), as OnFileNew() creates each of these elements. In Visual C++ 32-bit Edition, versions 4.0 or later, insert this code after the call to ProcessShellCommand():

...
CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
m_pOldView = pActiveView;
m_pNewView = (CView*) new CNewView;

CDocument* pCurrentDoc =
((CFrameWnd*) m_pMainWnd)->GetActiveDocument();

// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;

// The ID of the initial active view is AFX_IDW_PANE_FIRST.
// Incrementing this value by one for additional views works
// in the standard document/view case but the technique cannot
// be extended for the CSplitterWnd case.
UINT viewID = AFX_IDW_PANE_FIRST + 1;
CRect rect(0, 0, 0, 0); // gets resized later

// Create the new view. In this example, the view persists for
// the life of the application. The application automatically
// deletes the view when the application is closed.
m_pNewView->Create(NULL, "AnyWindowName", WS_CHILD, rect,
m_pMainWnd, viewID, &newContext);

// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.
m_pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);
...



Define the CMyApp::SwitchView() function. (Alternatively, SwitchView() could be declared and defined as a member of the main frame class.)

CView* CMyWinApp::SwitchView(CView* pNewView)
{
CView* pActiveView =
((CFrameWnd*) m_pMainWnd)->GetActiveView();

// Exchange view window ID's so RecalcLayout() works.
#ifndef _WIN32
UINT temp = ::GetWindowWord(pActiveView->m_hWnd, GWW_ID);
::SetWindowWord(pActiveView->m_hWnd, GWW_ID,
::GetWindowWord(pNewView->m_hWnd, GWW_ID));
::SetWindowWord(pNewView->m_hWnd, GWW_ID, temp);
#else
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);
#endif

pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
((CFrameWnd*) m_pMainWnd)->RecalcLayout();
pNewView->Invalidate();
return pActiveView;
}
NOTE: This function returns a pointer to the old view so that the old view can be destroyed if desired. Before destroying the view though, CDocument::RemoveView() should be called so the association between the view and the document is removed.


Add command handlers or other code to call the SwitchView() function when the application needs to switch between views.

SwSplit.exe Switches Splitter Pane Views in SDI App
Q199515
http://download.microsoft.com/download/vc60pro/Sample/10/WIN98/EN-US/SwSplit.exe
The VIEWEX and COLLECT samples might also be helpful. Both samples come with Visual C++. VIEWEX uses the MDI architecture, and shows you how to use different types of static splitter windows in separate child frame windows.

birth_chen 2002-05-14
  • 打赏
  • 举报
回复
有没有人能告诉我?
birth_chen 2002-05-14
  • 打赏
  • 举报
回复
谢谢,但是运行的时候到CreateView这一步出错,应用程序错误:

"0x04244c8b"指令引用的"0x04244c8b"内存。该内存不能为"read"。

要终止程序,请单击"确定"。
要调试程序,请单击"取消"。

请问这是什么原因呢?好有我搞不清CreateView函数中的pcontext参数是用来干吗的,能解释一下吗?

to jiangsheng(蒋晟卧病中):
代码中怎么没有.dsw?
蒋晟 2002-05-14
  • 打赏
  • 举报
回复
http://download.microsoft.com/download/vc40std/Sample/14/WIN98/EN-US/Split32.exe
蒋晟 2002-05-14
  • 打赏
  • 举报
回复
How to Replace a View in a Splitter Window

Q149257
tomcat_jb 2002-05-14
  • 打赏
  • 举报
回复
将下面的代码放在你的控制代码中,
其中控制参数是ITEMDATA,根据其中的值不同,可以改变右边的VIEW。先删除右边,再建立。如果CASE其他的值可以根据需要添加其他的VIEW。

CWinApp * pApp=AfxGetApp();
CMainFrame *pMainWnd=(CMainFrame *)pApp->m_pMainWnd;
ItemData = (int)m_treectrl.GetItemData(hItem);
CRuntimeClass* pRunclassView;

switch (ItemData)
{
case 0:
case 1:
//删除当前视图
pMainWnd->m_wndSplitter.DeleteView(0, 1);

//设置pContext

pRunclassView = RUNTIME_CLASS(CBaseInfoView);
pContext.m_pNewViewClass = pRunclassView;

//建立并显示新的试图
pMainWnd->m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CBaseInfoView), CSize(100,100), &pContext);
BaseInfoView = (CBaseInfoView*)pMainWnd->m_wndSplitter.GetPane(0,1);
if (BaseInfoView != NULL)
{
// the new view is there, but invisible and not active...
BaseInfoView->ShowWindow(SW_SHOW);
BaseInfoView->OnInitialUpdate();
pMainWnd->m_wndSplitter.RecalcLayout();
}
break;
LegerWu 2002-05-14
  • 打赏
  • 举报
回复
你必须创建一个动态分割器,用:

BOOL Create( CWnd* pParentWnd, int nMaxRows, int nMaxCols, SIZE sizeMin, CCreateContext* pContext, DWORD dwStyle = WS_CHILD | WS_VISIBLE |WS_HSCROLL | WS_VSCROLL | SPLS_DYNAMIC_SPLIT, UINT nID = AFX_IDW_PANE_FIRST );

来创建,而不是:

BOOL CreateStatic( CWnd* pParentWnd, int nRows, int nCols, DWORD dwStyle = WS_CHILD | WS_VISIBLE, UINT nID = AFX_IDW_PANE_FIRST );

这样,你可以动态调整分割;

当某一个View不需要时,你可以调用DeleteView将其删除,然后调用CreateView再创建新的视图.

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧