简体   繁体   中英

How to get font size of currently selected text in WebBrowser control

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 method 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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM