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; width: 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM