MFC中对话框之间或非对话框与对话框之间的成员变量的访问,得到各种句柄的总结

这几天遇到的问题,总结下,以便之后忘记时看看编程

1.对话框与对话框之间变量的访问多线程

例如:app

前提条件框架

对话框2个:Cdialog1; Cdialog2; ide

Cdialog2为Cdialog1的下一级子对话框函数

Cdialog1 *dlg;工具

//获取父窗口的对话框指针对象学习

dlg = (Cdialog*)GetParent();this

//经过指针对象访问父窗口成员spa

dlg->A = "gyp";//等等

//非对话框与对话框之间的成员访问,或者对话框之间的成员变量,函数之间的访问,此方法通用

HWND hWnd = ::FindWindow(NULL, "分组信息");//"分组信息"为对话框的caption值,第一个参数必须为NULL

if (hWnd)

{

    ::SetTimer(hWnd, 1, 100, 0);//执行后访问拥有hWnd句柄的对话框中的OnTimer函数

    Cdialog* pWnd = (Cdialog*)FromHandle(hWnd); //由句柄获得对话框的对象指针

    pWnd->xxx(); //调用Cdialog中的函数xxx();

}

//如下为转载内容

经过窗口类函数:CWnd *GetWindow得到窗口指针,pWnd->m_hWnd(The handle of the Windows window attached to this  CWnd)在::FromHandle(hWnd);

获取主窗口句柄:       CWnd *wnd  = AfxGetMainWnd();
                     HWND hwnd = wnd->GetSafeHwnd();

设置控件为窗口焦点:GetDlgItem(IDC_TREE1)->SetFocus();

获取控件句柄:HWND hwndctrl = ::GetDlgItem(mainHwnd, IDC_TREE1);//获取树形控件的句柄IDC_TREE IDC_COMBO1

获取当前最上层窗口的句柄: HWND  mainHwnd = ::GetForegroundWindow();//获取当前topmost的窗口句柄
获取当前窗口的焦点句柄:        HWND  currentFocus = ::GetFocus();

MFC中得到各个类的句柄的总结

 

 VC中编程,最大的障碍和问题就是消息机制和指针获取与操做。其实这些内容基本上是每本VC学习工具书上
必讲的内容,并且经过MSDN不少问题都能解决。
面文字主要是我的在编程中指针使用的一些体会,说的不当的地方请指正。
通常咱们使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架,不管是多文档仍是单文档,都存在指针获取和操做问题。
下面这节内容主要是通常的框架,而后再讲多线程中的指针使用。使用到的类须要包含响应的头文件。首先通常得到本类(视,文档,对话框都支持)实例指针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.