• Csharp:TinyMCE HTML Editor in .NET WindowsForms


      1  /// <summary>
      2     /// 
      3     /// </summary>
      4     public partial class Form2 : Form
      5     {
      6  
      7         private mshtml.IHTMLDocument2 hc;
      8         private mshtml.HTMLDocument hc1;
      9         //subdir in ApplicationData Folder, change whatever you think
     10         private const string M_WORKPATH = "someMCEenabledFolder";
     11         //easilly navigate into resources
     12         private const string M_RESOURCE = "WF_tinyMCE";
     13         /// <summary>
     14         /// 
     15         /// </summary>
     16         public override string Text
     17         {
     18             get
     19             {
     20                 return m_text;
     21             }
     22             set
     23             {
     24                 m_text = value;
     25             }
     26         }
     27         /// <summary>
     28         /// 
     29         /// </summary>
     30         /// <param name="source"></param>
     31         /// <param name="target"></param>
     32         public void CopyFile(Stream source, Stream target)
     33         {
     34             byte[] buffer = new byte[0x10000];
     35             int bytes;
     36             try
     37             {
     38                 while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0)
     39                 {
     40                     target.Write(buffer, 0, bytes);
     41                 }
     42             }
     43             finally
     44             {
     45                 target.Close();
     46             }
     47         }
     48 
     49         /// <summary>
     50         /// Get the correct internal path where tinyMCE and work file will be stored
     51         /// </summary>
     52         /// <param name="subPath"></param>
     53         /// <returns></returns>
     54         private string getWorkPath(string subPath)
     55         {
     56             string AppFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
     57             return System.IO.Path.Combine(AppFolder, M_WORKPATH + "\" + subPath);
     58         }
     59 
     60         private string m_strMCEDir = "";
     61         private string m_strMCESettings = "";
     62         /// <summary>
     63         /// Prepare tinyMCE package
     64         /// </summary>
     65         public void PrepareTinyMCE()
     66         {
     67             string sCpy = getWorkPath("tinyMCE.zip");
     68             m_strMCEDir = getWorkPath("tinyMCE");
     69             m_strMCESettings = getWorkPath("tinyMCE.txt");
     70             if (!Directory.Exists(getWorkPath("")))
     71             {
     72                 Directory.CreateDirectory(getWorkPath(""));
     73             }
     74 
     75             string sMCE = "tinyMCE\tinyMCE\jscripts\tiny_mce";
     76             if (!Directory.Exists(m_strMCEDir) || !File.Exists(getWorkPath(sMCE) + "\tiny_mce.js"))
     77             {
     78                 string sMce = M_RESOURCE + ".resources.tinyMCE.zip";
     79                 Stream strFile = GetResourceFile(sMce);
     80                 FileStream fs = new FileStream(sCpy, FileMode.OpenOrCreate);
     81                 CopyFile(strFile, fs);
     82                 strFile.Close();
     83 
     84                 Directory.CreateDirectory(m_strMCEDir);
     85                 FastZip fz = new FastZip();
     86                 fz.ExtractZip(sCpy, m_strMCEDir, FastZip.Overwrite.Never, null, null, null);
     87                 File.Delete(sCpy);
     88             }
     89             //settings 
     90             {
     91                 string sMce = M_RESOURCE + ".resources.tinyMCE.txt";
     92                 Stream strFile = GetResourceFile(sMce);
     93                 StreamReader sr = new StreamReader(strFile);
     94                 m_strMCESettings = sr.ReadToEnd();
     95                 sr.Close();
     96             }
     97             m_strMCEDir = sMCE;
     98         }
     99         /// <summary>
    100         /// 
    101         /// </summary>
    102         /// <param name="File"></param>
    103         /// <returns></returns>
    104         public Stream GetResourceFile(string File)
    105         {
    106             if (File == null || File == "")
    107                 return null;
    108             try
    109             {
    110                 return this.GetType().Assembly.GetManifestResourceStream(File);
    111             }
    112             catch (Exception)
    113             {
    114                 return null;
    115             }
    116         }
    117         /// <summary>
    118         /// 
    119         /// </summary>
    120         public void doInit()
    121         {
    122             object empty = System.Reflection.Missing.Value;
    123             //mshtml.IHTMLDocument doc2 =webBrowser1.Document;
    124             //IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser1.Document;
    125             //webBrowser1.Document.Write("<html><body></body></html>");
    126             //webBrowser1.Document.Body.innerHTML = "";//
    127             //this.webBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty);
    128             this.webBrowser1.Navigate("about:blank");
    129             hc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;//(mshtml.IHTMLDocument2)
    130 
    131         }
    132 
    133         private string m_text = "", m_type = "", m_css = "";
    134         /// <summary>
    135         /// 
    136         /// </summary>
    137         /// <param name="Text"></param>
    138         public void ShowText(string Text)
    139         {
    140             m_text = Text;
    141             showText(false);
    142             this.Show();
    143             //this.Dispose();
    144         }
    145         /// <summary>
    146         /// 
    147         /// </summary>
    148         /// <param name="Text"></param>
    149         /// <param name="Type"></param>
    150         /// <param name="CSSFile"></param>
    151         /// <returns></returns>
    152         public string EditText(string Text, string Type, string CSSFile)
    153         {
    154             m_text = Text;
    155             m_type = Type;
    156             m_css = CSSFile;
    157             PrepareTinyMCE();
    158             showText(true);
    159             this.ShowDialog();
    160             m_text = ((mshtml.IHTMLDocument3)webBrowser1.Document.DomDocument).getElementById("elm1").innerText;
    161             return m_text;
    162             //this.Dispose();
    163         }
    164 
    165         /// <summary>
    166         /// 
    167         /// </summary>
    168         /// <param name="edit"></param>
    169         private void showText(bool edit)
    170         {
    171             StringBuilder sb = new StringBuilder();
    172             sb.AppendLine("<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=utf-8" content="no-cache">");
    173             if (edit && !string.IsNullOrEmpty(m_strMCESettings))
    174             {
    175                 sb.Append("<script type="text/javascript" src="");
    176                 sb.Append(this.m_strMCEDir.Replace('\', '/')); //IE compatible
    177                 sb.AppendLine("/tiny_mce.js">");
    178                 sb.AppendLine("</script>");
    179                 sb.AppendLine(this.m_strMCESettings);
    180                 sb.AppendLine("</HEAD><BODY><FONT FACE="Arial" SIZE="-1">");
    181                 sb.AppendLine("<form method="" action="" onsubmit="return false;">");
    182                 sb.AppendLine("<textarea id="elm1" name="elm1" style=" 100%;height:100%">");
    183             }
    184             else
    185             {
    186                 sb.AppendLine("</HEAD><BODY><FONT FACE="Arial" SIZE="-1">");
    187             }
    188             sb.AppendLine(m_text);
    189             if (edit && !string.IsNullOrEmpty(m_strMCESettings))
    190             {
    191                 sb.AppendLine("</textarea>");
    192             }
    193             sb.AppendLine("</FONT></BODY></HTML>");
    194 
    195             //get CSS to local folder
    196             if (File.Exists(m_css))
    197             {
    198                 File.Copy(m_css, getWorkPath(m_type + ".css"), true);
    199                 m_css = m_type + ".css";
    200             }
    201             string t = sb.Replace("[CSS]", m_css).ToString();
    202             string sDoc = getWorkPath(m_type + ".html");
    203             StreamWriter sw = new StreamWriter(sDoc, false, System.Text.Encoding.GetEncoding(65001));
    204             sw.Write(t);
    205             sw.Close();
    206             try
    207             {
    208                 this.webBrowser1.Navigate(sDoc);
    209             }
    210             catch (Exception e)
    211             {
    212                 string s = e.Message;
    213             }
    214         }
    215         /// <summary>
    216         /// 
    217         /// </summary>
    218         public Form2()
    219         {
    220             InitializeComponent();
    221 
    222             doInit();
    223         }
    224         /// <summary>
    225         /// 
    226         /// </summary>
    227         /// <param name="sender"></param>
    228         /// <param name="e"></param>
    229         private void Form2_Load(object sender, EventArgs e)
    230         {
    231             //EditText("tinyMCE涂聚文 intergrated into .NET."
    232             //+"A bit uncomfortable way through IE."
    233             //+"But if you manage it good, you can use it for free."
    234             //+"Don't forget to press save in MCE toolbar."
    235             //+ "", "tempfilename", "yourCSS.css");
    236 
    237         }
    238         /// <summary>
    239         /// 
    240         /// </summary>
    241         /// <param name="sender"></param>
    242         /// <param name="e"></param>
    243         private void button1_Click(object sender, EventArgs e)
    244         {
    245            // MessageBox.Show(webBrowser1.Document.DomDocument.ToString());
    246             string s = this.webBrowser1.Document.Body.InnerHtml;
    247             string ss = this.webBrowser1.Document.Body.InnerText;
    248             System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;
    249 
    250             //if (document != null && document.All["elm1"] != null && String.IsNullOrEmpty(document.All["elm1"].GetAttribute("value")))
    251             //{
    252             System.Windows.Forms.MessageBox.Show(document.All["elm1"].TagName.ToString());
    253             System.Windows.Forms.MessageBox.Show(document.All["elm1"].OuterHtml);
    254             //System.Windows.Forms.MessageBox.Show(document.All["elm1"].DomElement.ToString());
    255             System.Windows.Forms.MessageBox.Show(document.All["elm1"].InnerText);
    256 
    257             System.Windows.Forms.MessageBox.Show(webBrowser1.Document.GetElementsByTagName("textarea")[0].TagName);
    258            // System.Windows.Forms.MessageBox.Show(document.All["elm1"].GetAttribute("value"));
    259             //}
    260 
    261            //MessageBox.Show(webBrowser1.DocumentText);//
    262 
    263             HtmlElement textArea = webBrowser1.Document.All["elm1"];
    264 
    265             HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("textarea");
    266             foreach (HtmlElement element in elements)
    267             {
    268                 if (element.InnerHtml.Contains("Rich text editor"))
    269                 {
    270                     // do something
    271                 }
    272             }
    273         }
  • 相关阅读:
    sql语句的删除
    JDK 11中的ZGC-一种可扩展的低延迟垃圾收集器
    强大的IDEA开发工具
    微信H5支付证书过滤
    statusText 报错parsererror
    python 爬取页面数据,生成词云和网络图
    springboot 跳转页面
    org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'xxx' is not present 报错解决
    airtest 中报错 'gbk' codec can't encode character 'xa5' in position 170: illegal multibyte sequence
    Error:(5, 45) java: 程序包org.springframework.boot.test.context不存在 解决
  • 原文地址:https://www.cnblogs.com/geovindu/p/3161131.html
Copyright © 2020-2023  润新知