2

I have two CDialog classes that I have created. Let's call them MainDialog and ExtraDialog. I want ExtraDialog to be able to be displayed both through doModal and as a nested dialog within MainDialog.

I can already bring it up separately through a Button and doModal. However, I've been stuck about how to place it within 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.

Edit: I am basically trying to put multiple CWnds into one CDialog, and having the CWnd's run different functionalities from different classes. Kinda like putting lego blocks together.

Edit2: I found a question that is kinda similar? 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

Kemal Tezer Dilsiz
  • 3,739
  • 5
  • 24
  • 43
  • Has your inner `CDialog` Style= Child? And Visible=True? – sergiol Dec 07 '17 at 17:58
  • I haven't done anything other than what I posted in the question. Perhaps not? If I do dlg->ShowWindow(SW_WHOW), it pops it up in a separate window which isn't what I want. Sorry if I couldn't understand your question, I'm fairly new to MFC – Kemal Tezer Dilsiz Dec 07 '17 at 18:05
  • Just put the code from the other question you found into your OnInitDialog function instead of a button handler. It can be repeated for each embedded dialog you need. – Mark Ransom Dec 07 '17 at 18:09
  • I'll try that and leave this question on until then! After (if) I (can) get it working, i'll post a solution for my own question if I can get it to work. Thank you – Kemal Tezer Dilsiz Dec 07 '17 at 18:13
  • It displayed it as another Dialog inside the dialog but I want it to be fixed window that doesn't move around, kinda like a group box, a fixed simple section of display. – Kemal Tezer Dilsiz Dec 07 '17 at 19:42
  • You need to set the proper styles on the child dialog window. You now have an answer that gives the details. – Mark Ransom Dec 07 '17 at 22:47

1 Answers1

3

I've been stuck about how to place it within MainDialog.

At the minimum, remove WS_POPUP, WS_CAPTION and WS_SYSMENU styles. Add the WS_CHILD style.

It is highly recommended to add the WS_EX_CONTROLPARENT extended style to enable keyboard navigation into and out of the embedded dialog.

For example, in OnInitDialog() of the parent dialog you could add:

// 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

I recommend to always use a CDialog-derived class as the parent of an embedded dialog. This ensures best compatibility with the Windows dialog manager for features like standard keyboard navigation. You will work with the system, not against it.

More to read:

zett42
  • 25,437
  • 3
  • 35
  • 72
  • I've combined your answer with the link I shared in my 2nd Edit and I have also used SetWindowPos for some extra properties: from the following link: https://msdn.microsoft.com/en-us/library/a1yzfz6d.aspx – Kemal Tezer Dilsiz Dec 08 '17 at 13:24
  • @KemalTezerDilsiz Yeah, it's a good idea to make Windows recalculate the NC area after we have removed the border. – zett42 Dec 08 '17 at 13:28