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

类别:.NET开发 点击:0 评论:0 推荐:
/// 利用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);
  }

  ///本文为转载

本文地址:http://com.8s8s.com/it/it43710.htm