混合MFC和WPF:模态对话框

时间:2021-07-21 10:45:56

I'm adding C# WPF dialogs to an existing C++ MFC app, using a C++/CLI interface layer. I've got things working, except I'm having a problem with modality. For example:

我正在使用C ++ / CLI接口层将C#WPF对话框添加到现有的C ++ MFC应用程序中。我有事情在工作,除了我有模态问题。例如:

  1. MFC app shows a WPF dialog using ShowDialog. Works as expected.
  2. MFC应用程序使用ShowDialog显示WPF对话框。按预期工作。

  3. That WPF dialog shows a MFC dialog using DoModal. The WPF dialog is hidden behind the base C++ app, and is not disabled unless I manually change IsEnabled. Not ideal, but it works.
  4. 该WPF对话框显示使用DoModal的MFC对话框。 WPF对话框隐藏在基本C ++应用程序后面,除非我手动更改IsEnabled,否则不会禁用。不理想,但它的工作原理。

  5. Now, that MFC dialog is closed. Now for some reason the base MFC app is enabled, when it should still be disabled due to the WPF dialog not having been closed. That's bad, as it now allows the user to do crazy things while the WPF dialog is still open.
  6. 现在,该MFC对话框已关闭。现在出于某种原因启用了基本MFC应用程序,因为WPF对话框尚未关闭,因此仍应禁用它。这很糟糕,因为它现在允许用户在WPF对话框仍然打开时做疯狂的事情。

I have a feeling that it would work better if I could set parent dialogs correctly. But so far I havent been able to set an MFC dialog's parent as a WPF dialog, or vice versa. And, I don't even know if that'd fix it.

我觉得如果我能正确设置父对话框会更好。但到目前为止,我还没有能够将MFC对话框的父级设置为WPF对话框,反之亦然。而且,我甚至都不知道是否能解决这个问题。

Any ideas?

2 个解决方案

#1


4  

When opening a CDialog, the trick is to use a WindowsInteropHelper to get the parent WPF dialog's HWND. Then, you can use CWnd::Attach to wrap that HWND in a CWnd class to pass to the CDialog's constructor.

打开CDialog时,诀窍是使用WindowsInteropHelper来获取父WPF对话框的HWND。然后,您可以使用CWnd :: Attach将CWND包装在CWnd类中以传递给CDialog的构造函数。

The problem I had was that I already had the CDialog constructed., but not yet displayed. The various versions of SetParent can only be used if your target child window already has a valid handle. I had to write a new function in my CDialog class to set m_wndParent, which is what it uses as the parent when it finally creates the dialog. Then everything works great!

我遇到的问题是我已经构建了CDialog,但尚未显示。只有当目标子窗口具有有效句柄时,才能使用各种版本的SetParent。我必须在我的CDialog类中编写一个新函数来设置m_wndParent,这是它在最终创建对话框时用作父项的内容。一切都很棒!

Somehow creating WPF dialogs from MFC dialogs "just works". It's magic.

以某种方式从MFC对话框创建WPF对话框“只是工作”。这是魔法。

#2


2  

When showing the WPF dialog, are you using the HwndSource class to wrap the WPF window? If so, you may be able to ::SetParent the WPF window as well as use the HwndSource.Handle property to set the sub-child's parent.

在显示WPF对话框时,您是否使用HwndSource类来包装WPF窗口?如果是这样,您可以:: SetParent WPF窗口以及使用HwndSource.Handle属性来设置子子的父级。

#1


4  

When opening a CDialog, the trick is to use a WindowsInteropHelper to get the parent WPF dialog's HWND. Then, you can use CWnd::Attach to wrap that HWND in a CWnd class to pass to the CDialog's constructor.

打开CDialog时,诀窍是使用WindowsInteropHelper来获取父WPF对话框的HWND。然后,您可以使用CWnd :: Attach将CWND包装在CWnd类中以传递给CDialog的构造函数。

The problem I had was that I already had the CDialog constructed., but not yet displayed. The various versions of SetParent can only be used if your target child window already has a valid handle. I had to write a new function in my CDialog class to set m_wndParent, which is what it uses as the parent when it finally creates the dialog. Then everything works great!

我遇到的问题是我已经构建了CDialog,但尚未显示。只有当目标子窗口具有有效句柄时,才能使用各种版本的SetParent。我必须在我的CDialog类中编写一个新函数来设置m_wndParent,这是它在最终创建对话框时用作父项的内容。一切都很棒!

Somehow creating WPF dialogs from MFC dialogs "just works". It's magic.

以某种方式从MFC对话框创建WPF对话框“只是工作”。这是魔法。

#2


2  

When showing the WPF dialog, are you using the HwndSource class to wrap the WPF window? If so, you may be able to ::SetParent the WPF window as well as use the HwndSource.Handle property to set the sub-child's parent.

在显示WPF对话框时,您是否使用HwndSource类来包装WPF窗口?如果是这样,您可以:: SetParent WPF窗口以及使用HwndSource.Handle属性来设置子子的父级。