HtmlEditor类对象说明之HtmlDocument2
HtmlDocument2 HTML文档对象模型:
主要方法有:
- GetBody() 获取当前文档Body对象
- GetReadyState() 获取当前文档状态信息,如是否正在加载,是否已经完成,枚举值为:uninitialized,loading,loaded,interactive,complete
- ExecCommand() 执行命令,如ExecCommand("MultipleSelection", false, true); 表示当前文档允许多选控件
- GetSelection() 获取当前选择区域,此方法用于两种场景:
场景1:用于在当前选中位置插入html代码。
var selRange = doc2.GetSelection().createRange() as IHTMLTxtRange; if (selRange != null){ selRange.pasteHTML("<input type='text'/>"); }
场景2:获取当前选中的控件
var controlRange=doc2.GetSelection().createRange() as IHTMLControlRange; if(controlRange!=null){ var length = controlRange.GetLength(); for (int i = 0; i < length;i++ ) { var control = controlRange.Item(i); } }
5.GetStyleSheets() 获取样式表,进行样式设置
IHTMLStyleSheetsCollection sheets = CurrentDocument.GetStyleSheets();//获取样式表 IHTMLStyleSheet sheet = null; if (sheets.length == 0)//样式表长度为0,则新增 { sheet = CurrentDocument.CreateStyleSheet("", 0); } else //大于0,则获取第一个样式表 { object i = 0; sheet = sheets.item(ref i) as IHTMLStyleSheet; } CommonArgs args = new CommonArgs(); args.Tag = sheet.GetCssText();//获取当前样式表的样式 HtmlViewCssStyleEditForm styleform = new HtmlViewCssStyleEditForm(args); if (styleform.ShowDialog() == DialogResult.OK) { try { sheet.SetCssText(args.Tag.ToString());//设置样式 _currentFormDesignFormModel.ViewStyleRules.Clear(); IHTMLStyleSheetRulesCollection rules = sheet.GetRules(); for (int i = 0; i < rules.GetLength(); i++) { var rule = rules.Item(i).GetSelectorText(); if (rule.Length > 1 && rule.StartsWith(".")) { var gciStyleRule = new GciStyleRule(); gciStyleRule.Name = rule.Substring(1); gciStyleRule.RuleText = rules.Item(i).GetStyle().GetCssText(); _currentFormDesignFormModel.ViewStyleRules.Add(gciStyleRule); } } _currentFormDesignFormModel.CurrentView.ViewCssStyle = args.Tag.ToString(); } catch (Exception ex) { } }