(转)MFC中获得各个类的指针/句柄 ID的总结
http://www.cnblogs.com/ylhome/archive/2009/10/06/1578478.html
下面这节内容主要是一般的框架,然后再讲多线程中的指针使用。使用到的类需要包含响应的头文件。首先一般获得本类(视,文档,对话框都支持)实例指针 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->指针(由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的总结的更多相关文章
- MFC中的CString类使用方法指南
		
MFC中的CString类使用方法指南 原文出处:codeproject:CString Management [禾路:这是一篇比较老的资料了,但是对于MFC的程序设计很有帮助.我们在MFC中使用字符 ...
 - MFC中获取各个窗口之间的句柄或者指针对象的方法
		
MFC在非常多的对话框操作中,我们常常要用到在一个对话框中调用还有一个对话框的函数或变量.能够用例如以下方法来解决. HWND hWnd=::FindWindow(NULL,_T("S ...
 - C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
		
一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...
 - VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
		
1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...
 - MFC中文件对话框类CFileDialog详解及文件过滤器说明
		
当前位置 : 首页 » 文章分类 : 开发 » MFC中文件对话框类CFileDialog详解及文件过滤器说明 上一篇 利用OpenCV从摄像头获得图像的坐标原点是在左下角 下一篇 Word中为 ...
 - VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)
		
转自:http://www.cnblogs.com/javaexam2/archive/2012/07/14/2632959.html ①需要一个别人写好的类CWebPage,将其对于的两个文件Web ...
 - MFC中CString.Format类详解
		
在MFC程序中,使用CString来处理字符串是一个很不错的选择.CString既可以处理Unicode标准的字符串,也可以处理ANSI标准的字符串.CString的Format方法给我们进行字符串的 ...
 - MFC中如何在一个类中调用另一个类的控件
		
学习记录: 两个类,一个为主类 1个为:CCkDlg,主类 1个为: Https,用来做HTTPS请求获得页面状态. 测试界面如下: CCkDlg 类里定义函数 void CCkDlg::printf ...
 - MFC中 自定义类访问主对话框控件的方法
		
之前一直在找有木有好点的方法.现在终于被我找到,收藏之~~~~~~ 在使用mfc的时候经常遇到自定义类访问主对话框控件的问题,例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件.控制对话框的 ...
 
随机推荐
- Linux常用指令---tar | zip (解压缩)
			
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...
 - 发布HTML5 2D游戏引擎YEngine2D
			
关于YEngine2D YEngine2D是一个开源的.采用HTML5技术和Javscript语言创建的2D游戏框架,用来构建web二维游戏. GitHub地址 最新版本 v0.1.2 浏览器支持 C ...
 - html5新增选择器
			
分享点html5的学习笔记,比较基础,突然发现通过写博客来记笔记有很多优点呢,平常记得笔记比较简单,复习起来比较吃力,看自己的博客理解起来还比较轻松,而且只有真正理解了才能表达清楚让别人看懂,还锻炼语 ...
 - vs2015发现一个字符串拼接 bug
			
VS2015支持 字符串拼接 如下: string user="test"; int password=123; string sql=$" user={user};pa ...
 - javascript继承(二)—创建对象的三种模式
			
一.工厂模式 function createPerson(name,age){ var o = {}; o.name = name; o.age = age; o.sayHi = function() ...
 - PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
			
一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...
 - Android的开发环境的发展演变
			
1.Android的开发环境: 之前大家都是安装eclipse,然后再下载安装sdk等插件,还需要配置比较麻烦.不过2013年,有了新的选择,在I/O大会上,谷歌推出新的Android开发环境——An ...
 - 利用Ant脚本生成war包的详细步骤
			
使用ant脚本前的准备 1.下载一个ant安装包.如:apache-ant-1.8.4-bin.zip.解压到E盘. 2.配置环境变量.新增ANT_HOME:E:\apache-ant-1.8.4:P ...
 - Productivity Power Tools 动画演示--给力的插件工具
			
免费的精品: Productivity Power Tools 动画演示 Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率 ...
 - JQuery思维导图