【发布时间】:2025-12-21 17:25:11
【问题描述】:
(标题已更新) 继this 问题之后,现在我对发生的事情有了更清晰的了解......
我有一个没有主窗口的 MFC 应用程序,它公开了一个 API 来创建对话框。当我重复调用其中一些方法时,创建的对话框是彼此为父级的,而不是所有的父级到桌面...我不知道为什么。
但无论如何,即使在创建之后,我也无法将父级更改回 NULL 或 CWnd::GetDesktopWindow()...如果我调用 SetParent 然后调用 GetParent,则没有任何改变。
因此,除了为什么 Windows 神奇地将每个对话框作为父级创建的最后一个对话框这个非常奇怪的问题之外,我还缺少什么能够将这些窗口设置为桌面的子级吗?
更新:我找到了所有这一切的原因,但没有找到解决方案。从我的对话框构造函数中,我们最终得到:
BOOL CDialog::CreateIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd,
void* lpDialogInit, HINSTANCE hInst)
{
ASSERT(lpDialogTemplate != NULL);
if (pParentWnd == NULL)
pParentWnd = AfxGetMainWnd();
m_lpDialogInit = lpDialogInit;
return CreateDlgIndirect(lpDialogTemplate, pParentWnd, hInst);
}
注意:if (pParentWnd == NULL)pParentWnd = AfxGetMainWnd();
我的对话框构造函数的调用堆栈如下所示:
- mfc80d.dll!CDialog::CreateIndirect(const DLGTEMPLATE * lpDialogTemplate=0x005931a8, CWnd * pParentWnd=0x00000000, void * lpDialogInit=0x00000000, HINSTANCE__ * hInst=0x00400000)
- mfc80d.dll!CDialog::CreateIndirect(void * hDialogTemplate=0x005931a8, CWnd * pParentWnd=0x00000000, HINSTANCE__ * hInst=0x00400000)
- mfc80d.dll!CDialog::Create(const char * lpszTemplateName=0x0000009d, CWnd * pParentWnd=0x00000000)
- mfc80d.dll!CDialog::Create(unsigned int nIDTemplate=157, CWnd * pParentWnd=0x00000000)
- MyApp.exe!CMyDlg::CMyDlg(CWnd * pParent=0x00000000)
在调试器中运行,如果我在 CDialog::CreateIndirect 中手动将 pParentWnd 改回 0,一切正常...但我如何首先阻止它发生?
【问题讨论】:
-
调用 SetParent() 是否成功?
-
您没有为原始问题提供任何其他信息。请不要重复问同一个问题,继续你原来的帖子。
-
另一个问题的答案是“Windows 正在互相作为父母”。不过COM等的讨论有点乱。
-
另外,当我将链接放入自己时,您不觉得“可能重复...”有点傻吗? :)