HtmlEditor類對象說明之HtmlDocument2

HtmlDocument2  HTML文檔對象模型:html

主要方法有:spa

    1. GetBody()  獲取當前文檔Body對象
    2. GetReadyState()   獲取當前文檔狀態信息,如是否正在加載,是否已經完成,枚舉值爲:uninitialized,loading,loaded,interactive,complete
    3. ExecCommand()   執行命令,如ExecCommand("MultipleSelection", false, true); 表示當前文檔容許多選控件
    4. GetSelection()   獲取當前選擇區域,此方法用於兩種場景:

           場景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)
                {

                }
            }
相關文章
相關標籤/搜索