利用IE浏览器的Com组件在WinForm中显示资源中的HTML文件

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 steeven 的 blog

  /// 利用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: steeven_lee@citiz.net
  ///还有两个地方不清楚:
  ///1.有文章提到 res://test.exe/about.htm 的用法好象行不通.
  ///2.重复点击about.htm的时候会不显示.
  ///我还写了一个聊天室刷屏的程序,需要的朋友请留言,呵呵

本文关键:IE 浏览器 COM Winform 资源 resource HTML
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top