亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#開發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器

 更新時(shí)間:2022年06月14日 09:17:06   作者:springsnow  
這篇文章介紹了C#開發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

做Web開發(fā)時(shí),我們經(jīng)常會(huì)用到HTML富文本框編輯器來編寫文章或產(chǎn)品描述的詳細(xì)內(nèi)容,常用的編輯器有FCKEditor、CKEditor 、TinyMCE、KindEditor和ueditor(百度的),

我們知道WinForm上有一個(gè)webBrowser控件,本文正是采用webBrowser結(jié)合Web上的HTML編輯器KindEditor來實(shí)現(xiàn)的,KindEditor是一個(gè)國人寫的編輯器,輕量級(jí)用起來挺不錯(cuò),至少我知道目前拍拍和開源中國就是用此編輯器。

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

首先我們需要去官網(wǎng)或者Github:https://github.com/kindsoft/kindeditor下載一份代碼,然后解壓到我們項(xiàng)目的bin文件夾下,然后在bin/KindEditor目錄下新建一個(gè)名字為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的朋友對(duì)以上代碼應(yīng)該不陌生,因?yàn)樗鼘?shí)際上就是初始化一個(gè) HTML編輯器而已,我們還在代碼中定義了一個(gè)setContent方法,該方法就是用來設(shè)置HTML編輯器的內(nèi)容,我們?cè)贑#代碼中需要調(diào)用該方法.

好了,下面我們回到WinForm上面,我們?cè)诮缑嫔侠粋€(gè)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();
        }
    }
}

到此這篇關(guān)于WinForm實(shí)現(xiàn)HTML編輯器的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論