简体   繁体   English

在主对话框中嵌入对话框,并在MFC中单击按钮切换它们

[英]Embedding dialogs in main dialog and switching them with button click in MFC

I have a design like below: 我的设计如下:

在此输入图像描述

So basically, I want to embed three dialogs in the application main dialog and switch between them, for each button click ie, button 1 will show dialog one , button 2 will hide dialog 1 and show dialog 2 .. and so on. 所以基本上,我想在应用程序主对话框中嵌入三个对话框并在它们之间切换,对于每个按钮单击,即按钮1将显示对话框1,按钮2将隐藏对话框1并显示对话框2 ..等等。 Each dialog will be having a different design and functions. 每个对话框都有不同的设计和功能。

I tried using CPropertySheet class to Add pages but its GUI is different. 我尝试使用CPropertySheet类来添加页面,但它的GUI是不同的。 It has either option for navigating the dialogs using next / back button , or from a tab control. 它具有使用下一个/后退按钮或选项卡控件导航对话框的选项。 None of which is as per my requirement. 根据我的要求,这些都不是。

So I want to know is it possible to have a design like this in MFC ? 所以我想知道在MFC中有可能有这样的设计吗? If yes how? 如果有,怎么样? Which Class/ control should I use. 我应该使用哪个类/控件。

Any help will be appreciated. 任何帮助将不胜感激。

What you can do is use a normal CDialog class, add your buttons to it and also create a frame/rect as a placeholder for where your embedded dialogs are to appear. 你可以做的是使用一个普通的CDialog类,添加你的按钮,并创建一个frame / rect作为占位符,用于显示嵌入式对话框的位置。 The following piece of code will create and position your embedded dialog. 以下代码将创建和定位嵌入式对话框。

CRect rect;
CWnd *pHost = GetDlgItem(ID_OF_YOUR_FRAME_RECT);
pHost->GetWindowRect(&rect);
ScreenToClient(&rect);
pDialog->Create(ID_OF_YOUR_DIALOG, this);
pDialog->MoveWindow(&rect);
pDialog->ShowWindow(SW_SHOW);

On button clicks, you hide the previously shown dialog (SW_HIDE) and show your selected dialog(SW_SHOW) with ShowWindow(...). 在按钮单击时,隐藏先前显示的对话框(SW_HIDE)并使用ShowWindow(...)显示所选对话框(SW_SHOW)。

If you create your embedded dialogs with IDD_FORMVIEW style in the add resource editor it'll have the proper styles for embedding. 如果在添加资源编辑器中使用IDD_FORMVIEW样式创建嵌入式对话框,则它将具有适当的嵌入样式。

Another option is probably to use an embedded PropertySheet and hide the tab row and programatically change the tabs on the button clicks. 另一种选择可能是使用嵌入的PropertySheet并隐藏选项卡行并以编程方式更改按钮单击上的选项卡。 I just find it to be too much fuzz with borders, positioning, validation and such for my liking. 我发现它对于边框,定位,验证以及我喜欢的内容都太过模糊了。

If you have the MFC Feature Pack, that first came with VS2008 SP1 and is in all later versions, you might like to consider CMFCPropertySheet . 如果你有MFC功能包,它首先附带VS2008 SP1并且在所有更高版本中,你可能想要考虑CMFCPropertySheet There are a number of examples on the linked page, that are very similar to your design. 链接页面上有许多示例,与您的设计非常相似。

For example, this: 例如,这个:

例1

What worked for me just using dialog based application is SetParent() method. 仅使用基于对话框的应用程序对我有用的是SetParent()方法。 Dont know why nobody mentioned it. 不知道为什么没有人提到它。 It seems to work fine. 它似乎工作正常。 I am doing like below: 我在做如下:

 VERIFY(pDlg1.Create(PanelDlg::IDD, this));
 VERIFY(pDlg2.Create(PanelDlg2::IDD, this));
 VERIFY(pDlg3.Create(PanelDlg2::IDD, this));

   ::SetParent(pDlg1.GetSafeHwnd(), this->m_hWnd);
   ::SetParent(pDlg2.GetSafeHwnd(), this->m_hWnd);
   ::SetParent(pDlg3.GetSafeHwnd(), this->m_hWnd);

Now I can show or hide a child dialog at will (button clicks) as below: 现在,我可以随意显示或隐藏子对话框(按钮点击),如下所示:

   pDlg1.ShowWindow(SW_SHOW);
   pDlg2.ShowWindow(SW_HIDE);
   pDlg3.ShowWindow(SW_HIDE);

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

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