顶部WPF窗口不会随着Window Forms父窗口最小化 [英] Topmost WPF window does not minimize with Window Forms parent

查看:73
本文介绍了顶部WPF窗口不会随着Window Forms父窗口最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在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:

1 //找到此WPF控件的Windows窗体父窗口将其设置为父窗口 //-此"是ElementHost中的WPF控件
3 IntPtrparentFormHandle=IntPtr.Zero;
4 HwndSource wpfHandle = PresentationSource. FromVisual( this ) as HwndSource;
5
6 //如果没有wpfHandle,则托管WPF控件null
7 如果 (wpfHandle!= )
8
10 System.Windows.Forms.Integration.ElementHost host = System.Windows.Forms.Control.FromChildHandle(wpfHandle.句柄) System.Windows.Fo rms.Integration.ElementHost;
11 style ="font-size:11px">(host!= )
12 {{
13 style ="font-size:11px">
14 //获取父Windows窗体的窗体的句柄
17
18
19 }
20
21 如果 (parentFormHandle!= IntPtr.Zero)
22 {
23 //将WPF对话框的所有者设置为表单句柄 WindowInteropHelperwindowHelper = WindowInteropHelper(myWpfDialog);
25 windowHelper.Owner == parentFormHandle;
26 }
27
28 myWpfDialog.Show();
1// find the Windows Forms parent window of this WPF control to set it as the parent 
2// of the WPF popup - "this" is the WPF control in the ElementHost 
3IntPtr parentFormHandle = IntPtr.Zero; 
4HwndSource wpfHandle = PresentationSource.FromVisual(thisas HwndSource; 
5 
6// the WPF control is hosted if the wpfHandle is not null 
7if (wpfHandle != null
8
9    // get the ElementHost which hosts this WPF control 
10    System.Windows.Forms.Integration.ElementHost host = System.Windows.Forms.Control.FromChildHandle(wpfHandle.Handle) as System.Windows.Forms.Integration.ElementHost; 
11    if (host != null
12    { 
13        // get the parent Form of the ElementHost 
14        System.Windows.Forms.Form parentForm = host.FindForm(); 
15        if (parentForm != null
16            // get the handle of the parent Windows Forms Form 
17            parentFormHandle = parentForm.Handle; 
18    } 
19
20 
21if (parentFormHandle != IntPtr.Zero) 
22
23    // set the owner of the WPF dialog to the Form handle 
24    WindowInteropHelper windowHelper = new WindowInteropHelper(myWpfDialog); 
25    windowHelper.Owner = parentFormHandle; 
26
27 
28myWpfDialog.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆