/// <summary> /// /// </summary> public partial class Form2 : Form { private mshtml.IHTMLDocument2 hc; private mshtml.HTMLDocument hc1; //subdir in ApplicationData Folder, change whatever you think private const string M_WORKPATH = "someMCEenabledFolder"; //easilly navigate into resources private const string M_RESOURCE = "WF_tinyMCE"; /// <summary> /// /// </summary> public override string Text { get { return m_text; } set { m_text = value; } } /// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="target"></param> public void CopyFile(Stream source, Stream target) { byte[] buffer = new byte[0x10000]; int bytes; try { while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0) { target.Write(buffer, 0, bytes); } } finally { target.Close(); } } /// <summary> /// Get the correct internal path where tinyMCE and work file will be stored /// </summary> /// <param name="subPath"></param> /// <returns></returns> private string getWorkPath(string subPath) { string AppFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); return System.IO.Path.Combine(AppFolder, M_WORKPATH + "\\" + subPath); } private string m_strMCEDir = ""; private string m_strMCESettings = ""; /// <summary> /// Prepare tinyMCE package /// </summary> public void PrepareTinyMCE() { string sCpy = getWorkPath("tinyMCE.zip"); m_strMCEDir = getWorkPath("tinyMCE"); m_strMCESettings = getWorkPath("tinyMCE.txt"); if (!Directory.Exists(getWorkPath(""))) { Directory.CreateDirectory(getWorkPath("")); } string sMCE = "tinyMCE\\tinyMCE\\jscripts\\tiny_mce"; if (!Directory.Exists(m_strMCEDir) || !File.Exists(getWorkPath(sMCE) + "\\tiny_mce.js")) { string sMce = M_RESOURCE + ".resources.tinyMCE.zip"; Stream strFile = GetResourceFile(sMce); FileStream fs = new FileStream(sCpy, FileMode.OpenOrCreate); CopyFile(strFile, fs); strFile.Close(); Directory.CreateDirectory(m_strMCEDir); FastZip fz = new FastZip(); fz.ExtractZip(sCpy, m_strMCEDir, FastZip.Overwrite.Never, null, null, null); File.Delete(sCpy); } //settings { string sMce = M_RESOURCE + ".resources.tinyMCE.txt"; Stream strFile = GetResourceFile(sMce); StreamReader sr = new StreamReader(strFile); m_strMCESettings = sr.ReadToEnd(); sr.Close(); } m_strMCEDir = sMCE; } /// <summary> /// /// </summary> /// <param name="File"></param> /// <returns></returns> public Stream GetResourceFile(string File) { if (File == null || File == "") return null; try { return this.GetType().Assembly.GetManifestResourceStream(File); } catch (Exception) { return null; } } /// <summary> /// /// </summary> public void doInit() { object empty = System.Reflection.Missing.Value; //mshtml.IHTMLDocument doc2 =webBrowser1.Document; //IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser1.Document; //webBrowser1.Document.Write("<html><body></body></html>"); //webBrowser1.Document.Body.innerHTML = "";// //this.webBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty); this.webBrowser1.Navigate("about:blank"); hc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;//(mshtml.IHTMLDocument2) } private string m_text = "", m_type = "", m_css = ""; /// <summary> /// /// </summary> /// <param name="Text"></param> public void ShowText(string Text) { m_text = Text; showText(false); this.Show(); //this.Dispose(); } /// <summary> /// /// </summary> /// <param name="Text"></param> /// <param name="Type"></param> /// <param name="CSSFile"></param> /// <returns></returns> public string EditText(string Text, string Type, string CSSFile) { m_text = Text; m_type = Type; m_css = CSSFile; PrepareTinyMCE(); showText(true); this.ShowDialog(); m_text = ((mshtml.IHTMLDocument3)webBrowser1.Document.DomDocument).getElementById("elm1").innerText; return m_text; //this.Dispose(); } /// <summary> /// /// </summary> /// <param name="edit"></param> private void showText(bool edit) { StringBuilder sb = new StringBuilder(); sb.AppendLine("<HTML><HEAD><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" content=\"no-cache\">"); if (edit && !string.IsNullOrEmpty(m_strMCESettings)) { sb.Append("<script type=\"text/javascript\" src=\""); sb.Append(this.m_strMCEDir.Replace('\\', '/')); //IE compatible sb.AppendLine("/tiny_mce.js\">"); sb.AppendLine("</script>"); sb.AppendLine(this.m_strMCESettings); sb.AppendLine("</HEAD><BODY><FONT FACE=\"Arial\" SIZE=\"-1\">"); sb.AppendLine("<form method=\"\" action=\"\" onsubmit=\"return false;\">"); sb.AppendLine("<textarea id=\"elm1\" name=\"elm1\" style=\"width: 100%;height:100%\">"); } else { sb.AppendLine("</HEAD><BODY><FONT FACE=\"Arial\" SIZE=\"-1\">"); } sb.AppendLine(m_text); if (edit && !string.IsNullOrEmpty(m_strMCESettings)) { sb.AppendLine("</textarea>"); } sb.AppendLine("</FONT></BODY></HTML>"); //get CSS to local folder if (File.Exists(m_css)) { File.Copy(m_css, getWorkPath(m_type + ".css"), true); m_css = m_type + ".css"; } string t = sb.Replace("[CSS]", m_css).ToString(); string sDoc = getWorkPath(m_type + ".html"); StreamWriter sw = new StreamWriter(sDoc, false, System.Text.Encoding.GetEncoding(65001)); sw.Write(t); sw.Close(); try { this.webBrowser1.Navigate(sDoc); } catch (Exception e) { string s = e.Message; } } /// <summary> /// /// </summary> public Form2() { InitializeComponent(); doInit(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form2_Load(object sender, EventArgs e) { //EditText("tinyMCE涂聚文 intergrated into .NET." //+"A bit uncomfortable way through IE." //+"But if you manage it good, you can use it for free." //+"Don't forget to press save in MCE toolbar." //+ "", "tempfilename", "yourCSS.css"); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { // MessageBox.Show(webBrowser1.Document.DomDocument.ToString()); string s = this.webBrowser1.Document.Body.InnerHtml; string ss = this.webBrowser1.Document.Body.InnerText; System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document; //if (document != null && document.All["elm1"] != null && String.IsNullOrEmpty(document.All["elm1"].GetAttribute("value"))) //{ System.Windows.Forms.MessageBox.Show(document.All["elm1"].TagName.ToString()); System.Windows.Forms.MessageBox.Show(document.All["elm1"].OuterHtml); //System.Windows.Forms.MessageBox.Show(document.All["elm1"].DomElement.ToString()); System.Windows.Forms.MessageBox.Show(document.All["elm1"].InnerText); System.Windows.Forms.MessageBox.Show(webBrowser1.Document.GetElementsByTagName("textarea")[0].TagName); // System.Windows.Forms.MessageBox.Show(document.All["elm1"].GetAttribute("value")); //} //MessageBox.Show(webBrowser1.DocumentText);// HtmlElement textArea = webBrowser1.Document.All["elm1"]; HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("textarea"); foreach (HtmlElement element in elements) { if (element.InnerHtml.Contains("Rich text editor")) { // do something } } }