Is there any way to get the font size of the currently selected text in the Microsoft WebBrowser control (MSHTML)?
I am aware of IHTMLDocument2::queryCommandState("FontSize", ...)
, but this metho开发者_C百科d only returns a value between 1 and 7, for the outdated font sizes "xx-small" to "xx-large". For font sizes like "10pt" or "14px", no useful value is returned.
Is there a more flexible way to determine the font size?
EDIT: In the meantime, I found a solution to my question (with some helpful hints from Microsoft support):
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)
{
}
Since you found out how to get it, here is a way to set it up.
mshtml.HTMLDocument doc = [Obtain HtmlDocument];
doc.execCommand("FontSize", false, "12pt");
To get the value you can use
doc.queryCommandValue("FontSize");
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());
}
精彩评论