C#開發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器
做Web開發(fā)時(shí),我們經(jīng)常會(huì)用到HTML富文本框編輯器來(lái)編寫文章或產(chǎn)品描述的詳細(xì)內(nèi)容,常用的編輯器有FCKEditor、CKEditor 、TinyMCE、KindEditor和ueditor(百度的),
我們知道WinForm上有一個(gè)webBrowser控件,本文正是采用webBrowser結(jié)合Web上的HTML編輯器KindEditor來(lái)實(shí)現(xiàn)的,KindEditor是一個(gè)國(guó)人寫的編輯器,輕量級(jí)用起來(lái)挺不錯(cuò),至少我知道目前拍拍和開源中國(guó)就是用此編輯器。
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方法,該方法就是用來(lái)設(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)文章
WinForm特效之桌面上的遮罩層實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm特效之桌面上的遮罩層實(shí)現(xiàn)方法,是一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
WPF自定義TreeView控件樣式實(shí)現(xiàn)QQ聯(lián)系人列表效果
TreeView控件在項(xiàng)目中使用比較頻繁,下面這篇文章主要給大家介紹了關(guān)于WPF自定義TreeView控件樣式實(shí)現(xiàn)QQ聯(lián)系人列表效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-04-04

