• C#(WinForm)实现HTML编辑器方法


    做Web开发时,我们经常会用到HTML富文本框编辑器来编写文章或产品描述的详细内容,常用的编辑器有FCKEditor、CKEditor 、TinyMCE、KindEditor和ueditor(百度的),

    我们知道WinForm上有一个webBrowser控件,本文正是采用webBrowser结合Web上的HTML编辑器KindEditor来实现的,KindEditor是一个国人写的编辑器,轻量级用起来挺不错,至少我知道目前拍拍和开源中国就是用此编辑器。

    KindEditor的官方地址为:http://kindeditor.net/down.php

    首先我们需要去官网或者Github:https://github.com/kindsoft/kindeditor下载一份代码,然后解压到我们项目的bin文件夹下,然后在bin/KindEditor目录下新建一个名字为e.html的html文件,并键入以下代码:

    <!doctype html>
    <html>
    
    <head>
        <meta charset="utf-8" />
        <title>Html Editor</title>
        <script charset="utf-8" src="kindeditor.js"></script>
        <script charset="utf-8" src="lang/zh_CN.js"></script>
    
    
        <script>
            window.onerror = function () { return true; };
            var editor;
            var contentSeted = false;
            KindEditor.ready(function (K) {
                editor = K.create('#details', {
                    allowFileManager: false,
                    allowImageUpload: false,
                    resizeType: 0, //不能更改大小
                    fullscreenMode: true,
                    items: [
                        'undo', 'redo', '|', 'cut', 'copy', 'paste',
                        'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                        'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                        'superscript', '|', 'clearhtml', 'quickformat', 'selectall', 'flash', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', '/',
                        'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                        'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
                        'link', 'unlink', '|', 'template', 'code', 'source', 'preview',
                    ],
                    afterChange: function () {
                        if (editor && contentSeted)
                            window.external.RequestContent(editor.html());
                    }
                });
                setContent(window.external.GetContent());
    
            });
            function setContent(content) {
                if (editor) {
                    contentSeted = false;
                    editor.html(content);
                    contentSeted = true;
                }
            }
    
        </script>
    </head>
    <body style="padding: 0; margin: 0;">
        <textarea id="details" style="display: block;  680px; height: 100%; visibility: hidden;"></textarea>
    
    </body>
    </html>

    如果在Web上用过 KindEditor的朋友对以上代码应该不陌生,因为它实际上就是初始化一个 HTML编辑器而已,我们还在代码中定义了一个setContent方法,该方法就是用来设置HTML编辑器的内容,我们在C#代码中需要调用该方法.

    好了,下面我们回到WinForm上面,我们在界面上拉一个webBrowser控件,cs里键入以下代码:

    namespace WinformHTMLEditor
    {
        [ComVisible(true)]
        public partial class Form1 : Form
        {
            string content = "";
            public Form1()
            {
                InitializeComponent();
                this.webBrowser1.Url = new System.Uri(Application.StartupPath + "\kindeditor\e.html", System.UriKind.Absolute);
                this.webBrowser1.ObjectForScripting = this;
    
            }
            public void SetDetailContent()
            {
    
                webBrowser1.Document.InvokeScript("setContent", new object[] { content });
            }
            public string GetContent()
            {
                return content;
            }
            public void RequestContent(string str)
            {
                content = str;
                richTextBox1.Text = content;
            }
    
            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                if (richTextBox1.Focused)
                {
                    content = richTextBox1.Text;
                    SetDetailContent();
                }
            }
    
            private void webBrowser1_Resize(object sender, EventArgs e)
            {
                this.webBrowser1.Refresh();
            }
        }
    }
    https://github.com/BobinYang/HtmlAgilityPackSample
  • 相关阅读:
    mongoDB
    邮箱认证
    Django中开启事务的两种方式
    总结
    __new__和__init__和__call__方法理解
    jupyter
    text()和html()区别
    django模型中auto_now和auto_now_add的区别
    ajax嵌套陷阱
    模板内置函数(HTML)
  • 原文地址:https://www.cnblogs.com/springsnow/p/13279316.html
Copyright © 2020-2023  润新知