WinForms C#:html編輯器工程源碼,含直接寫WebBrowser的文件流、IPersistStreamInit接口的聲明和一些相關的小方法
今天給大家帶來一個操作WebBrowser的一些高級方法,我專門寫了一個html編輯器的實現代碼,有需要的朋友可以自己擴充;
功能實現是直接寫流到WebBrowser內不通過臨時文件,並且支持對WebBrowser的一些高級控制(其實script可以達到的均可達到,想知道怎麼搞的可以閱讀代碼)。
其中關於IPersistStreamInit接口的聲明費了翻工夫,因爲以前在 delphi 中沒這麼麻煩,呵呵。在網絡上找了大半天沒找到,最後還是祭出Reflector,反編譯Windows.Forms,需要的朋友可以不用辛苦的自己搞了!
我在這個演示裏,製作的html編輯環境是比簡單的,您可以看看,比較比較 CodeProject 上的代碼;我採用的是ie自身提供的編輯方法,只是這樣的方式都被運用於web方式的編輯器內,就好比這個freeTextBox
以下是主要的代碼:
2 * 初始化瀏覽器狀態
3 * 指向about:blank
4 * *****************************/
5 private void Form1_Load(object sender, System.EventArgs e) {
6 object obj = null;
7 this.Show();
8 this.axWb.Navigate("about:blank",ref obj,ref obj,ref obj,ref obj);
9 //等待完成動作
10 while(axWb.ReadyState < SHDocVw.tagREADYSTATE.READYSTATE_INTERACTIVE)
11 Application.DoEvents();
12
13 //初始化html編輯器
14 InitHtmlEditor();
15 }
16
17 /*******************************
18 * 這裏是核心方法
19 * 完全調用IE自身的html編輯功能
20 * 可以看到,我採用了一種兼容的
21 * 方式,用Frame(框架),這樣
22 * 的話,默認安裝的Windows 98都
23 * 支持html編輯功能;
24 * 關鍵代碼如下:
25 * frame.document.designMode = "on";
26 * 表示開啓設計模式
27 ******************************/
28 private void InitHtmlEditor(){
29 string sw = "";
30 sw += "<html>/r/n";
31 sw += "<script language=javascript>/r/n";
32 sw += " function loadSet(){/r/n";
33 sw += " var frame=document.getElementById(/"i-frame/").contentWindow;/r/n";
34 sw += " frame.document.designMode = /"on/";/r/n";
35 sw += " frame.document.open();/r/n";
36 sw += " frame.document.write(/"<html><font color=red>hello 大家好啊!<br>我是S.F. <br>";
37 sw += " <a href=///"http://www.cnblogs.com/chinasf///">歡迎訪問我的weblog</a></font></html>/");/r/n";
38 sw += " frame.document.close();/r/n";
39 sw += " }/r/n";
40 sw += " function setBlod(obj){/r/n";
41 sw += " document.getElementById(/"i-frame/").contentWindow.document.execCommand(/"bold/");/r/n";
42 sw += " }/r/n";
43 sw += "</script>/r/n";
44 //這裏加入了一個html的button,也就是說,你可以把web模式的html編輯器的代碼完全copy進來
45 sw += "<body οnlοad=/"loadSet()/" scroll=/"yes/"><button οnclick=/"setBlod(this);/">Blod</button>/r/n";
46 sw += "<iframe id=/"i-frame/" frameBorder=/"1/" width=/"640/" height=/"480/"></iframe>/r/n";
47 sw += "</body></html>/r/n";
48
49 //寫入瀏覽器
50 WriteHtml(sw);
51 }
52
53 private void WriteHtml(string s){
54 //內存流,用於轉換string
55 MemoryStream ms = new MemoryStream();
56 try{
57 byte[] htmlcode = System.Text.Encoding.Default.GetBytes(s);
58 ms.Write(htmlcode,0,htmlcode.Length);
59 Stream dataStream = ms;
60 //恢復指針位置
61 dataStream.Seek(0,0);
62
63 if(axWb.Document!=null){
64 //轉換接口,並轉換爲IStream
65 (axWb.Document as UnsafeNativeMethods.IPersistStreamInit).Load(new UnsafeNativeMethods.ComStreamFromDataStream(dataStream));
66 }
67 }finally{
68 ms.Close();
69 }
70 }
71
72 private void button1_Click(object sender, System.EventArgs e) {
73 //獲取document,在IHTMLDocument2中取得楨
74 mshtml.IHTMLDocument3 idoc = (mshtml.IHTMLDocument3)axWb.Document;
75 mshtml.IHTMLFrameBase2 fb= (mshtml.IHTMLFrameBase2)idoc.getElementById("i-frame");
76 object obj=null;
77 fb.contentWindow.document.execCommand("bold",true,obj);
78 }
79
80 private void button3_Click(object sender, System.EventArgs e) {
81 //獲取document,在IHTMLDocument2中才有body.style
82 mshtml.IHTMLDocument2 idoc = (mshtml.IHTMLDocument2)axWb.Document;
83 //指定爲IHTMLStyle3,纔可以定製滾動條顏色
84 mshtml.IHTMLStyle3 istyle = (mshtml.IHTMLStyle3)idoc.body.style;
85 istyle.scrollbarArrowColor = "#0099FF";
86 istyle.scrollbar3dLightColor = "#FFFFFF";
87 istyle.scrollbarDarkShadowColor = "#0099FF";
88 istyle.scrollbarFaceColor = "#99CCFF";
89 istyle.scrollbarHighlightColor = "#0099FF";
90 istyle.scrollbarShadowColor = "#0099FF";
91 istyle.scrollbarTrackColor = "#FFFFFF";
92
93 }
94
95 private void button2_Click(object sender, System.EventArgs e) {
96 //查看源碼,文本方式
97 mshtml.IHTMLDocument3 idoc = (mshtml.IHTMLDocument3)axWb.Document;
98 mshtml.IHTMLFrameBase2 fb= (mshtml.IHTMLFrameBase2)idoc.getElementById("i-frame");
99 MessageBox.Show(fb.contentWindow.document.body.innerText);
100 }
101
102 private void button4_Click(object sender, System.EventArgs e) {
103 //查看源碼,HTML方式
104 mshtml.IHTMLDocument3 idoc = (mshtml.IHTMLDocument3)axWb.Document;
105 mshtml.IHTMLFrameBase2 fb= (mshtml.IHTMLFrameBase2)idoc.getElementById("i-frame");
106 MessageBox.Show(fb.contentWindow.document.body.innerHTML);
107 }