SDI框架中有个上下结构的拆分窗口.当事件来时,我想让上面的窗口替换为视窗CUpView.CUpView里面又实现了拆分,为左右结构.Class CUpView::public CView
{
CSplitterWnd m_wndSplitter;
};int CUpView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CCreateContext *pcontext=(CCreateContext*)lpCreateStruct->lpCreateParams;
if (CView::OnCreate(lpCreateStruct) == -1)
return -1; m_wndSplitter.CreateStatic(this,2,1); if(!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CMyViewA),CSize(160,1),pcontext))
return -1; if(!m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CMyViewB),CSize(1,1),pcontext))
return -1; return 0;
}只是,上面的窗口根本显示不出.各位帮我看看错在哪里.
{
CSplitterWnd m_wndSplitter;
};int CUpView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CCreateContext *pcontext=(CCreateContext*)lpCreateStruct->lpCreateParams;
if (CView::OnCreate(lpCreateStruct) == -1)
return -1; m_wndSplitter.CreateStatic(this,2,1); if(!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CMyViewA),CSize(160,1),pcontext))
return -1; if(!m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CMyViewB),CSize(1,1),pcontext))
return -1; return 0;
}只是,上面的窗口根本显示不出.各位帮我看看错在哪里.
应该不是上下左右的问题
上面的视切换的时候,调用这个函数.参数pNewClass是CUpView(参数是普通视窗类CMyViewA时候,是可以显示的...)void CMySplitterWnd::ReplaceView(int row, int col, CRuntimeClass* pNewClass)
{
CView *pOldView=(CView*)GetPane(0,0);
ASSERT(*pOldView); int ID=pOldView->GetDlgCtrlID();
CDocument *pDoc=pOldView->GetDocument();
CCreateContext context;
context.m_pCurrentDoc=pDoc;
context.m_pNewViewClass=pNewClass;
context.m_pNewDocTemplate=pDoc?pDoc->GetDocTemplate():NULL;
context.m_pCurrentFrame=NULL;
context.m_pLastView=NULL; CWnd *pWnd;
pWnd=(CWnd*)context.m_pNewViewClass->CreateObject(); ASSERT_KINDOF(CWnd, pWnd);
ASSERT(pWnd->m_hWnd == NULL); // not yet created.
if(!pWnd->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,ID,&context))
{
TRACE0( "Warning: couldn't create new view.\n" );
// pWnd will be cleaned up by PostNcDestroy
return ;
}
pOldView->ShowWindow(SW_HIDE);
pOldView->SetDlgCtrlID(0); RecalcLayout(); CView *pNewView=DYNAMIC_DOWNCAST(CView,GetPane(row,col) );
ASSERT_KINDOF(CView,pNewView);
pNewView->OnInitialUpdate();}
另外,你创建的view的size,居然有1,你改一下看看!
return -1;if(!m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CMyViewB),CSize(1,1),pcontext))
return -1;
上面的应该卸载OnCreateClient()虚函数里面。
参见MSDN的解释..The CSplitterWnd class provides the functionality of a splitter window, which is a window that contains multiple panes. A pane is usually an application-specific object derived from CView, but it can be any CWnd object that has the appropriate child window ID.A CSplitterWnd object is usually embedded in a parent CFrameWnd or CMDIChildWnd object. Create a CSplitterWnd object using the following steps: Embed a CSplitterWnd member variable in the parent frame.
Override the parent frame's CFrameWnd::OnCreateClient member function.
From within the overridden OnCreateClient, call the Create or CreateStatic member function of CSplitterWnd.
Call the Create member function to create a dynamic splitter window. A dynamic splitter window typically is used to create and scroll a number of individual panes, or views, of the same document. The framework automatically creates an initial pane for the splitter; then the framework creates, resizes, and disposes of additional panes as the user operates the splitter window's controls
创建单文档工程时,要在第几步里面(VC6和VS2005不同)选择拆分窗口选择。这样工程将自动创建一个两行两列的动态拆分窗口。第二步:
class CMainFrame : public CFrameWnd
{
protected: // 仅从序列化创建
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)// 属性
protected:
CSplitterWnd m_wndSplitter1;
CSplitterWnd m_wndSplitter2;public:// 操作
public:// 重写
public:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);// 实现
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected: // 控件条嵌入成员
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;// 生成的消息映射函数
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
CCreateContext* pContext)
{
// 拆分一行两列
if (!m_wndSplitter1.CreateStatic(this, 1, 2))
return FALSE;
if (!m_wndSplitter1.CreateView(0, 0, RUNTIME_CLASS(CMyClassA), CSize(200, 0), pContext))
return FALSE; // 将前一拆分右侧窗口拆分两行一列
if (!m_wndSplitter2.CreateStatic(&m_wndSplitter1, 2, 1, WS_CHILD | WS_VISIBLE, m_wndSplitter1.IdFromRowCol(0, 1)))
return FALSE;
if (!m_wndSplitter2.CreateView(0, 0, RUNTIME_CLASS(CMyClassB), CSize(0, 200), pContext))
return FALSE;
if (!m_wndSplitter2.CreateView(1, 0, pContext->m_pNewViewClass, CSize(0, 0), pContext))
return FALSE;
SetActiveView((CView *)m_wndSplitter1.GetPane(0, 0)); return TRUE;
}
这样就可以拆分了,呵呵