MFC中获取App,MainFrame,Doc和View类等指针的方法
1 获取应用程序类(App)指针
在任何类中都可用MFC全局函数AfxGetApp()获得
2 获取框架类(MainFrame)指针
1)在App中获得MainFrame指针
CWinApp 中的 m_pMainWnd变量就是MainFrame的指针
也可以: CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd();
2) 在View中获得MainFrame指针
CMainFrame *pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd;
3 获取各种视图类(View)指针
1)(在App,MainFrame,Doc中)获取当前已建立View
CMainFrame *pMain = (CMainFrame *)AfxGetApp()->m_pMainWnd;
CMyView *pView = (CMyView *)pMain->GetActiveView();
2)从文档类(Doc)取得视图类(View)的指针----多视
CDocument类提供了两个函数用于视图类的定位:GetFirstViewPosition()和GetNextView()
virtual POSITION GetFirstViewPosition() const;
virtual CView* GetNextView(POSITION& rPosition) const;
例:CTestView* pTestView; POSITION pos=GetFirstViewPosition(); pTestView=GetNextView(pos);
为了方便,我们将其作为一个文档类的成员函数,它有一个参数,表示要获得哪个类的指针。实现如下:
CView* CTestDoc::GetView(CRuntimeClass* pClass)
{
CView* pView;
POSITION pos=GetFirstViewPosition();
while(pos!=NULL){
pView=GetNextView(pos);
if(!pView->IsKindOf(pClass))
break;
}
if(!pView->IsKindOf(pClass)){
AfxMessageBox("Cannot Locate the View!");
return NULL;
}
return pView;
}
3)从一个视图类取得另一视图类的指针
用文档类作中转,先得到文档类的指针,再用文档类的视图定位函数取得另一个视图类。同样,可以实现成一个函数:
(假设要从CTestAView中取得指向其它视图类的指针)
CView* CTestAView::GetView(CRuntimeClass* pClass)
{
CTestDoc* pDoc=(CTestDoc*)GetDocument();
CView* pView;
POSITION pos=pDoc->GetFirstViewPosition();
while(pos!=NULL){
pView=pDoc->GetNextView(pos);
if(!pView->IsKindOf(pClass))
break;
}
if(!pView->IsKindOf(pClass)){
AfxMessageBox("Cannot Locate the View!");
return NULL;
}
return pView;
}
4)获取分割视图中各个视图的指针
CSplitterWnd m_wndSplitter;
m_wndSplitter.CreateStatic(this, 1, 2);//分割成一行两列
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftPaneView), CSize(10, 10), pContext);
m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0), pContext);
//获取左边视图的两种方法
CLeftPaneView* pLeftPaneView = (CLeftPaneView*) m_wndSplitter.GetPane(0,0);
//上一句可以用下句代替:
//CLeftPaneView* pLeftPaneView = (CLeftPaneView *)GetActiveView();
//获取右边视图
pLeftPaneView->m_pRightPaneFrame = (CRightPaneFrame*) m_wndSplitter.GetPane(0,1);
4 获取各种文档类(Doc)和文档模板类的指针
1)获得当前文档指针 CDocument * pCurrentDoc = (CFrameWnd *)m_pMainWnd->GetActiveDocument();
2)从文档模板获得文档类指针
一个文档模板可以有多个文档,每个文档模板都保留并维护了一个所有对应文档的指针列表。
用CDocTemplate::GetFirstDocPosition函数获得与文档模板相关的文档集合中第一个文档的位置,并用POSITION值作为CDocTemplate::GetNextDoc的参数来重复遍历与模板相关的文档列表。函数原形为:
virtual POSITION GetFirstDocPosition( ) const = 0;
virtual CDocument *GetNextDoc(POSITION & rPos) const = 0;
如果列表为空,则rPos被置为NULL.
3)在文档类中获得文档模板指针
在文档中可以调用CDocument::GetDocTemplate获得指向该文档模板的指针。
函数原形如下: CDocTemplate * GetDocTemplate ( ) const;
如果该文档不属于文档模板管理,则返回值为NULL。
MFC中获取App,MainFrame,Doc和View类等指针的方法的更多相关文章
- MFC中获取各个窗口之间的句柄或者指针对象的方法
MFC在非常多的对话框操作中,我们常常要用到在一个对话框中调用还有一个对话框的函数或变量.能够用例如以下方法来解决. HWND hWnd=::FindWindow(NULL,_T("S ...
- MFC中获取指针的方法
1.获取应用程序指针 CMyApp* pApp=(CMyApp*)AfxGetApp(); 2.获取主框架指针 CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针 CMainFr ...
- Android Studio中获取查看签名SHA1证书指纹数据或MD5的方法
原来在Eclipse中获取SHA1或者MD5,在IDE界面上就可以查找到. 切换到Android Studio后,如何查看呢?找了半天没找到.那就老办法命令行. 第一步.打开Android Studi ...
- Java中获取项目根路径和类加载路径的7种方法
引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...
- File类获取功能的方法和File类判断功能的方法
File类获取功能的方法-public String getAbsolutePath() :返回此file的绝对路径名字符串 -public String getPath() :将此File转换为路径 ...
- MFC中获取系统当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是%Y年% ...
- MFC中 获取新输入编辑框的内容
//得到原始内容的长度 int len = m_editPoemFileStr.GetLength(); UpdateData(true); //得到新增加的内容 CString sNewStrin ...
- VS2010 MFC中 单独添加ODBC数据库记录集类(CRecordset)方法
基于VS2010 MFC的项目是之前建好的,后来需要添加数据库. 方法分享于此. 1. 打开自己的项目,项目->添加类. 2. 选MFC ODBC使用者,点右下角的添加. 3. 点数据源. / ...
- java中获取类加载路径和项目根路径的5种方法
import java.io.File; import java.io.IOException; import java.net.URL; public class MyUrlDemo { publi ...
随机推荐
- Avro基础 分类: C_OHTERS 2015-02-14 19:56 310人阅读 评论(0) 收藏
一.Avro的基本功能 1.定义了数据模式文件的语法,一般使用json文件.以及一些数据基本类型与复杂类型. 2.定义了数据序列化到文件后的数据格式,此格式可供各种语言进行读取. 3.为部分语言定义了 ...
- bc -l 对于 %取模计算出错
https://yq.aliyun.com/articles/279384 expr % expr The result of the expression is the "rema ...
- android的edittext设置输入限制,只能输入数字
EditText的属性里面已经封装好了相关的设置,上一篇文章里面也提到了,不熟悉的可以去查看上一篇EditText属性大全,这里着重讲输入限制的属性: android:digits="123 ...
- ssion机制详解
ssion机制详解 ref:http://justsee.iteye.com/blog/1570652 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚sess ...
- [SCSS] Reuse Styles with the SCSS @extend Directive
We can write reusable styles with the SCSS @extend or @mixin directives. Which one is better? It dep ...
- [Compose] 18. Maintaining structure whilst asyncing
We take our Promise.all() analogy further by using traversable on a Map(). Then we use two traversal ...
- android使用Gson来解析json
Gson是一种对象的解析json,非常好用,介绍一个站点http://json.parser.online.fr/能够帮我们看一个字符串是不是Json 对于Json文件 { "id" ...
- 【codeforces 755B】PolandBall and Game
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 百度消息推送SDK探究(并附上最简推送Demo)
上一篇<百度消息推送REST API探究>中了解了如何使用REST API推送消息,这一篇我们来看一下百度消息推送为我们提供的SDK. 帮助文档:http://developer.baid ...
- PatentTips - Heterogeneous Parallel Primitives Programming Model
BACKGROUND 1. Field of the Invention The present invention relates generally to a programming model ...