Csharp:TinyMCE HTML Editor in .NET WindowsForms

简介: /// <summary> /// /// </summary> public partial class Form2 : Form { private mshtml.IHTMLDocument2 hc; private mshtml.HTMLDocument hc1; /

 /// <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
                }
            }
        }



目录
相关文章
|
4月前
|
JSON JavaScript 数据格式
文本-----wangEditor的使用,设置和获取内容,展示HTML无样式怎么办????console同步展示怎样写,Vue的配置在Vue3配置文件中的配置,是editor中的v-model绑定的值
文本-----wangEditor的使用,设置和获取内容,展示HTML无样式怎么办????console同步展示怎样写,Vue的配置在Vue3配置文件中的配置,是editor中的v-model绑定的值
|
编译器
在HTML页面中引用Markdown编辑器(Editor.md)
在HTML页面中引用Markdown编辑器(Editor.md)
在HTML页面中引用Markdown编辑器(Editor.md)
|
移动开发 前端开发 JavaScript
扩展HT for Web之HTML5表格组件的Renderer和Editor
在HT for Web提供了一下几种常用的Editor,分别是: slider:拉条 color picker:颜色选择器 enum:枚举类型 boolean:真假编辑器 string:普通的文本编辑器 除了这几种常用编辑器之外,用户还可以通过继承ht.widget.BaseItemEditor类来实现自定义编辑器。
1031 0
|
JavaScript 前端开发 .NET
[转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法
   from:http://tangb4c.yo2.cn/archives/633878 04月 22nd, 2008 现在做网页,用FCKEditor用得比较多,它的实现原理是在要加入FCKEditor的地方加入一个iframe,并将其src指向FCKeditor/editor/fckeditor.
1275 0
|
2月前
|
XML 前端开发 JavaScript
Html:CSS介绍
Html:CSS介绍
48 1
|
2月前
|
前端开发
Html:CSS的书写位置
Html:CSS的书写位置
30 0
|
1天前
|
移动开发 前端开发 JavaScript
[HTML、CSS]细节与使用经验
本文总结了前端开发中的一些重要细节和技巧,包括CSS选择器、定位、层级、全局属性、滚轮控制、轮播等。作者以纯文字形式记录,便于读者使用<kbd>Ctrl + F</kbd>快速查找相关内容。文章还提供了示例代码,帮助读者更好地理解和应用这些知识点。
14 1
[HTML、CSS]细节与使用经验
|
2天前
|
移动开发 前端开发 JavaScript
[HTML、CSS]知识点
本文涵盖前端知识点扩展、HTML标签(如video、input、canvas)、datalist和details标签的使用方法,以及CSS布局技巧(如margin、overflow: hidden和动态height)。文章旨在分享作者的学习经验和实用技巧。
12 1
[HTML、CSS]知识点