C#開發(fā)WinForm項(xiàng)目實(shí)現(xiàn)HTML編輯器
做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)文章
C#中前臺(tái)線程和后臺(tái)線程的區(qū)別與聯(lián)系
這篇文章主要介紹了C#中前臺(tái)線程和后臺(tái)線程的區(qū)別與聯(lián)系,本文先講解了它們的區(qū)別,然后給出了一個(gè)例子來驗(yàn)證這些區(qū)別,需要的朋友可以參考下2015-06-06
Unity讀取Excel文件轉(zhuǎn)換XML格式文件
這篇文章主要為大家詳細(xì)介紹了Unity讀取Excel文件轉(zhuǎn)換XML格式文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
C# datatable 不能通過已刪除的行訪問該行的信息處理方法
采用datatable.Rows[i].Delete()刪除行后再訪問該表時(shí)出現(xiàn)出現(xiàn)“不能通過已刪除的行訪問該行的信息”的錯(cuò)誤2012-11-11
C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(3)
這篇文章介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì),文章主要內(nèi)容是關(guān)于購票、座位顏色狀態(tài)的改變及場(chǎng)次座位狀態(tài)的顯示,需要的朋友可以參考下2015-11-11
C#中FlagsAttribute屬性在enum中的應(yīng)用詳解
這篇文章主要介紹了C#中FlagsAttribute屬性在enum中的應(yīng)用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能
這篇文章主要為大家介紹了使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換實(shí)例分析
這篇文章主要介紹了C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換的方法,結(jié)合實(shí)例分析了在DoNet2.0與Donet3.5環(huán)境下實(shí)現(xiàn)對(duì)象與JSON轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-11-11

