我知道默认的WPF行为是渲染WPF控件然后在顶部渲染WinForms,但有没有办法在WindowsFormsHost
之上渲染WPF?
编辑:我也发现了一个临时黑客攻击。当wpf控件重叠WindowsFormsHost
时,我会更改WindowsFormsHost
的大小(这仅适用于重叠的矩形对象,不适用于其他形状。)
答案 0 :(得分:7)
我知道这个晚会,但我最近使用WebBrowser控件遇到了这个问题。
最后一个修复是每当我在顶部托管模式对话框时创建Web浏览器的屏幕截图。由于这有点繁琐,我把它变成了一个Github项目,希望这有点帮助 -
https://github.com/chris84948/AirspaceFixer
(它也在Nuget上,在AirspaceFixer下)
一旦你有了项目,你需要做的就是这个
xmlns:asf="clr-namespace:AirspaceFixer;assembly=AirspaceFixer"
<asf:AirspacePanel FixAirspace="{Binding FixAirspace}">
<WebBrowser x:Name="Browser" />
</asf:AirspacePanel>
其中FixAirspace
是从内容的“真实”视图切换到屏幕截图或“虚假”视图的依赖项属性。
答案 1 :(得分:6)
WPF vNext中的这个“空域”问题是suppose to be fixed。有一些解决方案,例如here,here和here。
执行此操作的一种方法是将WPF内容托管在透明的Popup或Window中,该窗口覆盖Interop内容。
答案 2 :(得分:6)
这是到目前为止我在这个主题上看到的最佳答案的链接: Can I overlay a WPF window on top of another?
答案 3 :(得分:6)
尝试使用此尺寸:
<hacks:AirspaceOverlay>
<hacks:AirspaceOverlay.OverlayChild>
<Canvas ToolTip = "A tooltip over a DirectX surface" Background="#01000000" Name="Overlay" />
</hacks:AirspaceOverlay.OverlayChild>
<controls:OpenGLControlWrappingWindowsFormsHost />
</hacks:AirspaceOverlay>
// Adapted from http://blogs.msdn.com/b/pantal/archive/2007/07/31/managed-directx-interop-with-wpf-part-2.aspx & http://www.4mghc.com/topics/69774/1/in-wpf-how-can-you-draw-a-line-over-a-windowsformshost
public class AirspaceOverlay : Decorator
{
private readonly Window _transparentInputWindow;
private Window _parentWindow;
public AirspaceOverlay()
{
_transparentInputWindow = CreateTransparentWindow();
_transparentInputWindow.PreviewMouseDown += TransparentInputWindow_PreviewMouseDown;
}
public object OverlayChild
{
get { return _transparentInputWindow.Content; }
set { _transparentInputWindow.Content = value; }
}
private static Window CreateTransparentWindow()
{
var transparentInputWindow = new Window();
//Make the window itself transparent, with no style.
transparentInputWindow.Background = Brushes.Transparent;
transparentInputWindow.AllowsTransparency = true;
transparentInputWindow.WindowStyle = WindowStyle.None;
//Hide from taskbar until it becomes a child
transparentInputWindow.ShowInTaskbar = false;
//HACK: This window and it's child controls should never have focus, as window styling of an invisible window
//will confuse user.
transparentInputWindow.Focusable = false;
return transparentInputWindow;
}
void TransparentInputWindow_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
_parentWindow.Focus();
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
UpdateOverlaySize();
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (_transparentInputWindow.Visibility != Visibility.Visible)
{
UpdateOverlaySize();
_transparentInputWindow.Show();
_parentWindow = GetParentWindow(this);
_transparentInputWindow.Owner = _parentWindow;
_parentWindow.LocationChanged += ParentWindow_LocationChanged;
_parentWindow.SizeChanged += ParentWindow_SizeChanged;
}
}
private static Window GetParentWindow(DependencyObject o)
{
var parent = VisualTreeHelper.GetParent(o);
if (parent != null)
return GetParentWindow(parent);
var fe = o as FrameworkElement;
if (fe is Window)
return fe as Window;
if (fe != null && fe.Parent != null)
return GetParentWindow(fe.Parent);
throw new ApplicationException("A window parent could not be found for " + o);
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdateOverlaySize();
}
private void ParentWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateOverlaySize();
}
private void UpdateOverlaySize()
{
var hostTopLeft = PointToScreen(new Point(0, 0));
_transparentInputWindow.Left = hostTopLeft.X;
_transparentInputWindow.Top = hostTopLeft.Y;
_transparentInputWindow.Width = ActualWidth;
_transparentInputWindow.Height = ActualHeight;
}
}
答案 4 :(得分:3)
如果有人发现自己对黑客不满意,将WindowsFormsHost的可见性设置为折叠或隐藏始终是一种选择。
答案 5 :(得分:0)
在将WinForms应用程序移植到WPF时,试图创建一个托管WinForms控件的MDI样式界面时遇到了这个问题。
我通过执行以下操作设法解决了这个问题: WindowsFormsHost-> ElementHost-> WindowsFormsHost->我的胜利表单控件。
它非常丑陋,但是它为WPF内容创建了Windows图层,使其位于WinForms内容之下。