


全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一般我们使用的框架是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 FunctionsAfxFreeLibraryDecrements the reference count of the loaded dynamic-link library (DLL) module; when the reference count reaches zero, the module is unmapped.AfxGetAppReturns a pointer to the applications single CWinApp object.AfxGetAppNameReturns a string containing the applications name.AfxGetInstanceHandleReturns an HINSTANCE representing this instance of the application.AfxGetMainWndReturns a pointer to the current main window of a non-OLE application, or the in-place frame window of a server application.AfxGetResourceHandleReturns an HINSTANCE to the source of the applications default resources. Use this to access the applications resources directly.AfxInitRichEditInitializes the version 1.0 rich edit control for the application.AfxInitRichEdit2Initializes the version 2.0 and later rich edit control for the application.AfxLoadLibraryMaps a DLL module and returns a handle that can be used to get the address of a DLL function.AfxRegisterWndClassRegisters a Windows window class to supplement those registered automatically by MFC.AfxSocketInitCalled in a CWinApp:InitInstance override to initialize Windows Sockets.AfxSetResourceHandleSets the HINSTANCE handle where the default resources of the application are loaded.AfxRegisterClassRegisters a window class in a DLL that uses MFC.AfxBeginThreadCreates a new thread.AfxEndThreadTerminates the current thread.AfxGetThreadRetrieves a pointer to the current CWinThread object.AfxWinInitCalled 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.转自:/lihailin560/252689/message.aspx#ID-HANDLE-HWND三者之间的互相转换id-句柄(由ID得到句柄)-hWnd = :GetDlgItem(hParentWnd,id); id-指针(由ID得到指针)-CWnd:GetDlgItem(); 句柄-id(由句柄得到ID)-id = GetW
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论