获得父窗口指针:获得MFC窗口其它类指针的方法

来源:互联网 发布:群发手机短信软件 编辑:程序博客网 时间:2024/05/18 03:02

获得CWinApp:
-在CMainFrame,CChildFrame,CDocument,CView中直接调用AfxGetApp()或用theApp
-在其它类中只能用AfxGetApp()

获得CMainFrame:
-在CMinApp中用AfxGetMainWnd()或者m_pMainWnd
-在CChildFrame中可用GetParentFrame()
-在其它类中用AfxGetMainWnd()

获得CChildFrame:
-在CView中用GetParentFrame()
-在CMainFrame中用MDIGetActive()或GetActiveFrame()
-在其它类中用AfxGetMainWnd()->MDIGetActive()或AfxGetMainWnd()->GetActiveFrame()

获得CDocument:
-在CView中用GetDocument()
-在CChildFrame中用GetActiveView()->GetDocument()
-在CMainFrame中用
-if SDI:GetActiveView()->GetDocument()
-if MDI:MDIGetActive()->GetActiveView()->GetDocument()
-在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()->GetDocument()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()->GetDocument()

获得CView:
-在CDocument中 POSITION pos = GetFirstViewPosition();GetNextView(pos)
-在CChildFrame中 GetActiveView()
-在CMainFrame中
-if SDI:GetActiveView()
-if MDI:MDIGetActive()->GetActiveView()
-在其它类中
-if SDI:AfxGetMainWnd()->GetActiveView()
-if MDI:AfxGetMainWnd()->MDIGetActive()->GetActiveView()


不过要注意在doc中要取得view的指针C*View要注意类C*View声明的问题,
因为默认情况下,mfc在*View.h中已经包含了*Doc.h,如果在*Doc.h中包含
*View.h,就会引起嵌套包含问题,这样要在*Doc.h中加入 class C*View;
而在*Doc.cpp中加入 #include "*View.h"

//////////////////////////////////////////////////////////////////
其实完全可以在CYourApp中添加各种视或文档的指针,在那些视或文档初
始化的时候将指针传给CYourApp中的对应变量,这样以后不管在哪用上面
指针只需(CYourApp*)AfxGetApp()取其属性变量即可,明了而且清楚更是
方便我一直专门操作的说:)

//////////////////////////////////////////////////////////////////
我先抛块砖,有玉的砸过来!
在何时何地,你都可以通过以下方法精确的得到任何一个对象(Application,DocTemplate,Document,View,Frame)
1。通过AfxGetApp()得到当前的App对象;
2。通过AfxGetMainWnd()得到主窗口;
3。通过CMDIFrameWnd::GetActiveFrame得到当前活动窗口;
4。通过GetNextWindow()遍例所有的子窗口;(如果要得到你想要的子窗口,可以通过特定的成员变量来标志);
5。通过CWinApp::GetFirstDocTemplatePostion()以及CWinApp::GetNextDocTemplate()的组合应用来遍历所有的DocTemplate对象,并且用CDocTemplate::GetDocString()来判断当前得到的文档莫板对象是哪个。
6。通过CDocTemplate::GetFirstDocPosition()以及CDocTemplate的GetNextDoc()组合来遍历所有的该模板的文档对象,并用CDocument::GetDocTemplate()来得到文档模板,用CDocment::GetTitle() 或者GetPathName()来判断当前的文档是哪个。
7。通过CDocuemt的GetFirstViewPositon()以及GetNextView()来遍历视图对象,一般通过访问View的成员变量来区别各个视图;通过CView::GetDocument()来得到文档对象;
8。Frame->View: 通过GetActiveView方法;
9。Frame->Doc:通过GetActiveDocument();
10。View->Frame:GetParentFrame();
11。View->Doc:GetDocuemt()//前面已经说了。
12。Doc->View:前面说了;
13。Doc->Frame:不知道有没有很直接的方法。
MFC应用程序中指针的使用

1) 在View中获得Doc指针
2) 在App中获得MainFrame指针
3) 在View中获得MainFrame指针
4) 获得View(已建立)指针
5) 获得当前文档指针
6) 获得状态栏与工具栏指针
7) 获得状态栏与工具栏变量
8) 在Mainframe获得菜单指针
9) 在任何类中获得应用程序类
10) 从文档类取得视图类的指针(1)
11) 在App中获得文档模板指针
12) 从文档模板获得文档类指针
13) 在文档类中获得文档模板指针
14) 从文档类取得视图类的指针(2)
15) 从一个视图类取得另一视图类的指针

VC中编程对于刚刚开始学习的同学,最大的障碍和问题就是消息机制和指针获取与 操作。其实这些内容基本上是每本VC学习工具书上必讲的内容,而且通过MSDN很多 问题都能解决。下面文字主要是个人在编程中指针使用的一些体会,说的不当的地 方请指正。一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架, 无论是多文档还是单文档,都存在指针获取和操作问题。下面这节内容主要是一般 的框架,然后再讲多线程中的指针使用。使用到的类需要包含响应的头文件。首先 一般获得本类(视,文档,对话框都支持)实例指针this,用this的目的,主要可以通 过类中的函数向其他类或者函数中发指针,以便于在非本类中操作和使用本类中的功能。

1) 在View中获得Doc指针 CYouSDIDoc *pDoc=GetDocument();一个视只能有一个文
档。
2) 在App中获得MainFrame指针
CWinApp 中的 m_pMainWnd变量就是MainFrame的指针
也可以: CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd();
3) 在View中获得MainFrame指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;
4) 获得View(已建立)指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd;
CyouView *pView=(CyouView *)pMain->GetActiveView();
5) 获得当前文档指针 CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument();
6) 获得状态栏与工具栏指针 CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);