积极答复者
webBrowser 设置字体大小

问题
-
用 comboBox 下拉列表写了五个值 9 10 11 12 13
修改的时候获取 comboBox 的选择项 string fnb=""+comboBox2.SelectedItem;
当 comboBox 改变后执行
webBrowser.Document.ExecCommand("SelectAll", true, null);
webBrowser.Document.ExecCommand("FontSize", false, fnb.ToString());但是只能修改一次? 在点下拉 选择字号 文字大小就不会动了??
设置完后 貌似会触发一个错误
Domain “webBrowser.Document.Domain”引发了类型“System.Runtime.InteropServices.COMException”的异常 string {System.Runtime.InteropServices.COMException}
- 已编辑 ylzl 2016年11月23日 2:44
答案
全部回复
-
最好的方法还是利用页面JS 直接修改整个页面CSS 来控制页面字号
在载入页面之后
例子:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement script1 = webBrowser1.Document.CreateElement("script"); script1.SetAttribute("text", "function changeFontSize(s) { document.body.style.fontSize = s + 'px'; }"); head.AppendChild(script1); webBrowser1.Document.InvokeScript("changeFontSize", new []{ comboBox1.SelectedItem});
Bob Bao
-
但是 选择完毕后 执行了 字体大小没变化
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META content="text/html; charset=utf-8" http-equiv=Content-Type> <META content=IE=Edge,chrome=1 http-equiv="X - UA - Compatible"> <STYLE>*{margin:0; border:none; padding:0; outline:none;font-family:Microsoft YaHei;}html,body{background:#ECF5FB}img{max-width:24px;min-width:24px;max-height:24px;min-height:24px;border:none;}</STYLE> <META name=GENERATOR content="MSHTML 11.00.10570.1001"> <SCRIPT>function changeFontSize(s) { document.body.style.fontSize = s + 'px'; }</SCRIPT> </HEAD> <BODY style="WORD-WRAP: break-word; OVERFLOW-Y: scroll; WORD-BREAK: normal; MARGIN: 1%">123456</BODY></HTML>
- 已编辑 ylzl 2016年11月23日 3:51
-
private void comboBox2_SelectionChangeCommitted(object sender, EventArgs e) { if (comboBox2.Items != null && comboBox2.Items.Count > 0 && comboBox2.SelectedItem != null) { object FontSize=comboBox2.SelectedItem; HtmlElement head = webBrowser2.Document.GetElementsByTagName("head")[0]; HtmlElement script1 = webBrowser2.Document.CreateElement("script"); script1.SetAttribute("text", "function changeFontSize(s) { document.body.style.fontSize = s + 'px'; }"); head.AppendChild(script1); webBrowser2.Document.InvokeScript("changeFontSize", new[] { FontSize }); }
vs2015