简体   繁体   English

如何在另一个CDialog中显示嵌套的CDialog?

[英]How can I display a nested CDialog within another CDialog?

I have two CDialog classes that I have created. 我创建了两个CDialog类。 Let's call them MainDialog and ExtraDialog. 我们称它们为MainDialog和ExtraDialog。 I want ExtraDialog to be able to be displayed both through doModal and as a nested dialog within MainDialog. 我希望ExtraDialog既可以通过doModal显示,也可以作为MainDialog中的嵌套对话框显示。

I can already bring it up separately through a Button and doModal. 我已经可以通过Button和doModal分别提出它。 However, I've been stuck about how to place it within MainDialog. 但是,我一直对如何将其放置在MainDialog中感到困惑。

CWnd* m_pWndStatic = new CWnd;
m_pWndStatic->Create(_T("Something"), _T("Title"), WS_CHILD | WS_VISIBLE, CRect(x, y, xEnd, yEnd), this, idWnd);

CExtraDialog* dlg = new CExtraDialog;
dlg->Create(IDD_NEW_DIALOG, this); //Or second variable can be m_pWndStatic?
//dlg->SetWindowPos(m_pWndStatic, x, y, xEnd, yEnd, SWP_NOZORDER | SWP_NOACTIVATE);
//dlg->Invalidate();
//dlg->ShowWindow(SW_SHOW);
//m_pWndStatic->ShowWindow(SW_SHOW);

Above I shared some of the thigns I have tried. 在上面,我分享了我尝试过的一些尝试。 I was hoping to create a CWnd and put the dialog inside the CWnd but I feel like I am missing something and I couldn't really find anything helpful online. 我希望创建一个CWnd并将对话框放入CWnd中,但是我感觉好像丢失了一些东西,我真的找不到在线上有用的东西。

Edit: I am basically trying to put multiple CWnds into one CDialog, and having the CWnd's run different functionalities from different classes. 编辑:我基本上是试图将多个CWnd放入一个CDialog,并让CWnd运行来自不同类的不同功能。 Kinda like putting lego blocks together. Kinda喜欢将乐高积木拼在一起。

Edit2: I found a question that is kinda similar? Edit2:我发现了一个类似的问题? I hope to make it similar but I just don't want buttons and I want two of them get displayed at once. 我希望使其相似,但是我只是不想使用按钮,而是希望其中两个按钮能够一次显示。 Embedding dialogs in main dialog and switching them with button click in MFC 在主对话框中嵌入对话框并在MFC中单击按钮进行切换

I've been stuck about how to place it within MainDialog. 我一直被困在如何将其放置在MainDialog中。

At the minimum, remove WS_POPUP , WS_CAPTION and WS_SYSMENU styles. 至少删除WS_POPUPWS_CAPTIONWS_SYSMENU样式。 Add the WS_CHILD style. 添加WS_CHILD样式。

It is highly recommended to add the WS_EX_CONTROLPARENT extended style to enable keyboard navigation into and out of the embedded dialog. 强烈建议添加WS_EX_CONTROLPARENT扩展样式,以允许键盘导航进出嵌入式对话框。

For example, in OnInitDialog() of the parent dialog you could add: 例如,在父对话框的OnInitDialog()中,您可以添加:

// Note: Create member variable CExtraDialog, so there is no need for dynamic allocation!
m_extraDlg.Create( IDD_NEW_DIALOG, this );

// Adjust styles. 1st parameter removes, 2nd adds.
m_extraDlg.ModifyStyle( WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, WS_CHILD );

// Adjust extended styles. 1st parameter removes, 2nd adds.
m_extraDlg.ModifyStyleEx( WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE, WS_EX_CONTROLPARENT );

// As we have changed the frame, we let Windows recalculate the non-client area
// by passing the SWP_FRAMECHANGED flag to SetWindowPos().
m_extraDlg.SetWindowPos( nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | 
     SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE );

I was hoping to create a CWnd and put the dialog inside the CWnd 我希望创建一个CWnd并将对话框放入CWnd中

I recommend to always use a CDialog -derived class as the parent of an embedded dialog. 我建议始终使用CDialog派生类作为嵌入式对话框的父级。 This ensures best compatibility with the Windows dialog manager for features like standard keyboard navigation. 这样可以确保与Windows对话框管理器在标准键盘导航等功能方面的最佳兼容性。 You will work with the system, not against it. 您将使用该系统,而不是反对它。

More to read: 更多阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM