C#開發(fā)WinForm項目實現(xiàn)HTML編輯器
做Web開發(fā)時,我們經(jīng)常會用到HTML富文本框編輯器來編寫文章或產(chǎn)品描述的詳細(xì)內(nèi)容,常用的編輯器有FCKEditor、CKEditor 、TinyMCE、KindEditor和ueditor(百度的),
我們知道WinForm上有一個webBrowser控件,本文正是采用webBrowser結(jié)合Web上的HTML編輯器KindEditor來實現(xiàn)的,KindEditor是一個國人寫的編輯器,輕量級用起來挺不錯,至少我知道目前拍拍和開源中國就是用此編輯器。
KindEditor的官方地址為:http://kindeditor.net/down.php
首先我們需要去官網(wǎng)或者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的朋友對以上代碼應(yīng)該不陌生,因為它實際上就是初始化一個 HTML編輯器而已,我們還在代碼中定義了一個setContent方法,該方法就是用來設(shè)置HTML編輯器的內(nèi)容,我們在C#代碼中需要調(diào)用該方法.
好了,下面我們回到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();
}
}
}到此這篇關(guān)于WinForm實現(xiàn)HTML編輯器的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF自定義TreeView控件樣式實現(xiàn)QQ聯(lián)系人列表效果
TreeView控件在項目中使用比較頻繁,下面這篇文章主要給大家介紹了關(guān)于WPF自定義TreeView控件樣式實現(xiàn)QQ聯(lián)系人列表效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2018-04-04

