现在的位置: 首页 > 综合 > 正文

the relationship of view, doc and frame

2017年12月28日 ⁄ 综合 ⁄ 共 1580字 ⁄ 字号 评论关闭

对话框上建立一个文档视图模型代码,两个思路都遇到问题进行不下去。

1.first attempt

使用已有的框架机制:

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MENU_DIALOG,
RUNTIME_CLASS(CDialogBasedDoc),
RUNTIME_CLASS(CDialogBasedView),       // 主 SDI 框架窗口
RUNTIME_CLASS(CDialogBasedFrameView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);  ///CDialog 不具备这样的成员函数。 它来自CWinApp.

2.手工建立它们的关系:

        CCreateContext context;

context.m_pCurrentDoc=m_pDialogBasedDoc;
context.m_pCurrentFrame=(CFrameWnd*)this;//the frame pointer point to itself
context.m_pLastView=NULL;//the previous view is NULL
context.m_pNewDocTemplate=NULL;///the templateDoc is NULL;
context.m_pNewViewClass=RUNTIME_CLASS(CDialogBasedFrameView);
m_pView=(CDialogBasedFrameView*)context.m_pNewViewClass->CreateObject();///create the view object
if(m_pView==NULL)
{
TRACE1("Warning: Dynamic creating of view type %hs failed.\n",
context.m_pNewViewClass->m_lpszClassName);
}
ASSERT_KINDOF(CWnd,m_pView);
if(!m_pView->Create(NULL,        ///lpszClassNamem
NULL,                        ///window Namen
AFX_WS_DEFAULT_VIEW,         ///window style
RectTargetCtrl,
this,                        ///parent pointer
AFX_IDW_PANE_FIRST,          ///ID of the child window

&context))                   ///CCreateContext struct
{
TRACE0("Warning: Couldn't create view for frame.\n");
return FALSE;
}

//TRY load menu bar
CMenu *ptrDlgMenu=new CMenu();
ptrDlgMenu->LoadMenuW(IDR_MENU_DIALOG);
SetMenu(ptrDlgMenu);
DrawMenuBar();
//menu belongs to an CWnd Object.

//视图建立正常,但对话框上的菜单条命令无法路由进doc类对象。

(November 9,2011)


November 10,2011

目标前思路,

    在CWinApp类的InitInstance()添加一个新的文档模板类。

    在派生出的对话框类中使用该docTemplate对象,建立另外一个view/doc框架程序。

问题是,在新的对话框类中,怎样使用传进来位于CWinApp中的docTemplate来关联需要的view/doc类,并形成正确的消息命令路由机制。

【上篇】
【下篇】

抱歉!评论已关闭.