在 WebBrowser 控件上显示装饰器

发布于 2024-08-21 12:28:55 字数 1253 浏览 0 评论 0原文

我在应用程序中使用 System.Windows.Controls.WebBrowser 进行各种操作,并且我注意到装饰器在应该出现在 WebBrowser 上时被切断。我意识到 WebBrowser 控件实际上是 COM 组件的包装器,并且可能呈现不同的效果,但我想知道是否有人知道如何解决这个问题。

这就是我看到的问题。这里我只有一个示例装饰器,它应该在某个东西的顶角绘制一个大红色圆圈(作为示例)。

当我用这个装饰网络浏览器时,我得到这个结果:

我希望看到完整的圆圈。

这是这个毫无价值的装饰器的代码,以防有帮助:

public class SillyAdorner : Adorner
{
    public SillyAdorner(UIElement element) : base(element)
    {

    }
    protected override void OnRender(DrawingContext drawingContext)
    {
        drawingContext.DrawEllipse(new SolidColorBrush(Colors.Red), new Pen(), new Point(7, 7), 30, 30);
        base.OnRender(drawingContext);
    }
}

以下是我如何在主机控件的 OnRender 方法中将其应用到浏览器:

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);            
        var layer = AdornerLayer.GetAdornerLayer(browser);
        layer.Add(new SillyAdorner(browser));
    }

有人对此有任何黑客或解决方法吗?

编辑:我正在使用 .NET 4.0,如果这有什么不同的话。

编辑#2:WebBrowser 似乎继承自 HwndHost,我已经看到了另外一两个关于装饰器和 hwndsources 的问题,但我没有看到任何看起来可以为 WebControl 实现它的东西,但希望这对某人来说是有用的信息。

I'm using the System.Windows.Controls.WebBrowser for various things in my app and I've noticed that adorners are cut off when they are supposed to appear over a WebBrowser. I realize that the WebBrowser control is really a wrapper around a COM component and probably renders differently, but I wondered if anyone figured out how to solve this.

This is the problem I'm seeing. Here I have just a sample adorner that is supposed to draw a big red circle in the top corner of something (as a sample).

When I adorn the WebBrowser with this, I get this result:

I expect to see the full circle.

Here's the code for this worthless adorner, in case that is helpful:

public class SillyAdorner : Adorner
{
    public SillyAdorner(UIElement element) : base(element)
    {

    }
    protected override void OnRender(DrawingContext drawingContext)
    {
        drawingContext.DrawEllipse(new SolidColorBrush(Colors.Red), new Pen(), new Point(7, 7), 30, 30);
        base.OnRender(drawingContext);
    }
}

And here is how I apply it to the browser in the OnRender method of the host control:

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);            
        var layer = AdornerLayer.GetAdornerLayer(browser);
        layer.Add(new SillyAdorner(browser));
    }

Anyone have any hacks or workarounds for this?

Edit: I'm using .NET 4.0, if that makes a difference.

Edit #2: WebBrowser appears to inherit from HwndHost, which I've seen another question or two regarding adorners and hwndsources, but I'm not seeing anything that looks like I could implement it for the WebControl, but hopefully this is useful information for someone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岛徒 2024-08-28 12:28:55

我认为这不适用于 Adroner,但您可以使用透明 Popup 控件将内容浮动在 WebBroswer 控件上。可以找到更多详细信息和代码示例 这里

I don't think that will work with an Adroner, but you can float content over a WebBroswer control using a transparent Popup control. More details and a code sample can be found here.

近箐 2024-08-28 12:28:55

这是我的博客帖子,介绍了<我编写的 href="https://github.com/michaelsutton/hwnd-adorner" rel="nofollow noreferrer">库 用于在任何 hwnd 上分层 WPF 装饰器。一个简单的网络浏览器演示如下所示:
快照

Here's my blog post introducing a library I wrote for layering a WPF adorner over any hwnd. A simple web browser demo looks like this:
snapshot

嘿看小鸭子会跑 2024-08-28 12:28:55

这是由空域问题引起的。由于 WebBrowser 是本机非 WPF 控件,因此无法直接在其上呈现装饰器(或其他 WPF 内容)。

为此,您需要使用某种单独的窗口,并将内容放入该单独的窗口中。这通常意味着使用位于主窗口顶部的透明 WPF 窗口。不幸的是,这不会像真正的本机 WPF 控件那样提供集成的解决方案。

This is caused by airspace issues. Since a WebBrowser is a native, non-WPF control, there is no way to directly render adorners (or other WPF content) on top of it.

In order to do this, you need to use a separate window of some sort, and put the content into that separate window. This typically means using a transparent WPF window layered over the top of your main window. Unfortunately, this will not be as integrated of a solution as a true native WPF control would provide.

心的憧憬 2024-08-28 12:28:55

尝试使用另一个 透明顶层覆盖窗口。仅在 Windows 2000 或更高版本上支持,当然,Win9x 没有分层窗口。

Try use another transparent TOP LEVEL overlay window. Only supported on Windows 2000 or higher of course, Win9x does not have layered windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文