WPF WebBrowser 字体大小更改功能

发布于 2024-11-16 08:44:07 字数 602 浏览 4 评论 0原文

我希望能够使用按钮缩放 WebBrowser 控件的字体大小。

目前,我可以做类似的事情

    IHTMLDocument2 doc = myBrowser.Document as IHTMLDocument2;

        if (doc != null)
        {
            doc.execCommand("SelectAll", false, null);
            doc.execCommand("FontSize", false, someSize);
            doc.execCommand("Unselect", false, null);
        }
    }

,基本上选择我的网络浏览器中的所有内容,更改字体大小,然后取消选择。这样做的问题是,它将整个文档设置为相同的字体大小,并且它不是相对的,这意味着如果我有标题或带有某些段落的内容,那么在按下按钮后标题和段落将具有相同的大小。

仔细查看 IHTMLDocument2 接口 API,没有一个命令允许我选择文档的子集,这意味着我基本上无法做我想做的事情。我想知道是否还有其他地方可以访问 IHTMLDocument2 内子元素的字体大小属性。

I want to be able to scale the font size of my WebBrowser control with a button.

Currently, I can do something like

    IHTMLDocument2 doc = myBrowser.Document as IHTMLDocument2;

        if (doc != null)
        {
            doc.execCommand("SelectAll", false, null);
            doc.execCommand("FontSize", false, someSize);
            doc.execCommand("Unselect", false, null);
        }
    }

Which basically selects all the content in my WebBrowser, changes the font size, and deselects. The problem with this is that it sets the WHOLE document to the same font size, and it's not relative, which means if I have a header or something with some paragraphs, then the header and paragraph will have the same size after I press the button.

Sniffing around at the IHTMLDocument2 interface API, there isn't a command that allows me to select a subset of the document, which means I'm basically not able to do what I want to do. I'm wondering if there is anywhere else I can access the font size attribute of the child elements inside that IHTMLDocument2.

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

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

发布评论

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

评论(1

几味少女 2024-11-23 08:44:07

我找到了答案:

IHTMLDocument2 doc = webBrowser.Document as IHTMLDocument2;
doc.parentWindow.execScript("document.body.style.zoom=" + browserFontSize.ToString().Replace(",", ".") + ";");

其中 webBrowser 是您的 WebBrowser,而 browserFontSize 是您想要将 WebBrowser 缩放到的双精度值(大于 0,不确定上限是多少)。

仅当您的 IE 版本为 v7.0+ 时此功能才有效

I found the answer:

IHTMLDocument2 doc = webBrowser.Document as IHTMLDocument2;
doc.parentWindow.execScript("document.body.style.zoom=" + browserFontSize.ToString().Replace(",", ".") + ";");

Where webBrowser is your WebBrowser and browserFontSize is the double value (larger than 0, not sure what the cap is) you want to scale your WebBrowser to.

This will only work if your IE is v7.0+

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