2013-12-20 80 views
8

我已经使用了this blog中的代码,如下所示,但删节了,我在主窗口中看到了WinForm,但我在其中放置的示例文本不可见。为什么我的WPF托管WinForm为空?

[System.Windows.Markup.ContentProperty("Child")] 
public class WinFormsHost : HwndHost 
{ 
    public WinFormsHost() 
    { 
    var form = new ChildForm(); 
    Child = form; 
    } 
    private System.Windows.Forms.Form child; 
    public event EventHandler<ChildChangedEventArgs> ChildChanged; 
    public System.Windows.Forms.Form Child 
    { 
    get { return child; } 
    set 
    { 
     HwndSource ps = PresentationSource.FromVisual(this) as HwndSource; 
     if (ps != null && ps.Handle != IntPtr.Zero) 
     { 
     throw new InvalidOperationException("Cannot set the Child property after the layout is done."); 
     } 
     Form oldChild = child; 
     child = value; 
     OnChildChanged(oldChild); 
    } 
    } 

    private void CheckChildValidity() 
    { 
    if (child == null || child.Handle == IntPtr.Zero) 
    { 
     throw new ArgumentNullException("child form cannot be null"); 
    } 
    } 

    public Boolean ShowCaption 
    { 
    get 
    { 
     CheckChildValidity(); 
     return (GetWindowStyle(Child.Handle) & WindowStyles.WS_BORDER) == WindowStyles.WS_CAPTION; 
    } 
    set 
    { 
     if (child == null) 
     { 
     this.ChildChanged += delegate 
     { 
      if (value) 
      { 
      SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) | WindowStyles.WS_CAPTION); 
      } 
      else 
      { 
      SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) & ~WindowStyles.WS_CAPTION); 
      } 
     }; 
     } 
     else 
     { 
     if (value) 
     { 
      SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) | WindowStyles.WS_CAPTION); 
     } 
     else 
     { 
      SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) & ~WindowStyles.WS_CAPTION); 
     } 
     } 
    } 
    } 

    protected override HandleRef BuildWindowCore(HandleRef hwndParent) 
    { 
    CheckChildValidity(); 
    HandleRef childHwnd = new HandleRef(Child, child.Handle); 
    SetWindowStyle(childHwnd.Handle, WindowStyles.WS_CHILD | GetWindowStyle(childHwnd.Handle)); 
    WindowsFormsHost.EnableWindowsFormsInterop(); 
    System.Windows.Forms.Application.EnableVisualStyles(); 
    SetParent(childHwnd.Handle, hwndParent.Handle); 
    return childHwnd; 
    } 
} 

和:

<Window x:Class="WinFormsHost" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
    xmlns:cc="clr-namespace:XTime.Shell.WinformsHost" 
    Title="Hosting Form In WPF"> 
    <cc:WinFormsHost ShowCaption="False"> 
    <wf:Form/> 
    </cc:WinFormsHost> 
</Window> 
+1

你是否需要托管一个表单,或者你可以托管一个UserControl并使用内建的'WindowsFormHost'? – Iain

+0

它必须是一种形式。我用C#重写了一个Delphi应用程序,并告诉我是否可以托管一个Windows窗体,这是一个Win32窗口,我也可以托管一个Delphi窗体,也是一个Win32窗口。 – ProfK

+0

您无法托管窗体(如果它具有窗口镶边),则只能使用UserControl并使用一些适合UserControl的其他控件。 –

回答

9
<cc:WinFormsHost ShowCaption="False"> 
    <wf:Form/> 
    </cc:WinFormsHost> 

XAML中嵌入WinFormsHost内System.Windows.Forms.Form中的对象。这就是你所得到的,只是一个没有嵌入子控件的空白表单。它看起来像你试图在WinFormsHost构造函数中创建你自己的,分配Child属性,但是你的XAML重写它,所以你只是留下一个空白表单。

我把ChildForm类相同的命名空间内:

using System.Windows.Forms; 
using System.Drawing; 
... 

public class ChildForm : System.Windows.Forms.Form { 
    public ChildForm() { 
     this.BackColor = Color.FromKnownColor(KnownColor.Window); 
     var lbl = new Label { Text = "Hello world" }; 
     this.Controls.Add(lbl); 
    } 
} 

,并更新了XAML到:

<cc:WinFormsHost ShowCaption="False"> 
    <cc:ChildForm/> 
</cc:WinFormsHost> 

要获取:

enter image description here

设置FormBorderStyle到没有摆脱边界。等等。

将窗体的TopLevel属性设置为false,将Visible属性设置为true是将窗体转换为子控件btw的简单方法。我以这种方式离开了它,因为你暗示你可能想给Delphi窗口提供相同的处理。在这种情况下,您可能需要再次回到原始方法,在窗体类构造函数中创建子项,并只是省略XAML中的内容分配。

相关问题