/// 利用IE浏览器的Com组件在WinForm中显示资源文件中的HTML文件.
///
/// 这里重点有两个:
/// 1. 调用IE的COM组件
/// 2. 调用编译到exe文件中的resource
/// 具体步骤如下,具体用法请查询帮助:
/// 0. 在Project中添加about.htm, 内容自定,属性设置为"Embedded Resource"
/// 1. 在菜单tools->customize toolbox里面选中COM组件"microsot web 浏览器"
/// 2. 在Solution explorer->Test(项目名)->References右键添加Microsoft.mshtml(mshtml.dll)
/// 3. 在WinForm窗体上放置浏览器新增加的浏览器控件axWebBrowser1
/// 4. 添加链接按钮linkAbout
/// 5. 关键代码如下:
private void linkAbout_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
this.navigate("about:blank");
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)this.axWebBrowser1.Document;
String aboutHtml = this.getStringResource("about.htm");
System.Diagnostics.Debug.Write(aboutHtml);
object[] obj = {aboutHtml};
doc.write(obj);
}
private string getStringResource(String name)
{
System.Reflection.Assembly asm = this.GetType().Assembly;
//list all resources in this file
System.Diagnostics.Debug.WriteLine("found resouces:");
foreach (String rs in asm.GetManifestResourceNames())
System.Diagnostics.Debug.WriteLine(rs);
name = this.GetType().Namespace+"."+name;
System.IO.Stream strm = asm.GetManifestResourceStream(name);
//convert to String with default system encoding.
return new System.IO.StreamReader(strm,System.Text.Encoding.Default).ReadToEnd();
}
private void navigate(String url)
{
object flags = 0;
object targetFrame = String.Empty;
object postData = String.Empty;
object headers = String.Empty;
this.axWebBrowser1.Navigate(url,ref flags,ref targetFrame,ref postData,ref headers);
}
///后记:
///兄弟刚学C#,抛砖引玉,希望大家共同学习,有错误的地方请告诉我 msn:
[email protected] ///还有两个地方不清楚:
///1.有文章提到 res://Test.exe/about.htm 的用法好象行不通.
///2.重复点击about.htm的时候会不显示.
///我还写了一个聊天室刷屏的程序,需要的朋友请留言,呵呵
本文地址:http://com.8s8s.com/it/it45959.htm