混合 MFC 和 WPF:模态对话框
我使用 C++/CLI 界面层将 C# WPF 对话框添加到现有的 C++ MFC 应用程序。 我一切正常,只是我遇到了形式问题。 例如:
- MFC 应用程序使用 ShowDialog 显示 WPF 对话框。 按预期工作。
- 该 WPF 对话框显示使用 DoModal 的 MFC 对话框。 WPF 对话框隐藏在基本 C++ 应用程序后面,并且不会被禁用,除非我手动更改 IsEnabled。 不理想,但它有效。
- 现在,该 MFC 对话框已关闭。 现在,由于某种原因,基本 MFC 应用程序已启用,而由于 WPF 对话框尚未关闭,它仍应被禁用。 这很糟糕,因为它现在允许用户在 WPF 对话框仍然打开的情况下执行疯狂的操作。
我有一种感觉,如果我能正确设置父对话框,效果会更好。 但到目前为止,我还无法将 MFC 对话框的父级设置为 WPF 对话框,反之亦然。 而且,我什至不知道这是否能解决问题。
有任何想法吗?
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:
- MFC app shows a WPF dialog using ShowDialog. Works as expected.
- 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.
- 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.
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.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打开 CDialog 时,技巧是使用 WindowsInteropHelper 来获取父 WPF 对话框的 HWND。 然后,您可以使用 CWnd::Attach 将该 HWND 包装在 CWnd 类中以传递给 CDialog 的构造函数。
我遇到的问题是我已经构建了 CDialog,但尚未显示。 仅当您的目标子窗口已经具有有效句柄时,才能使用各种版本的 SetParent。 我必须在 CDialog 类中编写一个新函数来设置 m_wndParent,这是它最终创建对话框时用作父级的函数。 然后一切都很好!
不知何故,从 MFC 对话框创建 WPF 对话框“正常”。 这是魔法。
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.
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!
Somehow creating WPF dialogs from MFC dialogs "just works". It's magic.
显示 WPF 对话框时,您是否使用 HwndSource 类包装WPF窗口? 如果是这样,您可以::SetParent WPF 窗口以及使用 HwndSource.Handle 属性来设置子子窗口的父窗口。
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.