1

有什么方法可以获取 Microsoft WebBrowser 控件 (MSHTML) 中当前选定文本的字体大小?

我知道IHTMLDocument2::queryCommandState("FontSize", ...),但是对于过时的字体大小“xx-small”到“xx-large”,此方法仅返回 1 到 7 之间的值。对于像“10pt”或“14px”这样的字体大小,没有返回有用的值。

有没有更灵活的方法来确定字体大小?

编辑:与此同时,我找到了我的问题的解决方案(微软支持提供了一些有用的提示):

try
{
   mshtml.IHTMLTxtRange range = _dom.selection.createRange() as mshtml.IHTMLTxtRange;
   if (range != null)
   {
       mshtml.IHTMLElement2 elem = range.parentElement() as mshtml.IHTMLElement2;
       txtFontSize.Text = elem.currentStyle.fontSize.ToString();

   }
}
catch (COMException ex)
{
}
4

2 回答 2

1

既然你知道了如何获得它,这里有一种设置方法。

mshtml.HTMLDocument doc = [Obtain HtmlDocument];
doc.execCommand("FontSize", false, "12pt");

要获得您可以使用的价值

doc.queryCommandValue("FontSize");
于 2010-10-01T16:32:29.630 回答
0
IHTMLDocument2 htmlDocument = browser.Document.DomDocument as IHTMLDocument2;

IHTMLSelectionObject sel = (IHTMLSelectionObject)htmlDocument.selection;
IHTMLTxtRange range = (IHTMLTxtRange)sel.createRange() as IHTMLTxtRange;

if (range != null)
{
   range.select();
   var x = range.queryCommandValue("bold");
   textBoxFindData.Text = (x.ToString());
}
于 2015-05-20T12:25:14.100 回答