我正在使用MFC类,但使用的是非标准方式(即完全不使用CDocument派生的类)。
尽管如此,我有几个从CFormView派生的视图,并且有类成员变量。
我有使用CMiniFrameWnd派生类的代码,就像这样
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS(CImageView);
context.m_pCurrentDoc = NULL;
CView* pNewView = STATIC_DOWNCAST(CView, CreateView(&context));
if (pNewView != NULL)
{
pNewView->ShowWindow(SW_SHOW);
pNewView->OnInitialUpdate();
SetActiveView(pNewView);
}
// save view
但是与此类似的是CreateView()正在调用CImageView的默认构造函数,而OnInitialUpdate()是一个虚拟重写,必须与CView()的签名匹配。
那么,如何初始化属于CImageView的成员数据? CreateView()和OnInitialUpdate()会妨碍您的工作(除非我遗漏了一些东西)。似乎在MFC体系结构中不容易初始化CView或CFormView的派生类。
谢谢
答案 0 :(得分:0)
我要承认,我不确定这不是您想要的,但是在我的应用程序中,我必须转换为其他视图类型。我是这样的:
BOOL CCommunityTalksDoc::SwitchToView(CRuntimeClass* pNewViewClass)
{
POSITION rPos;
CView *pOldActiveView;
CFrameWnd *pChild;
CCreateContext context;
BOOL bAutoDelete;
rPos = GetFirstViewPosition();
pOldActiveView = GetNextView(rPos);
pChild = pOldActiveView->GetParentFrame();
// If we're already displaying this kind of view, no need to go further.
if (pOldActiveView->IsKindOf(pNewViewClass))
return TRUE;
// Set flag so that document will not be deleted when view is destroyed.
bAutoDelete = m_bAutoDelete;
m_bAutoDelete = FALSE;
// Delete existing view
pOldActiveView->DestroyWindow();
// restore flag
m_bAutoDelete = bAutoDelete;
// Create new view.
m_pScriptView = (CScriptParseView*)pNewViewClass->CreateObject();
if (m_pScriptView == nullptr)
{
TRACE1("Warning: Dynamic create of view type %s failed\n", pNewViewClass->m_lpszClassName);
return FALSE;
}
// we must ensure the popup dialogues don't display
m_pScriptView->SetBuildMode(FALSE);
// Draw new view.
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = this;
context.m_pNewDocTemplate = nullptr;
context.m_pLastView = nullptr;
context.m_pCurrentFrame = pChild;
if (!m_pScriptView->Create(nullptr, nullptr, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0),
pChild, AFX_IDW_PANE_FIRST, &context))
{
TRACE0("Warning: couldn't create view for frame\n");
delete m_pScriptView;
m_pScriptView = nullptr;
return FALSE;
}
m_pScriptView->SendMessage(WM_INITIALUPDATE, 0, 0); // WM_INITIALUPDATE is defined in afxpriv.h
pChild->RecalcLayout();
m_pScriptView->UpdateWindow();
pChild->SetActiveView(m_pScriptView);
return TRUE;
}
呼叫代码:
// we must set our view correctly
// note, I am not convinced we are doing this in the right place
// what happens if user makes a second view?
// and what about when RebuildReport is called ?
if(m_pScriptView == nullptr)
{
//bNewReport = true;
// we must create view
pNewViewClass = RUNTIME_CLASS(CScriptParseView);
if (!SwitchToView(pNewViewClass))
{
// fail, don't know why it would fail
ASSERT(FALSE);
}
else
{
// we set the variables map only once
m_pScriptView->SetVarValuesMap(psData->pMapSSVarsValues);
m_pScriptView->SetVarOptionsMap(psData->pMapSSVarsOptions); // AJT v10.6.0
}
}
希望这会有所帮助。