異世界


2013年12月16日 星期一

C# IE 運用


加入參考 : SHDocVw
image 
   1: //

   2: // 調用 jIE  Class

   3: //

   4: jIE MyBrowser;

   5: private void CreateIE()

   6: {

   7:     MyBrowser = new jIE();

   8:     Win32.SetParent(MyBrowser.HWND, pnlWeb.Handle);

   9:  

  10:     MyBrowser.Browser.FullScreen = true;

  11: }

  12:  

  13: public void Navigate()

  14: {

  15:     MyBrowser.Browser.Navigate(@"http://www.google.com.tw");

  16:     //webBrowser1.Navigate(MyUrl);

  17: }

  18:  

  19: private void FormBrowser_FormClosing(object sender, FormClosingEventArgs e)

  20: {

  21:      jIE.CloseAll();

  22: }

  23:  

  24: ====================================

  25:  

  26: class jIE : Object

  27:     {

  28:  static public List<SHDocVw.InternetExplorer> IEs = new List<SHDocVw.InternetExplorer>();

  29:  static public void Remove(SHDocVw.InternetExplorer ie)

  30:  {

  31:      int idx = IEs.IndexOf(ie);

  32:      try { IEs[idx].Quit(); }

  33:      catch { }

  34:      try { IEs.RemoveAt(idx); }

  35:      catch { }

  36:  }

  37:  static public void CloseAll()

  38:  {

  39:      for (int i = IEs.Count - 1; i >= 0; i--)

  40:      {

  41:          try { IEs[i].Quit(); }

  42:          catch { }

  43:          try { IEs.RemoveAt(i); }

  44:          catch { }

  45:      }

  46:  }

  47:  

  48:  /* 建構式 */

  49:  public jIE()

  50:  {

  51:      mBrowser = new SHDocVw.InternetExplorer();

  52:      IEs.Add(mBrowser);

  53:  

  54:      mBrowser.ToolBar = 0;

  55:      mBrowser.AddressBar = false;

  56:      mBrowser.StatusBar = false;

  57:      mBrowser.MenuBar = false;

  58:  

  59:      mBrowser.Visible = true;

  60:      mBrowser.OnQuit += new SHDocVw.DWebBrowserEvents2_OnQuitEventHandler(OnQuit);

  61:  }

  62:  public jIE(string URL)

  63:  {

  64:      mBrowser = new SHDocVw.InternetExplorer();

  65:      IEs.Add(mBrowser);

  66:  

  67:      // Win32.SetParent((System.IntPtr)MyBrowser.HWND, pnlWeb.Handle);

  68:  

  69:      mBrowser.ToolBar = 0;

  70:      mBrowser.AddressBar = false;

  71:      mBrowser.StatusBar = false;

  72:      mBrowser.MenuBar = false;

  73:  

  74:      //mBrowser.Width = pnlWeb.Width;

  75:      //mBrowser.Height = pnlWeb.Height;

  76:      mBrowser.Top = 0;

  77:      mBrowser.Left = 0;

  78:      mBrowser.Navigate(URL);

  79:      mBrowser.Visible = true;

  80:  

  81:      mBrowser.OnQuit += new SHDocVw.DWebBrowserEvents2_OnQuitEventHandler(OnQuit);

  82:  }

  83:  

  84:  /* 欄位 */

  85:  private SHDocVw.InternetExplorer mBrowser;

  86:  

  87:  /* 屬性 */

  88:  public SHDocVw.InternetExplorer Browser { get { return mBrowser; } set { mBrowser = value; } }

  89:  public int ToolBar { get { return mBrowser.ToolBar; } set { mBrowser.ToolBar = value; } }

  90:  public int Left { get { return mBrowser.Left; } set { mBrowser.Left = value; } }

  91:  public int Top { get { return mBrowser.Top; } set { mBrowser.Top = value; } }

  92:  public int Width { get { return mBrowser.Width; } set { mBrowser.Width = value; } }

  93:  public int Height { get { return mBrowser.Height; } set { mBrowser.Height = value; } }

  94:  

  95:  public bool AddressBar { get { return mBrowser.AddressBar; } set { mBrowser.AddressBar = value; } }

  96:  public bool StatusBar { get { return mBrowser.StatusBar; } set { mBrowser.StatusBar = value; } }

  97:  public bool MenuBar { get { return mBrowser.MenuBar; } set { mBrowser.MenuBar = value; } }

  98:  public bool Visible { get { return mBrowser.Visible; } set { mBrowser.Visible = value; } }

  99:  public System.IntPtr HWND { get { return (System.IntPtr)mBrowser.HWND; } }

 100:  

 101:  /* 方法 */

 102:  public void ReSize(int L, int T, int W, int H)

 103:  {

 104:      mBrowser.Width = W;

 105:      mBrowser.Height = H;

 106:      mBrowser.Top = T;

 107:      mBrowser.Left = L;

 108:  }

 109:  

 110:  /* 事件 */

 111:  public void OnQuit()

 112:  {

 113:      jIE.Remove(this.mBrowser);

 114:  }

 115:  

 116:     }


=============

Microsoft.mshtml =>



Interop.SHDocVw => Microsoft Internet Controls

image

沒有留言:

張貼留言