Pro*_*ofK 8 c# wpf winforms-interop winforms wpf-interop
我已经使用了这个博客中的代码,如下所示,但是我在主窗口中看到了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;
}
}
Run Code Online (Sandbox Code Playgroud)
和:
<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>
Run Code Online (Sandbox Code Playgroud)
<cc:WinFormsHost ShowCaption="False">
<wf:Form/>
</cc:WinFormsHost>
Run Code Online (Sandbox Code Playgroud)
您的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);
}
}
Run Code Online (Sandbox Code Playgroud)
并将XAML更新为:
<cc:WinFormsHost ShowCaption="False">
<cc:ChildForm/>
</cc:WinFormsHost>
Run Code Online (Sandbox Code Playgroud)
要得到:
将FormBorderStyle设置为None以摆脱边框.等等.
将窗体的TopLevel属性设置为false,将Visible属性设置为true是将窗体转换为子控件btw的简单方法.我这样离开了,因为你暗示你可能想给Delphi窗口提供同样的处理.在这种情况下,您可能希望再次返回到原始方法,在表单类构造函数中创建子项,并忽略XAML中的内容分配.
归档时间: |
|
查看次数: |
1056 次 |
最近记录: |