顶部WPF窗口不会随着Window Forms父窗口最小化 [英] Topmost WPF window does not minimize with Window Forms parent
问题描述
大家好,
我在Windows Forms Forms中使用ElementHost拥有WPF控件.通过此WPF控件,我打开了一个( non -modal)WPF窗口(使用Show()),该窗口设置为最高,因为它应始终出现在Windows Forms Form的前面.下面的树显示了这种情况:
Windows窗体Form
+ ElementHost
+ WPF控件
+ +打开( non -modal)最上方的WPF窗口
现在,如果最小化了Form(即最小化了应用程序),我也希望最顶层的WPF窗口也最小化.当然,这是行不通的,因为WPF窗口不知道表单应该是它的所有者.因此,我在WPF控件中使用了以下代码来将Form设置为WPF窗口的所有者:
Hi all,
I have a WPF control inside a Windows Forms Form using an ElementHost. From this WPF control, I open a (non-modal) WPF window (using Show()) which is set topmost because it should always appear in front of the Windows Forms Form. The following tree shows this situation:
Windows Forms Form
+ ElementHost
+ WPF Control
+ opens (non-modal) topmost WPF Window
Now, if the Form is minimized (i.e. the application is minimized), I want the topmost WPF window to minimize as well. Of course this does not work because the WPF window does not know that the Form should be its owner. Therefore, I have used the following code within the WPF control to set the Form as the Owner of the WPF window:
| //找到此WPF控件的Windows窗体父窗口将其设置为父窗口 //-此"是ElementHost中的WPF控件 |
| IntPtrparentFormHandle=IntPtr.Zero; |
| HwndSource wpfHandle = PresentationSource. FromVisual( this ) as HwndSource; |
| |
| //如果没有wpfHandle,则托管WPF控件null |
| 如果 (wpfHandle!= 空 ) |
| |
| System.Windows.Forms.Integration.ElementHost host = System.Windows.Forms.Control.FromChildHandle(wpfHandle.句柄)为 System.Windows.Fo rms.Integration.ElementHost; |
| style ="font-size:11px">(host!= 空 ) |
| {{ |
| style ="font-size:11px"> |
| 空 //获取父Windows窗体的窗体的句柄 |
| |
| |
| } |
| |
| 如果 (parentFormHandle!= IntPtr.Zero) |
| { |
| //将WPF对话框的所有者设置为表单句柄 WindowInteropHelperwindowHelper = WindowInteropHelper(myWpfDialog); |
| windowHelper.Owner == parentFormHandle; |
| } |
| |
| myWpfDialog.Show(); |
// find the Windows Forms parent window of this WPF control to set it as the parent | |
// of the WPF popup - "this" is the WPF control in the ElementHost | |
IntPtr parentFormHandle = IntPtr.Zero; | |
HwndSource wpfHandle = PresentationSource.FromVisual(this) as HwndSource; | |
// the WPF control is hosted if the wpfHandle is not null | |
if (wpfHandle != null) | |
{ | |
// get the ElementHost which hosts this WPF control | |
System.Windows.Forms.Integration.ElementHost host = System.Windows.Forms.Control.FromChildHandle(wpfHandle.Handle) as System.Windows.Forms.Integration.ElementHost; | |
if (host != null) | |
{ | |
// get the parent Form of the ElementHost | |
System.Windows.Forms.Form parentForm = host.FindForm(); | |
if (parentForm != null) | |
// get the handle of the parent Windows Forms Form | |
parentFormHandle = parentForm.Handle; | |
} | |
} | |
if (parentFormHandle != IntPtr.Zero) | |
{ | |
// set the owner of the WPF dialog to the Form handle | |
WindowInteropHelper windowHelper = new WindowInteropHelper(myWpfDialog); | |
windowHelper.Owner = parentFormHandle; | |
} | |
myWpfDialog.Show(); |
推荐答案
我正在做同样的事情,但是使用WPF UserControl,它工作正常.尝试改用UserControl.
感谢
Ben
I'm doing this same thing but using a WPF UserControl and it works fine. Try using a UserControl instead.
Thanks
Ben
这篇关于顶部WPF窗口不会随着Window Forms父窗口最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!