異世界


2018年9月25日 星期二

內崁IE

public class jgeIE : Object
{
     static public List<SHDocVw.InternetExplorer> IEs = new List<SHDocVw.InternetExplorer>();
     static public void Remove(SHDocVw.InternetExplorer ie)
     {
         int idx = IEs.IndexOf(ie);
         try { IEs[idx].Quit(); }
         catch { }
         try { IEs.RemoveAt(idx); }
         catch { }
     }
     static public void CloseAll()
     {
         for (int i = IEs.Count - 1; i >= 0; i--)
         {
             try { IEs[i].Quit(); }
             catch { }
             try { IEs.RemoveAt(i); }
             catch { }
         }
     }
     // 開心的 瀏覽
     static public void RunBrowser(Form MdiParent, TagObject tg)
     {
         //.. WebBrowser(IP)
         //           
         string _IP = tg.IP;
         string _CAption = tg.Text;
         string arg = String.Format("{0}@{1}", _IP, _CAption);
         // if (findFormBrowser(arg)) { return; }

        FormBrowser fB = new FormBrowser(MdiParent, arg);
         fB.MdiParent = MdiParent;
         fB.Show();
         fB.WindowState = FormWindowState.Maximized;

        fB.Navigate();

        /* 將 FormBrowser 嵌入
         {
             SetParent(fB.Handle, pnlBrowser.Handle);
             //ResizeControlHwnd(fB.Handle, mParnet);
             tabCtlMain.SelectTab(2);
         }*/
     }

    /* 建構式 */
     public jgeIE()
     {
         mBrowser = new SHDocVw.InternetExplorer();
         IEs.Add(mBrowser);

        mBrowser.ToolBar = 0;
         mBrowser.AddressBar = false;
         mBrowser.StatusBar = false;
         mBrowser.MenuBar = false;

        mBrowser.Visible = true;
         mBrowser.OnQuit += new SHDocVw.DWebBrowserEvents2_OnQuitEventHandler(OnQuit);
     }
     public jgeIE(string URL)
     {
         mBrowser = new SHDocVw.InternetExplorer();
         IEs.Add(mBrowser);

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

        mBrowser.ToolBar = 0;
         mBrowser.AddressBar = false;
         mBrowser.StatusBar = false;
         mBrowser.MenuBar = false;

        //mBrowser.Width = pnlWeb.Width;
         //mBrowser.Height = pnlWeb.Height;
         mBrowser.Top = 0;
         mBrowser.Left = 0;
         mBrowser.Navigate(URL);
         mBrowser.Visible = true;

        mBrowser.OnQuit += new SHDocVw.DWebBrowserEvents2_OnQuitEventHandler(OnQuit);
     }
     /* 欄位 */
     private SHDocVw.InternetExplorer mBrowser;

    /* 屬性 */
     public SHDocVw.InternetExplorer Browser { get { return mBrowser; } set { mBrowser = value; } }
     public int ToolBar { get { return mBrowser.ToolBar; } set { mBrowser.ToolBar = value; } }
     public int Left { get { return mBrowser.Left; } set { mBrowser.Left = value; } }
     public int Top { get { return mBrowser.Top; } set { mBrowser.Top = value; } }
     public int Width { get { return mBrowser.Width; } set { mBrowser.Width = value; } }
     public int Height { get { return mBrowser.Height; } set { mBrowser.Height = value; } }

    public bool AddressBar { get { return mBrowser.AddressBar; } set { mBrowser.AddressBar = value; } }
     public bool StatusBar { get { return mBrowser.StatusBar; } set { mBrowser.StatusBar = value; } }
     public bool MenuBar { get { return mBrowser.MenuBar; } set { mBrowser.MenuBar = value; } }
     public bool Visible { get { return mBrowser.Visible; } set { mBrowser.Visible = value; } }
     public System.IntPtr HWND { get { return (System.IntPtr)mBrowser.HWND; } }

    /* 方法 */
     public void ReSize(int L, int T, int W, int H)
     {
         mBrowser.Top = T;
         mBrowser.Left = L;
         mBrowser.Width = W;
         mBrowser.Height = H;
     }

    /* 事件 */
     public void OnQuit()
     {
         jIE.Remove(this.mBrowser);
     }

}


==============<<  Test   jgeIE class>>=================

private void button2_Click(object sender, EventArgs e)
{
     CreateIE(ref MyBrowser, pnlIE);
     MyBrowser.ReSize(-100,-100,pnlIE.Width,pnlIE.Height);
}


private void Navigate()
{
     Navigate(DefaultURL);
}

private void Navigate(String address)
{
      if (!String.IsNullOrEmpty(address))
      {
          if (!address.StartsWith("http://") && !address.StartsWith("https://"))
          {
              address = "http://" + address;
          }
          try
          {
              Navigate(new Uri(address));
          }
          catch (System.UriFormatException)
          {
              Navigate(new Uri(@"about:blank"));
          }
     }
     else
     {
          Navigate(new Uri(@"about:blank"));
     }
}

private void Navigate(Uri uri)
{
     MyBrowser.Browser.Navigate( uri.ToString() );           
}


#region 內建-IE
     jgeIE MyBrowser;
     private void CreateIE(ref jgeIE browser, Control parent)
     {
      browser = new jgeIE();
      Win32.SetParent(browser.HWND, parent.Handle);

    //MyBrowser.Browser.FullScreen = true;
      MyBrowser.Browser.Left = 0;
      MyBrowser.Browser.Top = 0;
      MyBrowser.Browser.Width = parent.Width;
      MyBrowser.Browser.Height = parent.Height;
     }
     public void jNavigate(string URL)
     {
      //MyBrowser.Browser.Navigate(@"http://www.google.com.tw"); //.. for test
      // MyBrowser.Browser.Navigate(URL);
      if ( (0 == URL.IndexOf("http://", 0))  || (0 == URL.IndexOf("https://", 0)) )
      {
          MyBrowser.Browser.Navigate(URL);
      }
      else
      {
          MyBrowser.Browser.Navigate("http://" + URL);
      }
     }
     private void FormBrowser_FormClosing(object sender, FormClosingEventArgs e)
     {
      jgeIE.CloseAll();
     }
#endregion

沒有留言:

張貼留言