mfc doc view 记录备忘
来源:互联网 发布:郑州淘宝摄影 编辑:程序博客网 时间:2024/06/05 16:45
getdoc()
{
CMDIChildWnd * pChild =
((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();
if ( !pChild )
return ;
CDocument * pDoc = pChild->GetActiveDocument();
}
getview()
{
}
{
CMainFrame * pFrm = (CMainFrame *)this->GetMainWnd();
CMDIChildWnd * pchildframe = (CMDIChildWnd*)pFrm->GetActiveFrame();
CView* pview = pchildframe->GetActiveView();
}
http://support.microsoft.com/kb/108587
http://www.codeguru.com/forum/showthread.php?t=473808
Q: How to get the active document anywhere in my application?
A: There are several methods, one is to get first the active frame then call CFrameWnd::GetActiveDocument.
In an SDI application the main frame is the main window itself so a function which gets the active document may look like:
CDocument* GetSDIActiveDocument()
{
CDocument* pDoc = NULL;
CWnd* pWndMain = AfxGetMainWnd();
ASSERT(pWndMain);
ASSERT(pWndMain->IsKindOf(RUNTIME_CLASS(CFrameWnd)) &&
!pWndMain->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd))); // Not an SDI app.
pDoc = ((CFrameWnd*)pWndMain)->GetActiveDocument();
return pDoc;
}
In MDI applications, we have to additionally get the active MDI child frame.
CDocument* GetMDIActiveDocument()
{
CDocument* pDoc = NULL;
CWnd* pWndMain = AfxGetMainWnd();
ASSERT(pWndMain);
ASSERT(pWndMain->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd))); // Not an MDI app.
CFrameWnd* pFrame = ((CMDIFrameWnd*)pWndMain)->MDIGetActive();
if(NULL != pFrame)
{
pDoc = pFrame->GetActiveDocument(); // get the active document
}
return pDoc;
}
Putting them together we can write the following generic function to be used either in SDI or MDI applications.
CDocument* GenericGetActiveDocument()
{
CDocument* pDoc = NULL;
CWnd* pWndMain = AfxGetMainWnd();
if(NULL != pWndMain)
{
if(pWndMain->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
{
// MDI application, so first we have to get the active MDI child frame.
CFrameWnd* pFrame = ((CMDIFrameWnd*)pWndMain)->MDIGetActive();
if(NULL != pFrame)
{
pDoc = pFrame->GetActiveDocument();
}
}
else if(pWndMain->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
{
// SDI appllication so main window is the active frame.
pDoc = ((CFrameWnd*)pWndMain)->GetActiveDocument();
}
else
{
ASSERT(FALSE); // Neither MDI nor SDI application.
}
}
return pDoc;
}
Before casting to a given document type we have to check if indeed the returned pointer is of that type.
CDocument* pDoc = GenericGetActiveDocument();
if((NULL != pDoc) && pDoc->IsKindOf(RUNTIME_CLASS(CMyDocument)))
{
CMyDocument* pMyDoc = (CMyDocument*)pDoc;
// enjoy of ponter to CMyDocument object.
}
To avoid such type of tests in many places, the GenericGetActiveDocument function can be improved, giving to it info about the required document type.
CDocument* GenericGetActiveDocument(CRuntimeClass* pClass)
{
ASSERT(NULL != pClass); // must be not NULL and derived from CDocument.
ASSERT(pClass->IsDerivedFrom(RUNTIME_CLASS(CDocument)));
CDocument* pDoc = NULL;
CWnd* pWndMain = AfxGetMainWnd();
if(NULL != pWndMain)
{
if(pWndMain->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
{
// MDI application, so first we have to get the active MDI child frame.
CFrameWnd* pFrame = ((CMDIFrameWnd*)pWndMain)->MDIGetActive();
if(NULL != pFrame)
{
CDocument* pActiveDoc = pFrame->GetActiveDocument();
if((NULL != pActiveDoc) && pActiveDoc->IsKindOf(pClass))
{
// The found document is of required type
pDoc = pActiveDoc;
}
}
}
else if(pWndMain->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
{
// SDI appllication so main window is the active frame.
pDoc = ((CFrameWnd*)pWndMain)->GetActiveDocument();
}
else
{
ASSERT(FALSE); // Neither MDI nor SDI application.
}
}
return pDoc;
}
With the help of a little macro...
#define GET_ACTIVE_DOC(x) (x*)GenericGetActiveDocument(RUNTIME_CLASS(x))
...all remained to write anywhere in the SDI/MDI MFC application is
CMyDocument* pDoc = GET_ACTIVE_DOC(CMyDocument);
- mfc doc view 记录备忘
- MFC中app doc view类区别
- MFC Doc/View结构实现ActiveX控件
- MFC 获取App,Doc,MainFrame,View指针
- MFC中MFC中 Doc 和 View之间的交互
- MVC结构模式与MFC Doc/View结构
- 【MFC】在DOC/View格局中创建CDialogBar
- (转)MFC中app doc view类区别
- MFC 中 Doc View Mainframe app 指针相互引用
- MFC中的MainFrame Dlg,App,Doc,View的关系
- MFC doc类,view类,frame类之间互相调用
- MFC中的MainFrame Dlg,App,Doc,View的关系
- MFC中的MainFrame Dlg,App,Doc,View的关系
- MFC中的MainFrame Dlg,App,Doc,View的关系
- MFC中的MainFrame Dlg,App,Doc,View的关系
- MFC中app、doc、view类区别与联系
- MFC中的MainFrame Dlg,App,Doc,View的关系
- MFC Doc/View笔记(一) 打开文件代码跟踪
- Shape Of My Heart--《这个杀手不太冷》主题曲
- 轻松搭建一个Windows SVN服务器
- 浅谈:国内软件公司为何无法做大做强?
- TortoiseSVN使用帮助和下载
- MVC2.0皮毛学习笔记(MVC2.0+VS2008)
- mfc doc view 记录备忘
- 飞鸽传书原创代码测试
- IBM INNOV8 介绍
- list_entry()宏
- php环境配置(windws)
- VS2008连接SQL2008时报错:“不支持此服务器版本,仅支持sql 2005以下版本的服务器”
- 我想回来了
- 让你受益一生的80句话
- 环境部署svn切换脚本编写