我有一个 WPF 窗口,其中包含一个 WindowsFormsHost
元素。我需要在此元素之上绘制内容,但 WindowsFormsHost 的性质意味着它始终位于绘图堆的顶部。由于我无法在 WindowsFormsHost
组件顶部的同一个 WPF 窗口中进行绘制,我可以在其顶部覆盖另一个窗口吗?
我已经以基本的方式尝试过这一点,但有一些问题:
1)我无法阻止其他应用程序的窗口进入主窗口和覆盖窗口之间。
2)当我按Alt-Tab时,覆盖窗口出现在窗口列表中,这非常难看。
基本上我需要“子窗口”的概念,并且该窗口在所有意图和目的上都显示为另一个窗口的一部分。 UserControls 对我不起作用,因为 WindowsFormsHost
将总是在它上面绘制。
有什么想法吗?
更新 [2011 年 5 月 23 日 10:13]
谢谢两位的回答。
我尝试了 ChildWindow
方法,并且 WindowsFormsHost
元素仍然绘制在顶部。据我了解,只有真正的窗口才能在 WindowsFormsHost
之上绘制,同一窗口中的任何内容都将位于 WindowsFormsHost
下。
带有 WindowsFormsHost
的元素仍然会在 WinForms 组件下绘制,它们总是绘制在顶部,这似乎是不可协商的......
我想我正在寻找的是一种停靠外部窗口充当主窗口的一部分。在Mac上,有一个真正的“子窗口”的概念,我正在寻找类似的东西。
I have a WPF window, which contains a WindowsFormsHost
element. I need to draw things on top of this element, but the nature of WindowsFormsHost
means that it's always on the top of the drawing pile. As I cannot draw in the same WPF window on top of the WindowsFormsHost
component, can I overlay another window on top of it?
I've tried this in a rudimentary way, but I have a few problems:
1) I can't stop windows from other apps going in between the main window, and the overlay window.
2) When I Alt-Tab, the overlay window appears in the window list, which is pretty ugly.
Basically I need the concept of a "child window", and window which to all intents and purposes appears as part of another window. UserControls won't work for me, as the WindowsFormsHost
will always draw on top of it.
Any Ideas?
Update [May 23 '11 at 10:13 ]
Thank you both for answers.
I've tried the ChildWindow
approach, and the WindowsFormsHost
element still draws on top. As I understand it, only a true window can draw on top of a WindowsFormsHost
, anything in the same window will go under the WindowsFormsHost
.
An element with the WindowsFormsHost
will still draw under a WinForms component, they are always drawn on top, and that seems non-negotiable...
I guess what I'm looking for is a way to dock an external window to act as part of the main window. On the Mac, there is the concept of a true "child window", I'm looking for something like that.
发布评论
评论(5)
解决了这个问题,
我通过使用
Popup
而不是透明的Window
更新最终得到了一个子类化的
Popup 我称之为
AirspacePopup
。AirspacePopup
的作用PlacementTarget
。Window
放置。此解决方案来自 Chris Cavanagh 的博客。Popup
并在其子项移出屏幕后为其子项设置负Margin
来实现的。该解决方案来自 这篇 StackOverflow 帖子,作者:Rick Sladkey下面是一个示例,其中
AirspacePopup
用于在其上绘制Ellipse
WebBrowser
控件(实际上是 WinForms 控件)的顶部,但它可以与任何WindowsFormsHost
一起使用。简单的代码隐藏导航..
AirspacePopup
I've worked around this problem by using a
Popup
rather than a transparentWindow
Update
I ended up with a subclassed
Popup
which I callAirspacePopup
.What
AirspacePopup
doesPlacementTarget
.Window
in which it is being placed. This solution comes from Chris Cavanagh's Blog.Popup
and setting negativeMargin
on its child once it moves off-screen. This solution comes from this StackOverflow post by Rick SladkeyHere is an example where
AirspacePopup
is used to draw anEllipse
on top of aWebBrowser
Control (which is in fact a WinForms Control) but it will work just as well with anyWindowsFormsHost
.Simple Code behind to navigate..
AirspacePopup
经过大量测试不同的解决方案后:
优点:
缺点:
优点:
缺点:
优点:
缺点:
对我来说,最好的解决方案是使用 Microsoft.DwayneNeed。 (我用它来将 CefSharp-Winforms 引入 WPF 应用程序)
因为让它工作并不简单,所以她是一个小教程:
到你的窗口。
到你的网格。
例子:
After alot of testing the different solutions:
Pros:
Cons:
Pros:
Cons:
Pros:
Cons:
For me the best solution was to use Microsoft.DwayneNeed. (I used it to get CefSharp-Winforms into a WPF-application)
Because geting it to work isnt straight forward her is a small tutorial:
to your Window.
to your Grid.
Example:
在使用 CAL 或事件聚合器的典型 MVP 应用程序中,您可能必须创建另一个窗口并将其命名为弹出窗口或子窗口 (
ChildWindow.XAML
),并在 (ChildWindow.XAML .CS
) 就像在 maniwindow 中有一个属性,可以在需要时返回 Childwindow 类型。
希望
这有帮助,
In a typical MVP application using the CAL or Event aggregator, you might have to create another window and name it popup or child (
ChildWindow.XAML
) and have a get method in (ChildWindow.XAML.CS
) likeHave a property in the maniwindow which can return Childwindow type when in need.
like
Hope this helps,
您可以在
WindowsFormsHost
中执行“覆盖”部分。因此,您可以有一个子元素,该子元素应该位于托管元素中其他内容的顶部。You could do the "overlaying" part in the
WindowsFormsHost
. So there you could have a child element which should be on top of other content in the hosted element.我建议使用 MahApps 库。
我已经在这个问题上苦苦挣扎了一段时间,发现 MahApps 库修复了空域问题,而我无需进行任何配置。有关更多信息,请参阅他们的常见问题解答:
https://github.com/MahApps/MahApps.Metro/wiki/FAQ#1-why-is-so-and-so-winforms-control-invisible-or-not-rendering-why-网络浏览器或其他控件是否覆盖我的浮出或另一个控件空域
I'd suggest using the MahApps library.
I've been struggling with this issue for some time and found the MahApps library fixed the airspace problem without any configuration on my part. For more information see their FAQ on this:
https://github.com/MahApps/MahApps.Metro/wiki/FAQ#1-why-is-so-and-so-winforms-control-invisible-or-not-rendering-why-is-the-webbrowser-or-other-control-covering-my-flyout-or-another-control-airspace