WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
2007-03-26 15:19
726 查看
首先多谢朋友们的捧场;
今天给大家带来一个操作WebBrowser的一些高级方法,我专门写了一个html编辑器的实现代码,有需要的朋友可以自己扩充;
功能实现是直接写流到WebBrowser内不通过临时文件,并且支持对WebBrowser的一些高级控制(其实script可以达到的均可达到,想知道怎么搞的可以阅读代码)。
其中关于IPersistStreamInit接口的声明费了翻工夫,因为以前在 delphi 中没这么麻烦,呵呵。在网络上找了大半天没找到,最后还是祭出Reflector,反编译Windows.Forms,需要的朋友可以不用辛苦的自己搞了!
我在这个演示里,制作的html编辑环境是比简单的,您可以看看,比较比较 CodeProject 上的代码;我采用的是ie自身提供的编辑方法,只是这样的方式都被运用于web方式的编辑器内,就好比这个freeTextBox
以下是主要的代码:
1
/**//********************************
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://www2.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 onload=/"loadSet()/" scroll=/"yes/"><button onclick=/"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
}
这里下载完整工程代码。
今天给大家带来一个操作WebBrowser的一些高级方法,我专门写了一个html编辑器的实现代码,有需要的朋友可以自己扩充;
功能实现是直接写流到WebBrowser内不通过临时文件,并且支持对WebBrowser的一些高级控制(其实script可以达到的均可达到,想知道怎么搞的可以阅读代码)。
其中关于IPersistStreamInit接口的声明费了翻工夫,因为以前在 delphi 中没这么麻烦,呵呵。在网络上找了大半天没找到,最后还是祭出Reflector,反编译Windows.Forms,需要的朋友可以不用辛苦的自己搞了!
我在这个演示里,制作的html编辑环境是比简单的,您可以看看,比较比较 CodeProject 上的代码;我采用的是ie自身提供的编辑方法,只是这样的方式都被运用于web方式的编辑器内,就好比这个freeTextBox
以下是主要的代码:
1
/**//********************************
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://www2.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 onload=/"loadSet()/" scroll=/"yes/"><button onclick=/"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
}
这里下载完整工程代码。
相关文章推荐
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- WinForms C#:html编辑器工程源码,含直接写WebBrowser的文件流、IPersistStreamInit接口的声明和一些相关的小方法
- 获取iframe中的内容、查找获取指定元素(关于用c++调用WEBBROWSER控件,使用相关接口操作web页面元素的一些方法)
- 直接访问WebBrowser控件中的HTML源码
- [第一篇]先发布一个高亮代码在线html编辑器(.NET控件),附源码,调用方法说明
- 直接访问WebBrowser控件中的HTML源码
- 直接访问WebBrowser控件中的HTML源码
- Spring boot项目 使用ip+port+contextPath进行访问的时候会直接显示出项目中的一些接口信息和相关数据
- 直接访问WebBrowser控件中的HTML源码
- 直接访问WebBrowser控件中的HTML源码
- 直接访问WebBrowser控件中的HTML源码
- 直接访问WebBrowser控件中的HTML源码
- 直接访问WebBrowser控件中的HTML源码 (收藏)
- 直接访问WebBrowser控件中的HTML源码
- Maven获取工程相关仓库的源码和javadoc方法
- 直接访问WebBrowser控件中的HTML源码
- 直接访问webbrowser控件中的html源码