怎样让GetActiveView以及GetActiveDocument的返回值不为NULL?

yangsaibing 2001-08-17 12:08:39
我的代码是这样的,自己建立一个基类为CMDIChildWnd的类CMyMDIWnd,里面有这样一些代码:
int CMyMDIWnd::SetLstInfo(int nObjFlag)
{
CDataDoc *pDoc = NULL;
CView *pView = NULL;
CDocument *pDocTemp = NULL;
pView = this->GetActiveView();
pDocTemp = this->GetActiveDocument() ;
if(!pDocTemp) return 0;
if(!pDocTemp->IsKindOf(RUNTIME_CLASS(CDataDoc)))
return 0;

pDoc = (CDataDoc*) pDocTemp;
if(pDoc) {
pDoc->SetViewData(nObjFlag);
return 1;

}
return 0;
}
请问,为什么调用时pView以及pDocTemp里的值总是为空呢?怎样让GetActiveView以及GetActiveDocument的返回值不为NULL?
...全文
569 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangsaibing 2001-08-17
  • 打赏
  • 举报
回复
请高手指点
emmai 2001-08-17
  • 打赏
  • 举报
回复
GetParentFrame()->SetActiveView();
猜的。
蒋晟 2001-08-17
  • 打赏
  • 举报
回复
VWRPLC32 Replacing a View in a CMDIChildWnd Window
ID: Q141499


--------------------------------------------------------------------------------
The information in this article applies to:

The Microsoft Foundation Classes (MFC), used with:
Microsoft Visual C++, 32-bit Editions, versions 4.0, 4.1, 4.2, 5.0, 6.0

--------------------------------------------------------------------------------
This is the 32-bit version of this sample.

SUMMARY
The VWRPLC32 sample demonstrates how, in a Multidocument Interface (MDI) application, a programmer can write a ReplaceView() member function for a CMDIChildWnd-derived class.

NOTE: The following samples, which also replace various views, exist:

SPLIT32 (SDI)
COLLECT (MDI)
ENROLL (SDI)
VSWAP32 (SDI)
The following files are available for download from the Microsoft Download Center. Click the file names below to download the files:


Vwrplc32.exe

For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.

NOTE: Use the -d option when running VWRPLC32.EXE to decompress the file and re-create the proper directory structure.



MORE INFORMATION
The core of the sample can be found in FRAME.CPP. You will see the following function:


BOOL CFrame::ReplaceView(CRuntimeClass * pViewClass)
{
CCreateContext context;
CView * pCurrentView;

// If no active view for the frame, return FALSE because
// thisfunction retrieves the current document from the active
// view.
if ((pCurrentView=GetActiveView())==NULL)
return FALSE;

// If we're already displaying this kind of view, no need
// to go further.
if ((pCurrentView->IsKindOf(pViewClass))==TRUE)
return TRUE;

// Get pointer to CDocument object so that it can be used
// in the creation process of the new view.
CDocument * pDoc= pCurrentView->GetDocument();

// Set flag so that document will not be deleted when
// view is destroyed.
BOOL bAutoDelete=pDoc->m_bAutoDelete;
pDoc->m_bAutoDelete=FALSE;
// Delete existing view
pCurrentView->DestroyWindow();
// restore flag
pDoc->m_bAutoDelete=bAutoDelete;

// Create new view and redraw.
context.m_pNewViewClass=pViewClass;
context.m_pCurrentDoc=pDoc;
context.m_pNewDocTemplate=NULL;
context.m_pLastView=NULL;
context.m_pCurrentFrame=this;

CView * pNewView = (CView *) pViewClass->CreateObject();

if (pNewView == NULL)
{
TRACE1("Warning: Dynamic create of view type %Fs
failed\n", pViewClass->m_lpszClassName);
return FALSE;
}

if (!pNewView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST,
&context))
{
TRACE0("Warning: couldn't create view for frame\n");
return FALSE; // Programmer can assume FALSE return value
// from this function means that there
// isn't a view.
}


// WM_INITIALUPDATE is define in AFXPRIV.H.
pNewView->SendMessage(WM_INITIALUPDATE, 0, 0);

RecalcLayout();

pNewView->UpdateWindow();

SetActiveView(pNewView);

return TRUE;
}
The function receives a pointer to CRuntimeClass object for the new view that is desired. It destroys the old view and replaces it with a new view of the same CDocument. When DestroyWindow() is called for the old view, this causes a "delete this" in the CView::PostNcDestroy() function. Also, the CView::~CView destructor calls CView::RemoveView(), which removes the view from the document's view list.



REFERENCES
For additional information, please see the following article in the Microsoft Knowledge Base:

Q102829 Replacing a View in a CMDIChildWnd Window

Additional query words: MDI

Keywords : kbcode kbfile kbsample kbDocView kbMDI kbMFC kbVC400 kbVC500 kbVC600 kbGrpMFCATL
Version : winnt:4.0,4.1,4.2,5.0,6.0
Platform : winnt
Issue type :
Technology : kbvc


Last Reviewed: March 13, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.




--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
蒋晟 2001-08-17
  • 打赏
  • 举报
回复
see VWRPLC sample in MSDN
yangsaibing 2001-08-17
  • 打赏
  • 举报
回复
请问,如何多个激活View中的一个,请高手帮帮我
蒋晟 2001-08-17
  • 打赏
  • 举报
回复
如果使用应用程序向导生成的代码,则不会有这种情况。

16,472

社区成员

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

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

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