2023年6月22日发(作者:)
MFC中获取各种指针、句柄的⽅法最近有些⼈在问MFC编程⼀些要点,有⼀些句柄的获取、指针的获取是常见的问题,本⽂将对这些问题做以解释,参考了前⼈的笔录(见reference),希望能够帮助⼤家更⽅便地进⾏MFC程序开发。
⼀般我们使⽤的框架是VC提供的Wizard⽣成的MFC App Wizard(exe)框架,⽆论是多⽂档还是单⽂档,都存在指针和句柄获取和操作问题。本⽂中将针对各类句柄的获得、指针的获得以及MFC中常见应⽤进⾏阐述并举例。
本⽂内容索引:=========================================================MFC中获取常见类句柄<视图类,⽂档类,框架类,应⽤程序类>MFC中获取窗⼝句柄及相关函数
MFC获取控件句柄MFC各类中获取类指针详解 MSDN关于应⽤程序信息和管理的各个函数==========================================================
MFC中获取常见类句柄<视图类,⽂档类,框架类,应⽤程序类>
本节为VC中常⽤的⽂档类,视图类,框架类,应⽤程序类,⾃定义类中获取其它四个类的⽅法:
GET App
AfxGetInstanceHandle()
AfxGetApp()
GET Frame->View->Document
SDI AfxGetMainWnd() -> GetActiveView() -> GetDocument()
MDI AfxGetMainWnd() -> MDIGetActive() -> GetActiveView() -> GetDocument()
GET Menu
CMenu *pMenu=AfxGetApp()->m_pMainWnd->GetMenu();
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);
Get View from Document
GetFirstViewPosition 和 GetNextView 函数得到指针。
MFC中获取窗⼝句柄及相关函数
⾸先,窗⼝句柄,在窗⼝类中直接使⽤成员变量m_hWnd,在窗⼝外最常见是⽤AfxGetMainWnd (获取主窗⼝指针,其成员变量m_hWnd为主窗⼝句柄):
HWND hWnd = AfxGetMainWnd()->m_hWnd;
与其相关的函数说明如下,这些函数对于获取窗⼝句柄⾮常有⽤:GetTopWindow函数功能:该函数检查与特定⽗窗⼝相联的⼦窗⼝z序(Z序:垂直屏幕的⽅向,即叠放次序),并返回在z序顶部的⼦窗⼝的句柄。函数原型:HWND GetTopWindow(HWND hWnd);参数: hWnd:被查序的⽗窗⼝的句柄。如果该参数为NULL,函数返回Z序顶部的窗⼝句柄。返回值: 如果函数成功,返回值为在Z序顶部的⼦窗⼝句柄。如果指定的窗⼝⽆⼦窗⼝,返回值为NULL。GetForegroundWindow函数功能:该函数返回当前系统的前台窗⼝的窗⼝句柄。函数原型:HWND GetForegroundWindow(VOID) 返回值:函数返回前台窗回的句柄。☆☆☆ GetActiveWindow 获取当前窗⼝句柄函数功能:该函数可以获得与调⽤该⽅法的线程的消息队列相关的活动窗⼝的窗⼝句柄(就是取得当前进程的活动窗⼝的窗⼝句柄)。函数原型:HWND GetActiveWindow(VOID)返回值:返回值是与调⽤线程的消息队列相关的活动窗⼝的句柄。否则,返回值为NULL。GetSafeHwnd函数功能:获取某个窗⼝对象(CWnd的派⽣对象)指针的句柄(HWND)时,最安全的⽅法是使⽤GetSafeHwnd()函数。通过下⾯的例⼦来看其理由: CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器[cpp]
1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器
2. HWND hwnd = pwnd->m_hwnd; //得到它的HWND
HWND hwnd = pwnd->m_hwnd; //得到它的HWND 这样的代码当开始得到的pwnd为空的时候就会出现⼀个“General protection error”,并关闭应⽤程序,因为⼀般不能对⼀个NULL指针访问其成员,如果⽤下⾯的代码: [cpp]
1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器
2. HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND
就不会出现问题,因为尽管当pwnd是NULL时,GetSafeHwnd仍然可以⽤,只是返回NULLIsWindowVisible函数功能:该函数获得给定窗⼝的可视状态。函数原型:BOOL IsWindowVisible(HWND hWnd);参数; hWnd:被测试窗⼝的句柄。返回值: 如果指定的窗⼝及其⽗窗⼝具有WS_VISIBLE风格,返回值为⾮零;如果指定的窗⼝及其⽗窗⼝不具有WS_VISIBLE风格,返回值为零。由于返回值表明了窗⼝是否具有Ws_VISIBLE风格,因此,即使该窗⼝被其他窗⼝遮盖,函数返回值也为⾮零。备注: 窗⼝的可视状态由WS_VISIBLE位指⽰。当设置了WS_VISIBLE位,窗⼝就可显⽰,⽽且只要窗⼝具有WS_VISIBLE风格,任何画在窗⼝的信息都将被显⽰。IsWindow:函数功能:该函数确定给定的窗⼝句柄是否标⽰⼀个已存在的窗⼝。 函数原型:BOOL IsWindow(HWND hWnd);参数: hWnd:被测试窗⼝的句柄。返回值: 如果窗⼝句柄标识了⼀个已存在的窗⼝,返回值为TURE;如果窗⼝句柄未标识⼀个已存在窗⼝,返回值为FALSE。
FindWindow:HWND FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName );参数:lpClassName 指向⼀个以null结尾的、⽤来指定类名的字符串或⼀个可以确定类名字符串的原⼦。如果这个参数是⼀个原⼦,那么它必须是⼀个在调⽤此函数前已经通过GlobalAddAtom函数创建好的全局原⼦。这个原⼦(⼀个16bit的值),必须被放置在lpClassName的低位字节中,lpClassName的⾼位字节置零。
lpWindowName 指向⼀个以null结尾的、⽤来指定窗⼝名(即窗⼝标题)的字符串。如果此参数为NULL,则匹配所有窗⼝名。返回值:如果函数执⾏成功,则返回值是拥有指定窗⼝类名或窗⼝名的窗⼝的句柄。 如果函数执⾏失败,则返回值为 NULL 。可以通过调⽤GetLastError函数获得更加详细的错误信息。
来说个应⽤,窗⼝标题的改变,我们可以通过SetWindowText来实现:注:如果窗⼝本⾝属性是不显⽰标题的,这个函数的调⽤不会影响窗⼝属性。[cpp]
1. //Set title for application’s main frame window .
2. AfxGetMainWnd ( ) -> SetWindowText (_T("Application title") )
3. //Set title for View’s MDI child frame window .
4. GetParentFrame ( ) -> SetWindowText ("_T ("MDI Child Frame new title") )
5. //Set title for dialog’s push button control.
6. GetDigitem (IDC_BUTTON) -> SetWindowText (_T ("Button new title ") )
MFC获取控件句柄SDI中的控件句柄获取:
[cpp]
1. CWnd *pWnd = GetDlgItem(ID_***); // 取得控件的指针
2. HWND hwnd = pWnd->GetSafeHwnd(); // 取得控件的句柄
取得CDC的指针是CDC* pdc = pwnd->GetWindowDC();
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()就是你定义的类中间的成员。MSDN关于应⽤程序信息和管理的各个函数
When you write an application, you create a single CWinApp-derived object. Attimes, you may want to get information about this objectfrom outside theCWinApp-derived Microsoft Foundation Class Library provides the following global functionsto help you accomplish these tasks:Application Information and Management FunctionsAfxFreeLibraryDecrements the reference count of the loaded dynamic-link library (DLL) module;when the reference count reaches zero, the module AppReturns a pointer to the application's single CWinApp AppNameReturns a string containing the application's InstanceHandleReturns an HINSTANCE representing this instance of the MainWndReturns a pointer to the current "main" window of a non-OLEapplication, or the in-place frame window of a server ResourceHandleReturns an HINSTANCE to the source of the application's default resources. Usethis to access the application's resources tRichEditInitializes the version 1.0 rich edit control for the tRichEdit2Initializes the version 2.0 and later rich edit control for the dLibraryMaps a DLL module and returns a handle that can be used to get the address of aDLL isterWndClassRegisters a Windows window class to supplement those registered automaticallyby ketInitCalled in a CWinApp::InitInstance override to initialize Windows ResourceHandleSets the HINSTANCE handle where the default resources of the application isterClassRegisters a window class in a DLL that uses inThreadCreates a new ThreadTerminates the current ThreadRetrieves a pointer to the current CWinThread InitCalled by the MFC-supplied WinMain function, as part of the CWinAppinitialization of a GUI-based application, to initialize MFC. Must becalleddirectly for console applications using MFC.
发布者:admin,转转请注明出处:http://www.yc00.com/web/1687428461a9316.html
评论列表(0条)