C#开发WinForm项目实现HTML编辑器

 更新时间:2022年06月14日 09:17:06   作者:springsnow  
这篇文章介绍了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();
        }
    }
}

到此这篇关于WinForm实现HTML编辑器的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C#实现rar压缩与解压缩文件的方法

    C#实现rar压缩与解压缩文件的方法

    这篇文章主要介绍了C#实现rar压缩与解压缩文件的方法,实例分析了C#利用winrar程序实现文件的压缩与解压缩的相关技巧,需要的朋友可以参考下
    2015-06-06
  • 详解c#与python的交互方式

    详解c#与python的交互方式

    这篇文章主要介绍了详解c#与python的交互方式,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • C#Winform窗口移动方法

    C#Winform窗口移动方法

    今天小编就为大家分享一篇C#Winform窗口移动方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • C#集合之集(set)的用法

    C#集合之集(set)的用法

    这篇文章介绍了C#集合之集(set)的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#中sealed修饰符的具体使用

    C#中sealed修饰符的具体使用

    在 C# 中,sealed 是一个修饰符,用于限制继承和重写,本文就来介绍一下具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • C#实现坦克大战游戏

    C#实现坦克大战游戏

    这篇文章主要为大家详细介绍了C#实现坦克大战游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • C#生成漂亮验证码完整代码类

    C#生成漂亮验证码完整代码类

    本文主要介绍了C#生成漂亮验证码的完整代码类。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • WinForm特效之桌面上的遮罩层实现方法

    WinForm特效之桌面上的遮罩层实现方法

    这篇文章主要介绍了WinForm特效之桌面上的遮罩层实现方法,是一个非常实用的技巧,需要的朋友可以参考下
    2014-09-09
  • .Net笔记:System.IO之Stream的使用详解

    .Net笔记:System.IO之Stream的使用详解

    本篇文章是对.Net中System.IO之Stream的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • WPF自定义TreeView控件样式实现QQ联系人列表效果

    WPF自定义TreeView控件样式实现QQ联系人列表效果

    TreeView控件在项目中使用比较频繁,下面这篇文章主要给大家介绍了关于WPF自定义TreeView控件样式实现QQ联系人列表效果的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2018-04-04

最新评论