Using IE within a Windows Forms application

类别:.NET开发 点击:0 评论:0 推荐:

Using IE within a Windows Forms application

Changed this blog posting into an article. It's just too long for a post, and might be handy to keep around...

Everytime I'm busy with something I think: hmmm, would be nice to share this.. but one way or the other the time just isn't there. I will try harder...

I'm working on a simple windows forms app to browse some of the hierarchies of SharePoint Portal Server 2003, and besides a tree control on the left side  I needed a browser control at the right side to display some detail information in HTML. Of course you can add the Internet Explorer dll shdocvw.dll to the toolbox so a wrapper is generated... but there are some problems left like what values to pass for all those parameters and how do I handle events that are raised.

One of the .Net world famous programmers Lutz Roeder, developer of Reflector (http://www.aisto.com/roeder/dotnet) did write a class encapsulating the Internet Explorer Active-X control that is used in Reflector. Although he did not publish the source code (as far as I know), this file came on the web one way or the other. I found it at the following link:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23%2487PZ4pBHA.1860%40tkmsftngp04&rnum=8

The code follows below, together with some code how to use it....


// Lutz Roeders's .NET Reflector, October 2000.
// Copyright (C) 2000-2002 Lutz Roeder. All rights reserved.
// http://www.aisto.com/roeder/dotnet
// [email protected]

namespace Reflector.Library
{
 using System;
 using System.IO;
 using System.Runtime.InteropServices;
 using System.Windows.Forms;

 public class HtmlBrowser : AxHost, IWebBrowserEvents
 {
  public event BrowserNavigateEventHandler BeforeNavigate;
  public event BrowserNavigateEventHandler NavigateComplete;
  IWebBrowser control = null;
  ConnectionPointCookie cookie;
  Boolean activate = false;
  String url = String.Empty;
  String html = String.Empty;
  String body = String.Empty;

  public HtmlBrowser() : base("8856f961-340a-11d0-a96b-00c04fd705a2")
  {
   HandleCreated += new EventHandler(Me_HandleCreated);
   NavigateComplete += new BrowserNavigateEventHandler(Me_NavigateComplete);
  }

  public void Activate()
  {
   activate = true;
  }

  public void Navigate(string url)
  {
   if (!IsHandleCreated)
   {
    this.url = url;
    return;
   }

   Object flags = 0;
   Object targetFrame = String.Empty;
   Object postData = String.Empty;
   Object headers = String.Empty;
   control.Navigate(url, ref flags, ref targetFrame, ref postData, ref
    headers);
  }

  public void SetHtmlText(string text)
  {
   this.html = text;
  }

  public void SetBodyText(string text)
  {
   if (control != null)
   {
    IHTMLDocument2 document = control.GetDocument();
    if (document != null)
    {
     IHTMLElement body = document.GetBody();
     if (body != null)
     {
      if (activate) DoVerb(-4);
      body.SetInnerHTML(text);
      return;
     }
    }
   }

   this.body = text;
  }

  void Me_HandleCreated(Object s, EventArgs e)
  {
   HandleCreated -= new EventHandler(Me_HandleCreated);

   if (url == String.Empty)
   {
    url = Path.GetTempFileName();
    StreamWriter writer = new StreamWriter(url, false);
    writer.WriteLine(html);
    writer.Flush();
    writer.Close();
   }

   Navigate(url);
  }

  void Me_NavigateComplete(Object s, BrowserNavigateEventArgs e)
  {
   if (activate) DoVerb(-4);

   if (html != String.Empty)
   {
    File.Delete(url);
    this.html = String.Empty;
   }

   if (body != String.Empty)
   {
    SetBodyText(body);
    this.body = String.Empty;
   }
  }

  protected override void CreateSink()
  {
   try 
   {
    cookie = new ConnectionPointCookie(GetOcx(), this,
       typeof(IWebBrowserEvents)); } 
   catch { }
  }

  protected override void DetachSink()
  {
   try { cookie.Disconnect(); } 
   catch { }
  }

  protected override void AttachInterfaces()
  {
   try { control = (IWebBrowser) GetOcx(); } 
   catch { }
  }

  protected override Boolean IsInputKey(Keys keyData)
  {
   return (keyData == Keys.Escape) ? false : base.IsInputKey(keyData);
  }

  public void RaiseBeforeNavigate(String url, int flags, String
   targetFrameName, ref Object postData, String headers, ref Boolean cancel)
  {
   BrowserNavigateEventArgs e = new BrowserNavigateEventArgs(url, false);
   if (BeforeNavigate != null) BeforeNavigate(this, e);
   cancel = e.Cancel;
  }

  public void RaiseNavigateComplete(String url)
  {
   BrowserNavigateEventArgs e = new BrowserNavigateEventArgs(url, false);
   if (NavigateComplete != null) NavigateComplete(this, e);
  }
 }

 [Guid("eab22ac2-30c1-11cf-a7eb-0000c05bae0b"),
 InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
 public interface IWebBrowserEvents
 {
  [DispId(100)]
  void RaiseBeforeNavigate(String url, int flags, String targetFrameName,
   ref Object postData, String headers, ref Boolean cancel);
  [DispId(101)]
  void RaiseNavigateComplete(String url);
 }

 public delegate void BrowserNavigateEventHandler(Object s,
 BrowserNavigateEventArgs e);

 public class BrowserNavigateEventArgs
 {
  String url;
  Boolean cancel;

  public BrowserNavigateEventArgs(String url, Boolean cancel)
  {
   this.url = url;
   this.cancel = cancel;
  }

  public String Url
  {
   get { return url; }
  }

  public Boolean Cancel
  {
   set { cancel = value; }
   get { return cancel; }
  }
 }

 [Guid("eab22ac1-30c1-11cf-a7eb-0000c05bae0b")]
 interface IWebBrowser
 {
  void GoBack(); void GoForward(); void GoHome(); void GoSearch();
  void Navigate(String Url, ref Object Flags, ref Object targetFrame, ref
   Object postData, ref Object headers);
  void Refresh(); void Refresh2(); void Stop();
  void GetApplication(); void GetParent(); void GetContainer();
  [return: MarshalAs(UnmanagedType.Interface)]
  IHTMLDocument2 GetDocument();
 }

 [Guid("332C4425-26CB-11D0-B483-00C04FD90119"),
 InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual), ComVisible(true)]
 interface IHTMLDocument2
 {
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetScript();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetAll();
  [return: MarshalAs(UnmanagedType.Interface)]
  IHTMLElement GetBody();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetActiveElement();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetImages();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetApplets();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetLinks();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetForms();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetAnchors();
  void SetTitle([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetTitle();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetScripts();
  void SetDesignMode([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetDesignMode();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetSelection();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetReadyState();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetFrames();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetEmbeds();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetPlugins();
  void SetAlinkColor([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetAlinkColor();
  void SetBgColor([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetBgColor();
  void SetFgColor([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetFgColor();
  void SetLinkColor([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetLinkColor();
  void SetVlinkColor([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetVlinkColor();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetReferrer();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetLocation();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetLastModified();
  void SetURL([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetURL();
  void SetDomain([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetDomain();
  void SetCookie([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetCookie();
  void SetExpando([In, MarshalAs(UnmanagedType.Bool)] Boolean p);
  [return: MarshalAs(UnmanagedType.Bool)] Boolean GetExpando();
  void SetCharset([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetCharset();
  void SetDefaultCharset([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetDefaultCharset();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetMimeType();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetFileSize();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetFileCreatedDate();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetFileModifiedDate();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetFileUpdatedDate();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetSecurity();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetProtocol();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetNameProp();
  void DummyWrite([In, MarshalAs(UnmanagedType.I4)] int psarray);
  void DummyWriteln([In, MarshalAs(UnmanagedType.I4)] int psarray);
  [return: MarshalAs(UnmanagedType.Interface)]
  Object Open([In, MarshalAs(UnmanagedType.BStr)] String URL, [In,
   MarshalAs(UnmanagedType.Struct)] Object name, [In,
   MarshalAs(UnmanagedType.Struct)] Object features, [In,
   MarshalAs(UnmanagedType.Struct)] Object replace);
  void Close();
  void Clear();
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean QueryCommandSupported([In, MarshalAs(UnmanagedType.BStr)] String
   cmdID);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean QueryCommandEnabled([In, MarshalAs(UnmanagedType.BStr)] String
   cmdID);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean QueryCommandState([In, MarshalAs(UnmanagedType.BStr)] String
   cmdID);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean QueryCommandIndeterm([In, MarshalAs(UnmanagedType.BStr)] String
   cmdID);
  [return: MarshalAs(UnmanagedType.BStr)]
  String QueryCommandText([In, MarshalAs(UnmanagedType.BStr)] String cmdID);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object QueryCommandValue([In, MarshalAs(UnmanagedType.BStr)] String
   cmdID);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean ExecCommand([In, MarshalAs(UnmanagedType.BStr)] String cmdID, [In,
   MarshalAs(UnmanagedType.Bool)] Boolean showUI, [In,
   MarshalAs(UnmanagedType.Struct)] Object value);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean ExecCommandShowHelp([In, MarshalAs(UnmanagedType.BStr)] String
   cmdID);
  [return: MarshalAs(UnmanagedType.Interface)]
  Object CreateElement([In, MarshalAs(UnmanagedType.BStr)] String eTag);
  void SetOnhelp([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnhelp();
  void SetOnclick([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnclick();
  void SetOndblclick([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndblclick();
  void SetOnkeyup([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnkeyup();
  void SetOnkeydown([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnkeydown();
  void SetOnkeypress([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnkeypress();
  void SetOnmouseup([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmouseup();
  void SetOnmousedown([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmousedown();
  void SetOnmousemove([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmousemove();
  void SetOnmouseout([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmouseout();
  void SetOnmouseover([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmouseover();
  void SetOnreadystatechange([In, MarshalAs(UnmanagedType.Struct)] Object
   p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnreadystatechange();
  void SetOnafterupdate([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnafterupdate();
  void SetOnrowexit([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnrowexit();
  void SetOnrowenter([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnrowenter();
  void SetOndragstart([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndragstart();
  void SetOnselectstart([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnselectstart();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object ElementFromPoint([In, MarshalAs(UnmanagedType.I4)] int x, [In,
   MarshalAs(UnmanagedType.I4)] int y);
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetParentWindow(); // IHTMLWindow2
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetStyleSheets();
  void SetOnbeforeupdate([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnbeforeupdate();
  void SetOnerrorupdate([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnerrorupdate();
  [return: MarshalAs(UnmanagedType.BStr)]
  String toString();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object CreateStyleSheet([In, MarshalAs(UnmanagedType.BStr)] String
   bstrHref, [In, MarshalAs(UnmanagedType.I4)] int lIndex);
 }

 [Guid("3050F1FF-98B5-11CF-BB82-00AA00BDCE0B"),
 InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual), ComVisible(true)]
 interface IHTMLElement
 {
  void SetAttribute([In, MarshalAs(UnmanagedType.BStr)] String
   strAttributeName, [In, MarshalAs(UnmanagedType.Struct)] Object
   AttributeValue, [In, MarshalAs(UnmanagedType.I4)] int lFlags);
  void GetAttribute([In, MarshalAs(UnmanagedType.BStr)] String
   strAttributeName, [In, MarshalAs(UnmanagedType.I4)] int lFlags, [Out,
   MarshalAs(UnmanagedType.LPArray)] Object[] pvars);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean RemoveAttribute([In, MarshalAs(UnmanagedType.BStr)] String
   strAttributeName, [In, MarshalAs(UnmanagedType.I4)] int lFlags);
  void SetClassName([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetClassName();
  void SetId([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetId();
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetTagName();
  [return: MarshalAs(UnmanagedType.Interface)]
  IHTMLElement GetParentElement();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetStyle(); // IHTMLStyle
  void SetOnhelp([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnhelp();
  void SetOnclick([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnclick();
  void SetOndblclick([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndblclick();
  void SetOnkeydown([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnkeydown();
  void SetOnkeyup([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnkeyup();
  void SetOnkeypress([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnkeypress();
  void SetOnmouseout([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmouseout();
  void SetOnmouseover([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmouseover();
  void SetOnmousemove([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmousemove();
  void SetOnmousedown([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmousedown();
  void SetOnmouseup([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnmouseup();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetDocument();
  void SetTitle([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetTitle();
  void SetLanguage([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetLanguage();
  void SetOnselectstart([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnselectstart();
  void ScrollIntoView([In, MarshalAs(UnmanagedType.Struct)] Object
   varargStart);
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean Contains([In, MarshalAs(UnmanagedType.Interface)] IHTMLElement
   pChild);
  [return: MarshalAs(UnmanagedType.I4)]
  int GetSourceIndex();
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetRecordNumber();
  void SetLang([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetLang();
  [return: MarshalAs(UnmanagedType.I4)]
  int GetOffsetLeft();
  [return: MarshalAs(UnmanagedType.I4)]
  int GetOffsetTop();
  [return: MarshalAs(UnmanagedType.I4)]
  int GetOffsetWidth();
  [return: MarshalAs(UnmanagedType.I4)]
  int GetOffsetHeight();
  [return: MarshalAs(UnmanagedType.Interface)]
  IHTMLElement GetOffsetParent();
  void SetInnerHTML([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetInnerHTML();
  void SetInnerText([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetInnerText();
  void SetOuterHTML([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetOuterHTML();
  void SetOuterText([In, MarshalAs(UnmanagedType.BStr)] String p);
  [return: MarshalAs(UnmanagedType.BStr)]
  String GetOuterText();
  void InsertAdjacentHTML([In, MarshalAs(UnmanagedType.BStr)] String where,
   [In, MarshalAs(UnmanagedType.BStr)] String html);
  void InsertAdjacentText([In, MarshalAs(UnmanagedType.BStr)] String where,
   [In, MarshalAs(UnmanagedType.BStr)] String text);
  [return: MarshalAs(UnmanagedType.Interface)]
  IHTMLElement GetParentTextEdit();
  [return: MarshalAs(UnmanagedType.Bool)]
  Boolean GetIsTextEdit();
  void Click();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetFilters();
  void SetOndragstart([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndragstart();
  [return: MarshalAs(UnmanagedType.BStr)]
  String toString();
  void SetOnbeforeupdate([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnbeforeupdate();
  void SetOnafterupdate([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnafterupdate();
  void SetOnerrorupdate([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnerrorupdate();
  void SetOnrowexit([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnrowexit();
  void SetOnrowenter([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnrowenter();
  void SetOndatasetchanged([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndatasetchanged();
  void SetOndataavailable([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndataavailable();
  void SetOndatasetcomplete([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOndatasetcomplete();
  void SetOnfilterchange([In, MarshalAs(UnmanagedType.Struct)] Object p);
  [return: MarshalAs(UnmanagedType.Struct)]
  Object GetOnfilterchange();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetChildren();
  [return: MarshalAs(UnmanagedType.Interface)]
  Object GetAll();
 }
}

And now some example code describing how to use it... I created a window with a tree control at the left, the Internet Explorer at the right and a splitter in the middle so the controls can be resized dynamically...


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

using Reflector.Library;

namespace SPS2Admin
{
 /// 
 /// Summary description for SPS2Admin.
 /// 
 public class SPS2Admin : System.Windows.Forms.Form
 {
  /// 
  /// An Internet Explorer embedded browser instance is used for displaying detail information.
  /// 
  private HtmlBrowser _htmlBrowser;

  /// 
  /// The tree for navigating SPS structures.
  /// 
  private TreeView spsTree;

  private System.Windows.Forms.MainMenu mainMenu;
  private System.Windows.Forms.MenuItem menuItemFile;
  private System.Windows.Forms.MenuItem menuItemView;
  private System.Windows.Forms.MenuItem menuItemFileConnect;
  private System.Windows.Forms.MenuItem menuItemFileExit;
  private System.Windows.Forms.MenuItem menuItemViewWebs;
  private System.Windows.Forms.MenuItem menuItemCurrentCredentials;
  private System.Windows.Forms.MenuItem menuItemCreateProgram;
  private System.Windows.Forms.MenuItem menuItemNavigator;
  /// 
  /// Required designer variable.
  /// 
  private System.ComponentModel.Container components = null;

  public SPS2Admin()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   _htmlBrowser = new HtmlBrowser();
   _htmlBrowser.Parent = this;
   _htmlBrowser.Dock = DockStyle.Fill;

   Splitter split = new Splitter();
   split.Parent = this;
   split.Dock = DockStyle.Left;
   split.BackColor = SystemColors.Control;

   spsTree = new TreeView();
   spsTree.Parent = this;
   spsTree.Dock = DockStyle.Left;

   _htmlBrowser.Navigate("http://www.macaw.nl");
  }
  :
  :
 }
}


I did display only some relevant part of the code... this should do the job, no "design time" control, but hey.. who needs that!

Update:
I got a solution from Julien CHEYSSIAL to resolve the problem concerning controls like textbox,
combobox which were displayed with the old looking style while using the WebBrowser in a Windows Form application. You have to add this meta in the head section of your pages : <META HTTP-EQUIV="MSThemeCompatible" Content="Yes">. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnie60/html/whatsnewpublicpreview.asp and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/userex/cookbook.asp for more info.



posted on Sunday, August 31, 2003 3:50 PM

Feedback

# re: Using IE within a Windows Forms application 4/3/2004 8:19 AM Patrick Tisseghem

You are certainly going to love the WebBrowser control in VS.NET 2005! No more low-level plumbing - nice, clean managed class!

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