在 WindowsFormsHost 上添加 WPF 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28553166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:57:10  来源:igfitidea点击:

Adding WPF controls on WindowsFormsHost?

c#wpf

提问by Ramses Asmush

How could I add WPF controls over a WindowsFormsHost (I already know that when I add a Child to WFH, it becomes a separated handle). I tried making it a separated StackPanel or Canvas but doesn't seem's to work:

如何在 WindowsFormsHost 上添加 WPF 控件(我已经知道,当我将子级添加到 WFH 时,它会变成一个单独的句柄)。我尝试将其设置为单独的 StackPanel 或 Canvas,但似乎不起作用:

class CustomCanvas : Canvas{
public CustomCanvas(/*Some Width, Height, path values received*/){

WindowsFormsHost _AnimatedBckgr = new WindowsFormsHost()
            {
                Width = _wdt,
                Height = Container_Height,
                Margin = new Thickness(0,0,0,0),
                Child = new System.Windows.Forms.Panel()
            };

            ((System.Windows.Forms.Panel)_AnimatedBckgr.Child).Controls.Add(new System.Windows.Forms.PictureBox()
            {
                //Width = (int)_wdt, Height = (int)Container_Height,
                Dock = System.Windows.Forms.DockStyle.Fill,
                BackgroundImage = new System.Drawing.Bitmap(GifAnimatedFilePath),
                BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch,
            });
//StackPanel or Canvas wndHost;
            wndHost = new StackPanel() { Width = _wdt, Height = Container_Height };
            wndHost.Children.Add(_AnimatedBckgr);

            Children.Add(wndHost); 
//Anything added in this canvas doesn't appears over the wndHost
            Children.Add(new Button(){ Content = "Hi" });

}
}

Is there any other way to add WPF controls over the WindowsFormsHost without using the hacky transparent Windows hack? Thanks.

有没有其他方法可以在不使用 hacky 透明 Windows hack 的情况下在 WindowsFormsHost 上添加 WPF 控件?谢谢。

回答by Ramses Asmush

After making some stuff in the C# procedural code, I tought of adding a Host over a host, and so over... So I can add WPF controls over the WinForms Controls overriding a WPF Control... Some kind of weird stuff, but you can try it by your own, notice there are sme bugs in .NET if you don't define the ElementHostor WindowsFormsHostsize at adding time. Example:

在 C# 程序代码中做了一些东西之后,我想在主机上添加一个主机,等等......所以我可以在覆盖 WPF 控件的 WinForms 控件上添加 WPF 控件......一些奇怪的东西,但是您可以自己尝试一下,如果您在添加时未定义ElementHostWindowsFormsHost大小,请注意 .NET 中存在一些错误。例子:

/*In a WPF Window, content container being a Canvas, adding a new WFH with
a `PicturBox()` on it. Then add it a DataGridView OVER this one, and then,
some `Label` over it*/

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        Content = new Canvas();

        System.Windows.Forms.Integration.WindowsFormsHost _wndHost = new System.Windows.Forms.Integration.WindowsFormsHost()
        {
            Margin = new Thickness(0, 0, 0, 0),
            Width = 200,
            Height = 200,
            Child = new System.Windows.Forms.PictureBox()
            {
                BackgroundImage = new System.Drawing.Bitmap(System.IO.Directory.GetCurrentDirectory() + "\f.jpg"),
                BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
            }
        };

        ((System.Windows.Forms.PictureBox)_wndHost.Child).Controls.Add(new System.Windows.Forms.DataGridView()
        {
            Width = 170, Height = 70,
            ColumnCount = 2, RowCount = 2
        });

        ((System.Windows.Forms.PictureBox)_wndHost.Child).Controls.Add(new System.Windows.Forms.Integration.ElementHost()
        {
            Width = 70, Height = 15,
            Child = new TextBlock()
            {
                Width = 10,
                Height = 10,
                Text = "First :O",
                Foreground = Brushes.Red,
                Background = Brushes.Transparent
            },
            BackColor = System.Drawing.Color.Transparent,
            Location = new System.Drawing.Point(10, 100)
        });

        ((System.Windows.Forms.PictureBox)_wndHost.Child).Controls.Add(new System.Windows.Forms.Integration.ElementHost()
        {
            Width = 70, Height = 15,
            Child = new TextBlock()
            {
                Width = 10,
                Height = 10,
                Text = "Second :O",
                Foreground = Brushes.Red,
                Background = Brushes.Transparent
            },
            BackColor = System.Drawing.Color.Transparent,
            Location = new System.Drawing.Point(10, 150)
        });

        ((Canvas)Content).Children.Add(_wndHost);


    }
}

Solution

解决方案

回答by John Peters

Ok this is closer... Add a Windows Form User Control to your project:

好的,这更接近了...向您的项目添加一个 Windows 窗体用户控件:

enter image description here

在此处输入图片说明

In that user control add your image:

在该用户控件中添加您的图像:

enter image description here

在此处输入图片说明

Now go back to XAML and add the WFH... adding in a child of the usercontrol you just created.

现在返回到 XAML 并添加 WFH... 添加您刚刚创建的用户控件的子项。

<Canvas>
  <WindowsForsHost HorizontalAlignment="Left" Height="34" VerticalAlignment="Top" Width="52">
    <local:UserControl1/>
  </WindowsformsHost>
  <Label Margin="0,35,-13,0">This is bill Clinton</Label>
</Canvas>

Notice in the markup the offset of the label was such that it can be put anywhere you want on the canvas.

请注意,在标记中,标签的偏移量可以放置在画布上的任何位置。

The Result is this but it don't animate....

结果是这样,但它没有动画......

enter image description here

在此处输入图片说明