MFC 获得各类指针、句柄的方法(转)
原文转自 https://blog.csdn.net/abcjennifer/article/details/7480019
1、MFC中获取常见类句柄<视图类,文档类,框架类,应用程序类>
本节为VC中常用的文档类,视图类,框架类,应用程序类,自定义类中获取其它四个类的方法:
(1) GET App
AfxGetInstanceHandle()
AfxGetApp()
(2) GET Frame->View->Document
SDI AfxGetMainWnd() -> GetActiveView() -> GetDocument()
MDI AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument()
(3) GET Menu
CMenu *pMenu=AfxGetApp()->m_pMainWnd->GetMenu();
(4) GET ToolBar,StatusBar
(CMainFrame *)GetParent()->m_wndToolBar;
(CMainFrame *)GetParent()->m_wndStatusBar;
CStatusBar * pStatusBa=(CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);
CToolBar * pToolBar=(CtoolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);
(5) Get View from Document
GetFirstViewPosition 和 GetNextView 函数得到指针。
2、MFC中获取窗口句柄及相关函数
首先,窗口句柄,在窗口类中直接使用成员变量m_hWnd,在窗口外最常见是用AfxGetMainWnd (获取主窗口指针,其成员变量m_hWnd为主窗口句柄): HWND hWnd = AfxGetMainWnd()->m_hWnd;
与其相关的函数说明如下,这些函数对于获取窗口句柄非常有用:
(1) GetTopWindow
函数功能:该函数检查与特定父窗口相联的子窗口z序(Z序:垂直屏幕的方向,即叠放次序),并返回在z序顶部的子窗口的句柄。
函数原型:HWND GetTopWindow(HWND hWnd);
参数:
hWnd:被查序的父窗口的句柄。如果该参数为NULL,函数返回Z序顶部的窗口句柄。
返回值:
如果函数成功,返回值为在Z序顶部的子窗口句柄。如果指定的窗口无子窗口,返回值为NULL。
(2) GetForegroundWindow
函数功能:该函数返回当前系统的前台窗口的窗口句柄。
函数原型:HWND GetForegroundWindow(VOID)
返回值:函数返回前台窗回的句柄。
(3) GetActiveWindow 获取当前窗口句柄
函数功能:该函数可以获得与调用该方法的线程的消息队列相关的活动窗口的窗口句柄(就是取得当前进程的活动窗口的窗口句柄)。
函数原型:HWND GetActiveWindow(VOID)
返回值:返回值是与调用线程的消息队列相关的活动窗口的句柄。否则,返回值为NULL。
(4) GetSafeHwnd
函数功能:获取某个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数。
通过下面的例子来看其理由:
CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器
HWND hwnd = pwnd->m_hwnd; //得到它的HWND
这样的代码当开始得到的pwnd为空的时候就会出现一个“General protection error”,并关闭应用程序,因为一般不能对一个NULL指针访问其成员,如果用下面的代码:
CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器
HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND
就不会出现问题,因为尽管当pwnd是NULL时,GetSafeHwnd仍然可以用,只是返回NULL。
(5) IsWindowVisible
函数功能:该函数获得给定窗口的可视状态。
函数原型:BOOL IsWindowVisible(HWND hWnd);
参数;
hWnd:被测试窗口的句柄。
返回值:
如果指定的窗口及其父窗口具有WS_VISIBLE风格,返回值为非零;如果指定的窗口及其父窗口不具有WS_VISIBLE风格,返回值为零。由于返回值表明了窗口是否具有Ws_VISIBLE风格,因此,即使该窗口被其他窗口遮盖,函数返回值也为非零。
备注:
窗口的可视状态由WS_VISIBLE位指示。当设置了WS_VISIBLE位,窗口就可显示,而且只要窗口具有WS_VISIBLE风格,任何画在窗口的信息都将被显示。
(6) IsWindow:
函数功能:该函数确定给定的窗口句柄是否标示一个已存在的窗口。
函数原型:BOOL IsWindow(HWND hWnd);
参数:
hWnd:被测试窗口的句柄。
返回值:
如果窗口句柄标识了一个已存在的窗口,返回值为TURE;如果窗口句柄未标识一个已存在窗口,返回值为FALSE。
(7) FindWindow:
HWND FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName );
参数:
lpClassName
指向一个以null结尾的、用来指定类名的字符串或一个可以确定类名字符串的原子。如果这个参数是一个原子,那么它必须是一个在调用此函数前已经通过GlobalAddAtom函数创建好的全局原子。这个原子(一个16bit的值),必须被放置在lpClassName的低位字节中,lpClassName的高位字节置零。
lpWindowName
指向一个以null结尾的、用来指定窗口名(即窗口标题)的字符串。如果此参数为NULL,则匹配所有窗口名。
返回值:
如果函数执行成功,则返回值是拥有指定窗口类名或窗口名的窗口的句柄。如果函数执行失败,则返回值为 NULL 。可以通过调用GetLastError函数获得更加详细的错误信息。
(8) 来说个应用,窗口标题的改变,我们可以通过SetWindowText来实现:
注:如果窗口本身属性是不显示标题的,这个函数的调用不会影响窗口属性。
//Set title for application’s main frame window .
AfxGetMainWnd ( ) -> SetWindowText (_T("Application title") )
//Set title for View’s MDI child frame window .
GetParentFrame ( ) -> SetWindowText ("_T ("MDI Child Frame new title") )
//Set title for dialog’s push button control.
GetDigitem (IDC_BUTTON) -> SetWindowText (_T ("Button new title ") )
3、MFC获取控件句柄
SDI中的控件句柄获取:
CWnd *pWnd = GetDlgItem(ID_***); // 取得控件的指针
HWND hwnd = pWnd->GetSafeHwnd(); // 取得控件的句柄
取得CDC的指针是CDC* pdc = pwnd->GetWindowDC();
4、MFC各类中获取类指针详解
使用到的类需要包含响应的头文件。首先一般获得本类(视,文档,对话框都支持)实例指针 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得到指针
最后提醒大家,在提取到各个句柄之后,因为初次提取的都是标准类句柄,所以,在使用时要注意将标准句柄转换成自己的类的句柄。
如:AfxGetApp();//得到的是WinApp类的句柄,所以操作前记得转换成自己定义的类的句柄。如:((CMyApp*)AfxGetApp())->XXXX();//这的xxxx()就是你定义的类中间的成员。
5、MSDN关于应用程序信息和管理的各个函数
When you write an application, you create a single CWinApp-derived object. Attimes, you may want to get information about this object from outside theCWinApp-derived object.
The Microsoft Foundation Class Library provides the following global functionsto 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-OLEapplication, or the in-place frame window of a server application.
AfxGetResourceHandle
Returns an HINSTANCE to the source of the application's default resources. Usethis 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 aDLL function.
AfxRegisterWndClass
Registers a Windows window class to supplement those registered automaticallyby MFC.
AfxSocketInit
Called in a CWinApp::InitInstance override to initialize Windows Sockets.
AfxSetResourceHandle
Sets the HINSTANCE handle where the default resources of the application areloaded.
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 CWinAppinitialization of a GUI-based application, to initialize MFC. Must be calleddirectly for console applications using MFC.
MFC 获得各类指针、句柄的方法(转)的更多相关文章
- MFC获取各类指针句柄
最近有些人在问MFC编程一些要点,有一些句柄的获取.指针的获取是常见的问题,本文将对这些问题做以解释,参考了前人的笔录(见reference),希望能够帮助大家更方便地进行MFC程序开发. 一般我们使 ...
- 【转】MFC窗口句柄各类指针获取函数
原文网址:http://www.pythonschool.com/CPP_JHK/5003.html 获取所在类窗口的句柄 this->m_hwnd; // this是一个指针,指向当前类的实例 ...
- MFC中获取各个窗口之间的句柄或者指针对象的方法
MFC在非常多的对话框操作中,我们常常要用到在一个对话框中调用还有一个对话框的函数或变量.能够用例如以下方法来解决. HWND hWnd=::FindWindow(NULL,_T("S ...
- MFC获取各窗口指针句柄
MFC在很多的对话框操作中,我们经常要用到在一个对话框中调用另一个对话框的函数或变量.可以用如下方法来解决. HWND hWnd=::FindWindow(NULL,_T("Sphere ...
- (转)MFC中获得各个类的指针/句柄 ID的总结
http://www.cnblogs.com/ylhome/archive/2009/10/06/1578478.html 一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard ...
- MFC中的CString类使用方法指南
MFC中的CString类使用方法指南 原文出处:codeproject:CString Management [禾路:这是一篇比较老的资料了,但是对于MFC的程序设计很有帮助.我们在MFC中使用字符 ...
- 文件指针/句柄(FILE*)、文件描述符(fd)以及 文件路径(filepath)的相互转换(转)
转自: http://blog.csdn.net/jenghau/article/details/5532265 文件指针/句柄(FILE*).文件描述符(fd)以及 文件路径(filepath)的相 ...
- C#中遍历各类数据集合的方法总结
C#中遍历各类数据集合的方法总结: 1.枚举类型 //遍历枚举类型Sample的各个枚举名称 foreach (string sp in Enum.GetNames(typeof(Sample))) ...
- 【.NET】C#中遍历各类数据集合的方法
[.NET]C#中遍历各类数据集合的方法 C#中遍历各类数据集合的方法,这里自己做下总结: 1.枚举类型 //遍历枚举类型Sample的各个枚举名称 ...
随机推荐
- POJ 3171 区间最小花费覆盖 (DP+线段树
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4245 Accepted: 1429 D ...
- hadoop中节点上的nodemanager一直启动不起来
当我们启动Hadoop集群的时候,发现有一台机器的nodemanager启动后自动关闭, 查看日志的时候发现有错误:yarn-root-nodemanager-log 解决办法: netstat a ...
- linux下创建用户 费元星站长
linux下创建用户(一) Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系 ...
- laravel5.5事件系统
目录 1 注册事件和监听器 2 定义事件 3 定义监听器 4 分发事件 更多使用方法 1. 可以手动注册事件 2. 事件监听器中调用队列 3.事件订阅者 1 注册事件和监听器 1.修改EventSer ...
- DNSSec
Domain Name System Security Extensions (DNSSEC)DNS安全扩展,是由IETF提供的一系列DNS安全认证的机制(可参考RFC2535).它提供了一种来源鉴定 ...
- 剑指Offer - 九度1520 - 树的子结构
剑指Offer - 九度1520 - 树的子结构2013-11-30 22:17 题目描述: 输入两颗二叉树A,B,判断B是不是A的子结构. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每 ...
- 百度webuploader上传 1
百度webupload网址:http://fex.baidu.com/webuploader/ 引入js和css <script src="../../Content/webuploa ...
- java初学2
1.数组操作类Arrays与System public static void arraycopy(Object src, int srcPos, Object dest,int destPos,in ...
- Python网络编程(子进程的创建与处理、简单群聊工具)
前言: 昨天我们已经了解了多进程的原理以及它的实际使用 Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函数调用,调用一次,返回一次, 但是fork()调用一次,返回两次 ...
- 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...