WPF WebBrowser - 如何缩放内容?

发布于 2024-10-19 19:25:04 字数 409 浏览 5 评论 0原文

尝试在 WPF(C#/XAML、.NET 4.0)Web 浏览器应用程序中测试基本浏览器概念。到目前为止,唯一的问题是以编程方式缩放。有人有这方面的经验吗?

MSDN 未列出任何内容: http://msdn.microsoft.com /en-us/library/system.windows.controls.webbrowser.aspx 此外,我尝试了各种方法,例如 RenderTransform 选项,但无济于事。这要么不可能,要么没有记录。我希望是后者。请注意,WinForm 解决方案是不可接受的。

预先感谢您的任何帮助, 比姆斯

Trying to test basic browser concepts in a WPF (C#/XAML, .NET 4.0) WebBrowser application. So far, the only problem is programatically zooming. Has anyone had any experience with this?

MSDN lists nothing: http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.aspx
Additionally, I have tried various things such as RenderTransform options to no avail. Either this is not possible or not documented. I'm hoping for the latter. Note that a WinForm solution isn't acceptable.

Thanks in advance for any help,
Beems

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

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

发布评论

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

评论(3

情话难免假 2024-10-26 19:25:04

也许你可以像这样执行 JavaScript。

document.body.style.zoom = 1.5;

在WPF中我们可以操作文档。我为你创建了一个扩展方法,这样你就可以设置缩放:

// www.tonysistemas.com.br
public static partial class MyExtensions
{
    public static void SetZoom(this System.Windows.Controls.WebBrowser WebBrowser1, double Zoom)
    {
        // For this code to work: add the Microsoft.mshtml .NET reference      
        mshtml.IHTMLDocument2 doc = WebBrowser1.Document as mshtml.IHTMLDocument2;
        doc.parentWindow.execScript("document.body.style.zoom=" + Zoom.ToString().Replace(",", ".") + ";");
    }
}

用法:

WebBrowser1.SetZoom(0.5);

Maybe you can execute a javascript like this.

document.body.style.zoom = 1.5;

In WPF we can manipulate the document. I Created a Extension Method for you, so you can set the Zoom:

// www.tonysistemas.com.br
public static partial class MyExtensions
{
    public static void SetZoom(this System.Windows.Controls.WebBrowser WebBrowser1, double Zoom)
    {
        // For this code to work: add the Microsoft.mshtml .NET reference      
        mshtml.IHTMLDocument2 doc = WebBrowser1.Document as mshtml.IHTMLDocument2;
        doc.parentWindow.execScript("document.body.style.zoom=" + Zoom.ToString().Replace(",", ".") + ";");
    }
}

Usage:

WebBrowser1.SetZoom(0.5);
乞讨 2024-10-26 19:25:04

我使用了这个答案中的点点滴滴https://stackoverflow.com/a/7326179/17822来协助缩放问题。这里的关键是ExecWB方法。 Windows 桌面上的缩放比例与 WebBrowser 控件上的缩放比例不是 1-1。你将不得不玩它。等式的伪代码如下所示:

zoomLevel = (winDesktopZoom - 100) + _winDesktopZoom + 10

请注意,您将需要对 SHDocVw.dll 的引用,对于 x64 计算机,它可以在 C:\Windows\SysWOW64 中找到;对于 x86 计算机,可以在 C:\Windows\System32 中找到。

这并不漂亮,但这是我发现的唯一东西,除了升级到 http://awesomium.com 实际上将 IE 默认缩放设置(默认为 Windows 桌面缩放)与 WebBrowser Control 相匹配。另请注意,Windows 桌面缩放仅适用于 Vista、Win 7,可能还存在于控制面板中的 2k8 -->显示,但我没有检查Vista或2k8。 XP(任何服务包)都没有它。

为了获得 Windows 桌面缩放(出于某种原因,这在 XP 上确实有效),我这样做了:

var presentSource = PresentationSource.FromVisual(this);

if (presentSource != null && presentSource.CompositionTarget != null 
    && presentSource.CompositionTarget.TransformToDevice != null)
{
    _zoomPercentage = Convert.ToInt32(100 * presentSource.CompositionTarget.TransformToDevice.M11);
}

此逻辑被放置在该 XAML 窗口的 OnSourceInitialized 重写中。

I used bits and pieces from this answer https://stackoverflow.com/a/7326179/17822 to assist with the zoom issue. The key here is the ExecWB method. The zoom on the Windows Desktop is not 1-1 to the zoom on the WebBrowser Control. You will have to play with it. The pseudo-code for the equation looks like this:

zoomLevel = (winDesktopZoom - 100) + _winDesktopZoom + 10

Note that you will need a reference to SHDocVw.dll which can be found in the C:\Windows\SysWOW64 for x64 machines and in C:\Windows\System32 for x86 machines.

This is not pretty, but it is the only thing that I have found, short of upgrading to http://awesomium.com that actually matches IE default zoom settings (which default to the Windows Desktop zoom) to WebBrowser Control. Also note that the Windows Desktop Zoom only exists for Vista, Win 7 and probably 2k8 as well in the Control Panel --> Display, but I didn't check Vista or 2k8. It is not there for XP (any service pack).

To get the Windows Desktop Zoom (this does work on XP for some reason) I did:

var presentSource = PresentationSource.FromVisual(this);

if (presentSource != null && presentSource.CompositionTarget != null 
    && presentSource.CompositionTarget.TransformToDevice != null)
{
    _zoomPercentage = Convert.ToInt32(100 * presentSource.CompositionTarget.TransformToDevice.M11);
}

This logic is placed in the OnSourceInitialized override for that XAML Window.

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