WinForm邮件内容编辑器的简单实现
1、概述
????? 在WinForm開(kāi)發(fā)中,您可能會(huì)碰到發(fā)送郵件時(shí)內(nèi)容編輯的問(wèn)題,大部分開(kāi)發(fā)人員第一個(gè)想法是希望將 WinForm中的內(nèi)容編輯器做到和.NET
中類似FCK一樣。今天我們就來(lái)介紹下如何實(shí)現(xiàn)和FCK一樣的內(nèi)容編輯器功能。
2、正文
?????郵件內(nèi)容編輯器的問(wèn)題,在網(wǎng)上搜索一下,碰到這個(gè)的問(wèn)題的人還不少,大多數(shù)開(kāi)發(fā)者參考的下面這篇文章中的內(nèi)容:http://www.codeproject.com/cs/miscctrl/editor_in_windows_forms.asp且不說(shuō)這篇文章中實(shí)現(xiàn)的方法的好壞。但里面包含的Microsoft.MSHTML.dll,動(dòng)輒7M多,實(shí)在是讓人看了不舒服。又或是利用WindowsLive.Writer.Interop.Mshtml和WindowsLive.Writer.Mshtml兩個(gè)組件來(lái)實(shí)現(xiàn),維護(hù)起來(lái)相當(dāng)麻煩,代碼就更不好看懂了。今天我向大家介紹一種新的內(nèi)容編輯器的實(shí)現(xiàn),徹底解決上述的問(wèn)題,總大小不過(guò)幾百KB。
?????首先我們來(lái)分析下實(shí)現(xiàn)的原理。在上述那個(gè)例子中,它利用C#中的webBrowser控件和HTML交互達(dá)到的內(nèi)容編輯器的效果,但它的實(shí)現(xiàn)卻是需要去完成單個(gè)單個(gè)的功能最后組合而成,例如,實(shí)現(xiàn)剪切的功能的核心代碼:
1?public?void?Cut()2?{
3?????webBrowser1.Document.ExecCommand("Cut",?false,?null);
4?}
5?public?void?Bold()
6?{
7????webBrowser1.Document.ExecCommand("Bold",?false,?null);
8?}
這種方法是不是太過(guò)復(fù)雜?!而且維護(hù)起來(lái)也不方便。既然是webBrowser和HTML的交互實(shí)現(xiàn)原來(lái),那為什么我們不直接做一個(gè)純HTML的郵件編輯器,然后再來(lái)和webBrowser交互呢?!這就是我今天要介紹方法的核心原理。下面我們來(lái)看看它的實(shí)現(xiàn)過(guò)程:
第一步:純HTML內(nèi)容編輯器
其實(shí)這一步最簡(jiǎn)單,基本上您在網(wǎng)上搜索HTML內(nèi)容編輯器,就會(huì)有一大堆出現(xiàn)在您面前。既然是純HTML的,記住一定是雙擊既可以打開(kāi)預(yù)覽的。如圖:
當(dāng)然了,我們還是要寫一點(diǎn)必要的JS,便于獲取和設(shè)置從webBrowser控件傳出來(lái)的內(nèi)容。
<script>//獲取郵件內(nèi)容
function?GetContent(){
????return?editor.html();
}
function?GetTitile(){
????return?document.getElementById('txttile').value;
}
function?GetFileUrl(){
????return?document.getElementById('txtfile').value;
}
function?SetTitile(arg){
????document.getElementById('txttile').value=arg;
}
function?SetContent(arg){
????editor.html(arg);}
</script>
第二步:內(nèi)容交互
這里比較簡(jiǎn)單,就是一個(gè)從webBrowser1中執(zhí)行JS函數(shù)的核心代碼:
webBrowser1.Document.InvokeScript("SetContent", objArray);
其他部分,沒(méi)什么好說(shuō)明的,直接看代碼吧。
View Code ?1?using?System;?2?using?System.Collections.Generic;
?3?using?System.ComponentModel;
?4?using?System.Data;
?5?using?System.Drawing;
?6?using?System.Linq;
?7?using?System.Text;
?8?using?System.Windows.Forms;
?9?using?System.Security.Permissions;?//需加的
10?
11?namespace?EmailEdit
12?{
13?????[PermissionSet(SecurityAction.Demand,?Name?=?"FullTrust")]//需加的
14?????[System.Runtime.InteropServices.ComVisibleAttribute(true)]//需加的?
15?????public?partial?class?SendEmail?:?Form
16?????{
17?????????public?SendEmail()
18?????????{
19?????????????InitializeComponent();
20?????????}
21?
22?????????private?void?SendEmail_Load(object?sender,?EventArgs?e)
23?????????{
24?????????????webBrowser1.Navigate(Application.StartupPath?+?"/edit/editemail.html");
25?????????}
26?
27?????????//獲取郵件信息
28?????????private?void?button1_Click(object?sender,?EventArgs?e)
29?????????{
30?????????????string?strtitle?=?webBrowser1.Document.InvokeScript("GetTitile").ToString();
31?????????????string?strfile?=?webBrowser1.Document.InvokeScript("GetFileUrl").ToString();
32?????????????string?strcontent?=?webBrowser1.Document.InvokeScript("GetContent").ToString();
33?????????????MessageBox.Show("郵件的標(biāo)題是:"?+?strtitle?+?"\n"?+?"附件:"?+?strfile?+?"\n"?+?"內(nèi)容:"?+?strcontent);
34?????????}
35?
36?????????//設(shè)置郵件信息?
37?????????private?void?webBrowser1_DocumentCompleted(object?sender,?WebBrowserDocumentCompletedEventArgs?e)
38?????????{
39?????????????//郵件標(biāo)題
40?????????????string?strtitle?=?"這是標(biāo)題";
41?????????????Object[]?objArray?=?new?Object[1];
42?????????????objArray[0]?=?(Object)strtitle;
43?????????????webBrowser1.Document.InvokeScript("SetTitile",?objArray);
44?
45?????????????//郵件內(nèi)容
46?????????????string?name?=?@"<font?color=""#FF0000""><h1>hello?world</h1></font>!sendry?lee.";
47?????????????objArray?=?new?Object[1];
48?????????????objArray[0]?=?(Object)name;
49?????????????webBrowser1.Document.InvokeScript("SetContent",?objArray);
50?????????}
51?????}
52?}
?第三步:運(yùn)行
看實(shí)現(xiàn)的效果和圖片吧:
????? 如果對(duì)您有幫助別忘了分享給其他人,感謝您的閱讀!
?
轉(zhuǎn)載于:https://www.cnblogs.com/sendrylee/archive/2012/05/24/WinForm%E9%82%AE%E4%BB%B6%E5%86%85%E5%AE%B9%E7%BC%96%E8%BE%91%E5%99%A8.html
總結(jié)
以上是生活随笔為你收集整理的WinForm邮件内容编辑器的简单实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [网络安全自学篇] 五十六.i春秋老师分
- 下一篇: 线性代数、微积分学习与回顾