http://www.cnblogs.com/ylhome/archive/2009/10/06/1578478.html

一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架,无论是多文档还是单文档,都存在指针获取和操作问题。
下面这节内容主要是一般的框架,然后再讲多线程中的指针使用。使用到的类需要包含响应的头文件。首先一般获得本类(视,文档,对话框都支持)实例指针 this,用this的目的,主要可以通过类中的函数向其他类或者函数中发指针,以便于在非本类中操作和使用本类中的功能。 这其中的关键在于理解 m_pMainWnd, AfxGetApp(),AfxGetMainWnd() 的意义!
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);
CToolBar * pToolBar=(CtoolBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);
7) 如果框架中加入工具栏和状态栏变量还可以这样
(CMainFrame *)GetParent()->m_wndToolBar;
(CMainFrame *)GetParent()->m_wndStatusBar;
8) 在Mainframe获得菜单指针
CMenu *pMenu=m_pMainWnd->GetMenu();
9) 在任何类中获得应用程序
AfxGetInstanceHandle 得到句柄,AfxGetApp 得到指针
B1.如何在自己的类和“应用程序类”中获得“文档类”的句柄?
SDI AfxGetMainWnd() -> GetActiveView() -> GetDocument() 得到指针
MDI AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument() 得到指针
B3.如何在“框架类”中获得“文档类”句柄?
SDI GetActiveView() -> GetDocument() 得到指针,MDI MDIGetActive() -> GetActiveView() -> GetDocument() 从 CMainFrame 得到指针,GetActiveView() -> GetDocument() 从 CChildFrame 得到指针
B4.如何在“视图类”中获得“文档类”句柄?
GetDocument()
C1.如何在“文档类”中获得“视图类”句柄?
GetView(),调用 GetFirstViewPosition 和 GetNextView 函数得到指针。
C2.如何在自己的类和“应用程序类”中获得“视图类”句柄?
SDI GetActiveView 得到指针
MDI MDIGetActive() -> GetActiveView() 从 CMainFrame 得到指针,GetActiveView 从 CChildFrame 得到指针

最后提醒大家,在提取到各个句柄之后,因为初次提取的都是标准类句柄,所以,在使用时要注意将标准句柄转换成自己的类的句柄。
如:
AfxGetApp();//得到的是WinApp类的句柄,
所以操作前记得转换成自己定义的类的句柄。
如:
((CMyApp*)AfxGetApp())->XXXX();//这的xxxx()就是你定义的类中间的成员。

另外,附上 MSDN 关于应用程序信息和管理的各个函数:
When you write an application, you create a single CWinApp-derived object. At times, you may want to get information about this object from outside the CWinApp-derived object.
The Microsoft Foundation Class Library provides the following global functions to help you accomplish these tasks:
Application Information and Management Functions
AfxFreeLibrary
Decrements the reference count of the loaded dynamic-link library (DLL) module; when the reference count reaches zero, the module is unmapped.

AfxGetApp
Returns a pointer to the application's single CWinApp object.

AfxGetAppName
Returns a string containing the application's name.

AfxGetInstanceHandle
Returns an HINSTANCE representing this instance of the application.

AfxGetMainWnd
Returns a pointer to the current "main" window of a non-OLE application, or the in-place frame window of a server application.

AfxGetResourceHandle
Returns an HINSTANCE to the source of the application's default resources. Use this to access the application's resources directly.

AfxInitRichEdit
Initializes the version 1.0 rich edit control for the application.

AfxInitRichEdit2
Initializes the version 2.0 and later rich edit control for the application.

AfxLoadLibrary
Maps a DLL module and returns a handle that can be used to get the address of a DLL function.

AfxRegisterWndClass
Registers a Windows window class to supplement those registered automatically by MFC.

AfxSocketInit
Called in a CWinApp::InitInstance override to initialize Windows Sockets.

AfxSetResourceHandle
Sets the HINSTANCE handle where the default resources of the application are loaded.

AfxRegisterClass
Registers a window class in a DLL that uses MFC.

AfxBeginThread
Creates a new thread.

AfxEndThread
Terminates the current thread.

AfxGetThread
Retrieves a pointer to the current CWinThread object.

AfxWinInit
Called by the MFC-supplied WinMain function, as part of the CWinApp initialization of a GUI-based application, to initialize MFC. Must be called directly for console applications using MFC.

 
 
 
 
ID--HANDLE--HWND三者之间的互相转换
id->句柄(由ID得到句柄)-----------hWnd = ::GetDlgItem(hParentWnd,id);

id->指针(由ID得到指针)-----------CWnd::GetDlgItem();

句柄->id(由句柄得到ID)-----------id = GetWindowLong(hWnd,GWL_ID);

句柄->指针(由句柄得到指针)--------CWnd *pWnd=CWnd::FromHandle(hWnd);

指针->ID(由指针得到ID)----------id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);

GetDlgCtrlID();

指针->句柄(由 指针得到句柄)--------hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;

(转)MFC中获得各个类的指针/句柄 ID的总结的更多相关文章

  1. MFC中的CString类使用方法指南

    MFC中的CString类使用方法指南 原文出处:codeproject:CString Management [禾路:这是一篇比较老的资料了,但是对于MFC的程序设计很有帮助.我们在MFC中使用字符 ...

  2. MFC中获取各个窗口之间的句柄或者指针对象的方法

    MFC在非常多的对话框操作中,我们常常要用到在一个对话框中调用还有一个对话框的函数或变量.能够用例如以下方法来解决.    HWND hWnd=::FindWindow(NULL,_T("S ...

  3. C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...

  4. VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...

  5. MFC中文件对话框类CFileDialog详解及文件过滤器说明

    当前位置 : 首页 » 文章分类 :  开发  »  MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...

  6. VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)

    转自:http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html ①需要一个别人写好的类CWebPage,将其对于的两个文件Web ...

  7. MFC中CString.Format类详解

    在MFC程序中,使用CString来处理字符串是一个很不错的选择.CString既可以处理Unicode标准的字符串,也可以处理ANSI标准的字符串.CString的Format方法给我们进行字符串的 ...

  8. MFC中如何在一个类中调用另一个类的控件

    学习记录: 两个类,一个为主类 1个为:CCkDlg,主类 1个为: Https,用来做HTTPS请求获得页面状态. 测试界面如下: CCkDlg 类里定义函数 void CCkDlg::printf ...

  9. MFC中 自定义类访问主对话框控件的方法

    之前一直在找有木有好点的方法.现在终于被我找到,收藏之~~~~~~ 在使用mfc的时候经常遇到自定义类访问主对话框控件的问题,例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件.控制对话框的 ...

随机推荐

  1. [C++] 如何查看DLL有哪些函数

    Visual Studio里面自带了一个工具 dumpbin. 打开VS的command line,输入dumpbin可以查看帮助. 我们查看导出函数的话,使用选项/EXPORTS. 如果函数太多,可 ...

  2. Linux第三次学习笔记

    #信息的表示和处理 三种重要的数字表示 1. 无符号数编码: 基于传统的二进制表示法,表示大于或者等于零的数字. 2. 补码编码: 表示有符号数整数的最常见的方式,有符号数就是只可 以为正或者为负的数 ...

  3. Unity3D 的摄像机

    什么是摄像机 Unity3D中,摄像机是一个非常非常重要的组件. 他的作用就是:将你设计的场景投影到设备的屏幕上. 摄像机的属性 1 clear flags 确定屏幕的哪一部分将被清除. 每个摄像机在 ...

  4. post数据

    http://cnodejs.org/topic/4f16442ccae1f4aa2700104d http://www.cnblogs.com/pingfan1990/p/4701355.html ...

  5. JavaScript表单处理(下)

    内容提纲: 1.文本框脚本 2.选择框脚本 发文不易,转载请亲注明链接出处,谢谢!   一.文本框脚本 在HTML中,有两种方式来表现文本框: 一种是单行文本框<input type=" ...

  6. 第三章:模块加载系统(requirejs)

    任何一门语言在大规模应用阶段,必然要经历拆分模块的过程.便于维护与团队协作,与java走的最近的dojo率先引入加载器,早期的加载器都是同步的,使用document.write与同步Ajax请求实现. ...

  7. HTML5——地图应用

    我们就拿百度地图举例吧: 废话少说,直接上Demo 简要截图如下:' 简要代码如下: <!DOCTYPE html> <html> <head> <title ...

  8. 每天一个linux命令(33):ps命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  9. LINQ构建交叉表

    最近碰到客户的一个需求.使用交叉表来显示客户数据.也就是以同时以行头和列头交叉形式显示数据内容.同时要求即使有些列没有数据,也需要显示该列内容,并设置默认值. 说明: “交叉表”对象是一个网格,用来根 ...

  10. Java基础-JVM类加载机制

    JVM的类加载是通过ClassLoader及其子类来完成的,类的层次关系和加载顺序可以由下图来描述: 1)Bootstrap ClassLoader /启动类加载器 $JAVA_HOME中jre/li ...