Why is my WPF hosted in WinForm?

I used the code for this blog as shown below, but shortened, and I see WinForm inside my main window, but the sample text that I placed on it in the shortcut is not displayed.

[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; } } 

and

 <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> 
+8
c # winforms wpf
source share
1 answer
  <cc:WinFormsHost ShowCaption="False"> <wf:Form/> </cc:WinFormsHost> 

Your XAML embeds a System.Windows.Forms.Form object inside WinFormsHost. This is what you have, just an empty form with no child controls built into it. It looks like you tried to create your own in the WinFormsHost constructor by setting the Child property, but your XAML overrides it and you are left with an empty form again.

I put the ChildForm class inside the same namespace:

 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); } } 

And updated XAML to:

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

To obtain:

enter image description here

Set FormBorderStyle to None to get rid of the border. Etcetera.

Setting the form The TopLevel property to false and Visible to true is a much simpler way to turn the form into a child btw control. I left it that way, as you hinted that you might want to give the Delphi window the same treatment. In this case, you can return to your original approach again by creating a child in the constructor of the form class and simply missing the purpose of the content in XAML.

+9
source share

All Articles