关于在模态对话框中获取父窗口对象的方法研究.doc_第1页
关于在模态对话框中获取父窗口对象的方法研究.doc_第2页
关于在模态对话框中获取父窗口对象的方法研究.doc_第3页
关于在模态对话框中获取父窗口对象的方法研究.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

关于在模态对话框中获取父窗口对象的方法研究 姚君 /*/ 测试方法 /*/ 首先,新建一个默认的单文档应用程序。 然后添加对话框资源,并为该对话框资源关联一个类,例如:CSl;为添加菜单,如Test; 为view 类添加成员变量: public: COLORREF m_clr; 在view 类下添加菜单命令函数: void CColrView:OnTest() / TODO: Add your command handler code here Csl dlg(this);/指定当前view 类窗口为模态对话框父窗口 dlg.DoModal(); 在CSl 类中添加消息响应函数(WM_PAINT): void Csl:OnPaint() CPaintDC dc(this); / device context for painting / TODO: Add your message handler code here CColrView * cp=(CColrView *)GetParent();/不能正确的获取父窗口view 类对象指针 CWnd *cw=GetParent(); m_clr=(CColrView *)m_pParentWnd)-m_clr;/可以通过断点debug,发现m_clr 是我们要 的值。 / Do not call CDialog:OnPaint() for painting messages 注意:m_pParentWnd 是在CDialog 类中定义的,该类的头文件是AFXWIN.H ”; protected: CWnd* m_pParentWnd; 为什么调用CWnd 类的GetParent 方法不能正确的获取到父窗口类对象指针的问题留待有 时间认真阅读该函数源代码后再予以说明。 - - 关于GetParent 方法不能正确获取父窗口类对象指针的问题已得到解决, 先将研究结果放置于下方 - - 首先,来看MFC 的源代码,这儿找到的是CWnd* CWnd:GetParentOwner(),该函数源 码位于WINCORE.CPP 中。 CWnd* CWnd:GetParentOwner() const if (GetSafeHwnd() = NULL) / no Window attached return NULL; ASSERT_VALID(this); HWND hWndParent = m_hWnd; HWND hWndT; while (:GetWindowLong(hWndParent, GWL_STYLE) & WS_CHILD) & (hWndT = :GetParent(hWndParent) != NULL) hWndParent = hWndT; return CWnd:FromHandle(hWndParent); 首先,调用了WIN32API 函数GetWindowLong来获取Windows Styles,然后与WS_CHILD 进行&运算,之后又调用全局API 函数GetParent 获取父窗口句柄.注意,这儿是一个While 循环,所以最终找到的将是the most immediate parent or owner window that is not a child window (does not have the WS_CHILD style)的句柄。最后将句柄传给CWnd:FromHandle 函数并将返回的CWnd 指针作为函数的返回值。 因为没有找到GetParent 函数源码,那么就MSDN 一下,在MSDN 中是这样介绍的: Remarks Call this function to get a pointer to a child windows parent window (if any). The GetParent function returns a pointer the immediate parent. In contrast, the GetParentOwner function returns a pointer to the most immediate parent or owner window that is not a child window (does not have the WS_CHILD style). If you have a child window within a child window GetParent and GetParentOwner return different results. 现在让我们回到上边的程序中断点调试。在CMainFrame 类的OnCreate 函数中有一个this 调用,我们就在这儿设置第一个断点;在view 类OnTest 函数设置第二个断点;在CSl 类 OnPaint 函数中设置第三个断点。然后调试运行。 我们会发现指针cp、cw 的值其实就是this 指针的值,也就是指向了框架类窗口对象。所以 我们想要调用m_clr 就会出错。 CWnd:GetParent()函数在view 类方法中调用时可以正确的找到父窗口。 - - 最后再介绍一种方法来解决本题: - - 首先,介绍一个函数: CWnd:FromHandle static CWnd* PASCAL FromHandle( HWND hWnd ); Return Value Returns a pointer to a CWnd object when given a handle to a window. If a CWnd object is not attached to the handle, a temporary CWnd object is created and attached. The pointer may be temporary and should not be stored for later use. Parameters hWnd An HWND of a Windows window. 这是一个静态函数,可以通过窗口句柄返回相关的CWnd 类对象的指针。 好,下面我们来修改下 void Csl:OnPaint() CPaintDC dc(this); / device context for painting / TODO: Add your message handler code here (CColrView *)FromHandle(m_pParentWnd-m_hWnd)-m_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论