MFC 获得各个类的指针、句柄 ID 和 this指针的使用

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 得到指针
   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; 
===================================================================================================================
补充:this 指针
1、只能在成员函数中使用的指针,它指向调用成员函数的对象,只用非静态成员函数才可以使用this指针。当对象调用非静态成员函数是,
对象地址会作为函数的隐藏参数传入
2、一般而言,关键字this是一个指针,对于一般成员函数,它指向调用该函数的对象,而对于构造函数,它则指向这个正在被创建的对象;

3、使用场合
    1)构造函数中区分同名成员变量和局部变量;
    2)可以作为函数参数,实现对象间的交互;
    3)把this指针作为返回值,多级串连调用;

4、常量型成员函数中的this指针为常量型,以此防止对成员变量的意外修改;
===================================================================================================================
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值