/// <summary> /// 元素绑定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void designWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { IHTMLDocument3 pBody = ((IWebBrowser2)designWebBrowser.ActiveXInstance).Document as IHTMLDocument3; m_elemEvents.ConnectToHtmlEvents(pBody.documentElement); if (idoc2.body != null) { idoc2.body.innerHTML = _articleText; codeTextEditorControl.Text = GeneralMethods.tidy(idoc2.body.innerHTML); string htmlCode = codeTextEditorControl.Text; string ostr = "${srs_"; while (htmlCode.IndexOf(ostr) > 0) { string mediaId = htmlCode.Substring(htmlCode.IndexOf(ostr) + 6, 32); SimpleExIndexXmlElement ele = Service.Sdsite.CurrentDocument.GetElementById(mediaId) as SimpleExIndexXmlElement; if (ele != null) { if (!ResourcesIdPaths.ContainsKey(ele.Id)) { ResourcesIdPaths.Add(ele.Id, ele.AbsoluteFilePath); } string PATH = SiteResourceService.ParseFormatId(ele.Id, true); htmlCode = htmlCode.Replace(htmlCode.Substring(htmlCode.IndexOf(ostr), 39), "file:///" + ele.AbsoluteFilePath.Replace("\\", "/")); } else { break; } } } DesignToCode(); CodeToDesign(); }
public static bool AddToSelection(IHTMLDocument2 m_pDoc2, string s_BeginHtml, string s_EndHtml) { if (m_pDoc2 == null) { return(false); } IHTMLSelectionObject sel = m_pDoc2.selection as IHTMLSelectionObject; IHTMLElement ele = m_pDoc2.activeElement as IHTMLElement; if (sel == null) { return(false); } IHTMLTxtRange range = sel.createRange() as IHTMLTxtRange; if (range == null) { return(false); } string shtml = string.Empty; if (!string.IsNullOrEmpty(s_BeginHtml)) { if (s_EndHtml.IndexOf("span") < 0) { shtml = s_BeginHtml + range.htmlText + s_EndHtml; } else { #region string xmstr = GeneralMethods.tidy(range.htmlText); if (xmstr.Length > 4) { xmstr = xmstr.Replace("\n", "");//去掉/n/r } xmstr = xmstr.Replace("\r", ""); string[] style = new string[100]; int i = 0; int span = xmstr.IndexOf("span"); string spanstr = ((span == 1) ? "</span>" : ""); while (xmstr.Length > 0) //将选中字符串分段存入数组 { if (xmstr.IndexOf("<span") != 0) //span处理的字符串不在最前 { if (xmstr.IndexOf("<span") == -1) //没有span的情况,即没有经过样式处理的字符串 { style[i++] = xmstr; xmstr = ""; break; } style[i++] = xmstr.Substring(0, xmstr.IndexOf("<span")); xmstr = xmstr.Substring(xmstr.IndexOf("<span")); } else {//将span处理的字符串存入数组 int spans = xmstr.IndexOf("<span"); int spand = xmstr.IndexOf("</span>"); if (spand != -1 && spans != -1) { style[i++] = xmstr.Substring(spans, spand + 7); xmstr = xmstr.Substring(spand + 7); } } } int k = -1; for (int j = 0; j < style.Length; j++) { if (style[j] != null && style[j] != " ") { if (style[j].IndexOf("span") > -1)//如果包含Span { k++; if (k == 0) { style[j] = "</span>" + style[j].Substring(0, style[j].IndexOf(">") - 1) + ";" + s_BeginHtml + style[j].Substring(style[j].IndexOf(">") - 1); } else { style[j] = style[j].Substring(0, style[j].IndexOf(">") - 1) + ";" + s_BeginHtml + style[j].Substring(style[j].IndexOf(">") - 1); } } else { k++; style[j] = "<span style='" + s_BeginHtml + "'>" + style[j] + "</span>"; } spanstr += style[j]; } if (style[j] == null) { break; } } #endregion if (spanstr.IndexOf("spanstyle") > -1) { spanstr = spanstr.Replace("spanstyle", "span style"); } shtml = spanstr; } } //if (shtml.Contains("CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95")) //{ // shtml=shtml.Replace("CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95", "{id}"); //} range.pasteHTML(shtml); range.findText("cao", 0, 0); //range.collapse(false); range.select(); return(true); }