HtmlDocument2 HTML文檔對象模型:html
主要方法有:spa
場景1:用於在當前選中位置插入html代碼。code
var selRange = doc2.GetSelection().createRange() as IHTMLTxtRange; if (selRange != null){ selRange.pasteHTML("<input type='text'/>"); }
場景2:獲取當前選中的控件orm
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() 獲取樣式表,進行樣式設置htm
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) { } }