【发布时间】:2015-02-18 06:33:47
【问题描述】:
如何在 WindowsFormsHost 上添加 WPF 控件(我已经知道,当我将 Child 添加到 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" });
}
}
有没有其他方法可以在不使用 hacky 透明 Windows hack 的情况下在 WindowsFormsHost 上添加 WPF 控件?谢谢。
【问题讨论】:
-
@GrantWinney 因为
Image()不能在后台播放动画GIF,我可以使用MediaPlayer()或MediaTimeline()来执行此操作,但我不希望我的程序继续消耗20% 的 CPU 用于永久播放Media元素。如果我使用 PictureBox,它可以比 WPF 的MediaPlayer()更流畅、更快地播放 GIF,并且默认播放(设置背景路径,完成。) -
所以我很清楚,你想要你的 WPF 应用程序中的 WFH; WFH 将主持一个 WinForms
PictureBox;但是你想在上面叠加 WPF 元素?如果是这样,我认为这是不可能的。与原生 Windows 和 WinForms 不同,WPF 有一个呈现的窗口。我曾经托管过 WinForms 图表控件,并且无法在图表的 Z 顺序前放置 WPF 内容 -
@MickyDuncan 正是 Hugs ^_^
-
@user3718577 哈哈。祝你好运! :)
-
@MickyDuncan 谢谢:D 我得到了解决方案:D