代码之家  ›  专栏  ›  技术社区  ›  user128300

如何在WebBrowser控件中获取当前选定文本的字体大小

  •  1
  • user128300  · 技术社区  · 16 年前

    我知道 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)
    {
    }
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   husayt    15 年前

    既然你知道如何得到它,这里有一个方法来设置它。

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

    为了得到你可以使用的价值

    doc.queryCommandValue("FontSize");
    
        2
  •  0
  •   Bojan B Rajeev Ranjan    9 年前
    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());
    }
    
    推荐文章