MSDN 提示CFileDialog的GetDlgItem()要在前面加上GetParent(),看了MSDN的一段解释,不是很懂,MSND上关于GetDlgItem()的说明就更短了,没讲出什么,小弟初学VC,还望前辈指点。到底什么情况下GetDlgItem()可以直接用,什么情况下要用GetParent(),谢谢!!

解决方案 »

  1.   

    一般GetDlgItem()都可以直接用MSDN上的那一段是说如果你要改变打开文件对话框的缺省行为想要操作对话框上的控件,比如确定按钮需要用 dlg.GetParent()->GetDlgItem(IDOK)一般很少需要这么做也不推荐这么做
      

  2.   

    18. CFileDialog is always a child of the Explorer dialog window: When you use the Explorer-style CFileDialog (which in Windows 95 you are doing by default), MFC 4.0 assumes the Explorer model of customization. This implies that custom improvements to the File dialog are included on a separate template that is added around the standard Explorer dialog. In MFC 4.0, the actual CFileDialog window is a child dialog of the main File Common Dialog, even if you are not providing a template to customize the dialog. Therefore, if you have a need to alter the standard Explorer interface by moving or hiding controls, prefix all GetDlgItem() calls to Explorer controls with GetParent(). For example, this expression:    GetParent()->GetDlgItem(IDOK) will return a pointer to the Open/Save button on the Explorer dialog. However, this is not recommended because code that relies on the details of the standard Explorer dialog controls will break if the Explorer layout is changed in the future. This default can be changed by removing the OFN_EXPLORER style. For more information, see the following Microsoft Knowledge Base article: Q131225 PRB: CFileDialog::DoModal() Does Not Display FileOpen Dialog 
      

  3.   

    先谢了。
    我就是看了这一段。就是不明白为什么这种情况需要用。那个“Therefore”我不太明白!!
      

  4.   

    首先 Win32 本身有个 API 提供打开文件对话框然后 MFC 的实现是在这个对话框里嵌入一个子对话框但是用户界面上的 ok 和 cancel 按钮还是外层对话框的儿子明白了吗?
      

  5.   

    GetDlgItem()你知道它的源代码是什么样的形式吗
    通过调用api ::GetDlgItem(hWnd,idChild) 返回子窗口的句柄然后再由FromHandlePermanent,取得子窗口的mfc对象知道这些了应该知道什么情况下用GetParent,什么情况下不用吧