面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0

最近有些人在问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); //希望找到资源管理器

  1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器  
  2.   HWND hwnd = pwnd->m_hwnd; //得到它的HWND  

       HWND hwnd = pwnd->m_hwnd; //得到它的HWND

  这样的代码当开始得到的pwnd为空的时候就会出现一个“General protection error”,并关闭应用程序,因为一般不能对一个NULL指针访问其成员,如果用下面的代码:  
  
  1. CWnd *pwnd = FindWindow(“ExploreWClass”,NULL); //希望找到资源管理器  
  2.   HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND  

  就不会出现问题,因为尽管当pwnd是NULL时,GetSafeHwnd仍然可以用,只是返回NULL


IsWindowVisible
函数功能:该函数获得给定窗口的可视状态。
函数原型: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来实现:

注:如果窗口本身属性是不显示标题的,这个函数的调用不会影响窗口属性。

  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中的控件句柄获取:

  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 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.

本文转自:http://blog.csdn.net/tianmo2010/article/details/7843332


其他链接:http://www.cnblogs.com/carekee/articles/1835979.html

1、获取应用程序指针

  CMyApp* pApp=(CMyApp*)AfxGetApp();

2、获取主框架指针

  CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针

  CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);

  或者

  CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

3、获取菜单指针

  CMenu* pMenu = AfxGetMainWnd()->GetMenu();

4、获取工具栏、状态栏指针

  主框架中可以直接使用m_wndToolBar、m_wndStatusBar

  其他:

  CToolBar* pToolBar = (CToolBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR);

  CStatusBar* pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR);

5、获取控件指针

  先用 GetDlgItem() 再转换,如:

  CButton* pButton = (CButton*)GetDlgItem(IDC_MYBUTTON);

6、获取文档、视图指针

SDI:

CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

CYourDoc* pDoc = (CYourDoc*)pMainFrame->GetActiveDocument();

CYourView* pView = (CYourView*)pMainFrame->GetActiveView();

MDI:

CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();

CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame();

CYourDoc* pDoc = (CYourDoc*)pChildFrame->GetActiveDocument();

CYourView* pView = (CYourView*)pChildFrame->GetActiveView();

7、文档、视图

从视图获取文档指针:

CYourDoc* pDoc = GetDocument();

从文档获取视图指针:

利用成员函数 GetFirstViewPosition() 和 GetNextView() 遍历

virtual POSITION GetFirstViewPosition() const;

virtual CView* GetNextView(POSITION& rPosition) const;

SDI:

CYourView* pView;

POSITION pos = GetFirstViewPosition();

pView = GetNextView(pos);

MDI:

定义函数

CView* CYourDoc::GetView(CRuntimeClass* pClass)

{

    CView* pView;

    POSITION pos=GetFirstViewPosition();

    while(pos!=NULL)

    {

        pView=GetNextView(pos);

         if(!pView->IsKindOf(pClass))

             break;

    }

    if(!pView->IsKindOf(pClass))

    {

        AfxMessageBox("Connt Locate the View.");

       return NULL;

    }

    return pView;

}

使用如下:

CYourView* pView=(CYourView*)GetView(RUNTIME_CLASS(CYourView));

8、文档模版、文档

从文档获取文档模版指针:

CDocTemplate* GetDocTemplate() const;

从文档模版获取文档指针:

viaual POSITION GetFirstDocPosition( ) const = 0;

visual CDocument* GetNextDoc(POSITION & rPos) const = 0;

9、获取分割视图中各个视图的指针

主框架中定义:CSplitterWnd m_wndSplitter;

定义两个View类:CView1、CView2

框架类中重载:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext)

{

    VERIFY(m_splitter.CreateStatic(this,2,1)); //分割成两行一列

    VERIFY(m_splitter.CreateView(0,0,RUNTIME_CLASS(CView1),CSize(100,100),pContext));

    VERIFY(m_splitter.CreateView(1,0,RUNTIME_CLASS(CView2),CSize(100,100),pContext));

    return TRUE;

}

获取分割视图指针

CView1* pView1 = (CView1*)m_wndSplitter.GetPane(0,0);

CView2* pView2 = (CView2*)m_wndSplitter.GetPane(1,0);

10、通过鼠标获得子窗口指针

CWnd* ChildWindowFromPoint(POINT point) const;

CWnd* ChildWindowFromPoint(POINT point,UINT nFlags) const;

用于确定包含指定点的子窗口

如果指定点在客户区之外,函数返回NULL;

如果指定点在客户区内,但是不属于任何一个子窗口,函数返回该CWnd的指针;

如果有多个子窗口包含指定点,则返回第一个子窗口的指针。

还要注意的是,该函数返回的是一个伪窗口指针,不能将它保存起来供以后使用。

对于第二个参数nFlags有几个含义:

CWP_ALL             file://不忽略任何子窗口

CWP_SKIPNIVSIBLE    file://忽略不可见子窗口

CWP_SKIPDISABLED    file://忽略禁止的子窗口

CWP_SKIPRANSPARENT file://忽略透明子窗口

在获取视类的指针时,需要在需要获取类的cpp文件前面加入#include "***View.h",这样编译时会报错,解决方法是在视类的cpp文件前面加入#include "***Doc.h"

posted on 2012-09-03 17:41 王海光 阅读(1110) 评论(0)  编辑 收藏 引用 所属分类: MFC

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理