引用AxSHDocVw.dll ,SHDocVw.dllusing AxSHDocVw; using System; using System.Drawing; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; using mshtml;namespace Maydeal.WebBrowser { public class WebBrowserEx : AxWebBrowser, IDocHostUIHandler { public bool m_bCanGoBack; public bool m_bCanGoForward; public bool CanGoForward { get { return m_bCanGoForward; } } public bool CanGoBack { get { return m_bCanGoBack; } } public event ContextMenuEventHandler ContextMenu; public WebBrowserEx(Control controlParent) { IHTMLDocument2 iHTMLDocument2 = null; ICustomDoc iCustomDoc = null;
base.BeginInit(); controlParent.Controls.Add(this); base.CommandStateChange += new DWebBrowserEvents2_CommandStateChangeEventHandler( OnWebStateChange); base.EndInit(); base.RegisterAsBrowser = true; base.RegisterAsDropTarget = true; base.Silent = false; Navigate("about:blank"); iHTMLDocument2 = base.Document as IHTMLDocument2; iCustomDoc = iHTMLDocument2 as ICustomDoc; iCustomDoc.SetUIHandler(this); } public void OnWebStateChange(object sender, DWebBrowserEvents2_CommandStateChangeEvent e) { if (e.command == 2) { m_bCanGoBack = e.enable; } if (e.command == 1) { m_bCanGoForward = e.enable; } } public void Navigate(string strUrl) {
using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using mshtml;namespace Maydeal.WebBrowser
{
public class WebBrowserEx : AxWebBrowser, IDocHostUIHandler
{
public bool m_bCanGoBack; public bool m_bCanGoForward;
public bool CanGoForward
{
get
{
return m_bCanGoForward;
}
} public bool CanGoBack
{
get
{
return m_bCanGoBack;
}
} public event ContextMenuEventHandler ContextMenu; public WebBrowserEx(Control controlParent)
{
IHTMLDocument2 iHTMLDocument2 = null;
ICustomDoc iCustomDoc = null;
base.BeginInit();
controlParent.Controls.Add(this);
base.CommandStateChange += new DWebBrowserEvents2_CommandStateChangeEventHandler( OnWebStateChange);
base.EndInit();
base.RegisterAsBrowser = true;
base.RegisterAsDropTarget = true;
base.Silent = false;
Navigate("about:blank");
iHTMLDocument2 = base.Document as IHTMLDocument2;
iCustomDoc = iHTMLDocument2 as ICustomDoc;
iCustomDoc.SetUIHandler(this);
} public void OnWebStateChange(object sender, DWebBrowserEvents2_CommandStateChangeEvent e)
{
if (e.command == 2)
{
m_bCanGoBack = e.enable;
}
if (e.command == 1)
{
m_bCanGoForward = e.enable;
}
} public void Navigate(string strUrl)
{
object local = null;
local = Missing.Value;
base.Navigate(strUrl, ref local, ref local, ref local, ref local);
} public void NavigateNW(string strUrl)
{
object local2 = null;
object local1 = null;
local2 = Missing.Value;
local1 = "http://www.Maydeal.com";
base.Navigate(strUrl, ref local2, ref local1, ref local2, ref local2);
} public void ShowContextMenu(uint dwID, tagPOINT ppt, [MarshalAs(UnmanagedType.IUnknown)] object pcmdtReserved, [MarshalAs(UnmanagedType.IDispatch)] object pdispReserved)
{
ContextMenuConstants contextMenuConstants = (ContextMenuConstants)dwID;
Point point2 = new Point();
point2 = new Point(ppt.x, ppt.y);
Point point1 = point2;
if (ContextMenu != null)
{
ContextMenu(this, new ContextMenuEventArgs(contextMenuConstants, point1, pcmdtReserved, pdispReserved));
}
} public void GetHostInfo(DOCHOSTUIINFO pInfo)
{
} public void ShowUI(uint dwID, [MarshalAs(UnmanagedType.Interface)] IntPtr pActiveObject, [MarshalAs(UnmanagedType.Interface)] IntPtr pCommandTarget, [MarshalAs(UnmanagedType.Interface)] IntPtr pFrame, [MarshalAs(UnmanagedType.Interface)] IntPtr pDoc)
{
} public void HideUI()
{
} public void UpdateUI()
{
} public void EnableModeless(int fEnable)
{
} public void OnDocWindowActivate(int fActivate)
{
} public void OnFrameWindowActivate(int fActivate)
{
} public void ResizeBorder(tagRECT prcBorder, [MarshalAs(UnmanagedType.Interface)] IntPtr pUIWindow, int fRameWindow)
{
} public void TranslateAccelerator(tagMSG lpmsg, ref Guid pguidCmdGroup, uint nCmdID)
{
} public void GetOptionKeyPath(string pchKey, uint dw)
{
} public void GetDropTarget([MarshalAs(UnmanagedType.Interface)] IntPtr pDropTarget, [MarshalAs(UnmanagedType.Interface)] IntPtr ppDropTarget)
{
} public void GetExternal(object ppDispatch)
{
} public void TranslateUrl(uint dwTranslate, ushort pchURLIn, IntPtr ppchURLOut)
{
} public void FilterDataObject(IDataObject pDO, IDataObject ppDORet)
{
} protected void raise_ContextMenu(object i1, ContextMenuEventArgs i2)
{
if (ContextMenu != null)
{
ContextMenu(i1, i2);
}
}
}}
你Google里搜索下
public class WebBrowserPage : Panel
{
private bool m_bStatusBar; private bool m_bProgressBar; private WebBrowserEx m_browserWeb; private Label m_labelProgressBar; private Label m_labelStatusBar; private Label m_labelHider;
public WebBrowserEx WebBrowser
{
get
{
return m_browserWeb;
}
} public bool CanGoForward
{
get
{
return m_browserWeb.CanGoForward;
}
} public bool CanGoBack
{
get
{
return m_browserWeb.CanGoBack;
}
} public string Address
{
get
{
return m_browserWeb.LocationURL;
}
}
#region 标题
public string Title
{
get
{
return m_browserWeb.LocationName;
}
}
#endregion
public event UpdateUIEventHandler UpdateUI;
public event AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler NewWindiws2; public WebBrowserPage()
{
base.Resize += new EventHandler(this.OnResize);
m_bProgressBar = false;
m_bStatusBar = true;
m_labelHider = new Label();
base.Controls.Add(m_labelHider);
m_browserWeb = new WebBrowserEx(this);
m_browserWeb.CommandStateChange += new DWebBrowserEvents2_CommandStateChangeEventHandler( OnWebStateChange);
m_browserWeb.ProgressChange += new DWebBrowserEvents2_ProgressChangeEventHandler( OnWebProgressChange);
m_browserWeb.TitleChange += new DWebBrowserEvents2_TitleChangeEventHandler( OnWebTitleChange);
m_browserWeb.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler( OnWebNavigateComplete);
m_browserWeb.StatusTextChange += new DWebBrowserEvents2_StatusTextChangeEventHandler( OnWebStatusChange);
m_browserWeb.NewWindow2+=new DWebBrowserEvents2_NewWindow2EventHandler(OnNewWindow2EventHandler);
m_labelProgressBar = new Label();
m_labelProgressBar.BorderStyle = BorderStyle.None;
m_labelProgressBar.Visible = false;
base.Controls.Add(m_labelProgressBar);
m_labelStatusBar = new Label();
m_labelStatusBar.BorderStyle = BorderStyle.None;
m_labelStatusBar.Visible = false;
base.Controls.Add(m_labelStatusBar);
}
private void OnNewWindow2EventHandler(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
// if(m_browserWeb.ReadyState!=SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
// {
this.NewWindiws2(this, e );
return;
// }
//WebBrowserPage NewWindows=new WebBrowserPage(); //m_browserWeb.LocationName="新窗口";
// e.ppDisp= NewWindows.m_browserWeb.Application;
// NewWindows.Show();
}
protected void OnResize(object sender, EventArgs e)
{
if (m_browserWeb != null)
{
int k; if (!m_bProgressBar && !m_bStatusBar)
{
k = base.Height + 2;
}
else
{
k = base.Height - 12;
}
m_browserWeb.SetBounds(-1, -1, base.Width + 2, k);
m_labelHider.SetBounds(-1, m_browserWeb.Bottom - 1, base.Width, 1);
if (m_bProgressBar)
{
int j; if (m_bStatusBar)
{
j = 50;
}
else
{
j = base.Width;
}
m_labelProgressBar.SetBounds(0, m_browserWeb.Bottom + 1, j, 12);
m_labelProgressBar.Visible = true;
}
if (m_bStatusBar)
{
int i; if (m_bProgressBar)
{
i = base.Width - m_labelProgressBar.Width;
}
else
{
i = base.Width;
}
m_labelStatusBar.SetBounds((m_bProgressBar == false) ? 0 : 50, m_browserWeb.Bottom + 1, i, 12);
m_labelStatusBar.Visible = true;
}
}
} public void OnWebStateChange(object sender, DWebBrowserEvents2_CommandStateChangeEvent e)
{
if (e.command == 2 && UpdateUI != null)
{
UpdateUI(this, new UpdateUIEventArgs(UpdateUIElement.Back));
}
if (e.command == 1 && UpdateUI != null)
{
UpdateUI(this, new UpdateUIEventArgs(UpdateUIElement.Forward));
}
} public void OnWebProgressChange(object sender, DWebBrowserEvents2_ProgressChangeEvent e)
{
double d3 = e.progress;
double d2 = e.progressMax;
double d1 = d3 / d2 * 50.0;
m_labelProgressBar.Width = (int)d1;
} public void OnWebTitleChange(object sender, DWebBrowserEvents2_TitleChangeEvent e)
{
if (UpdateUI != null)
{
UpdateUI(this, new UpdateUIEventArgs(UpdateUIElement.Title));
}
} public void OnWebNavigateComplete(object sender, DWebBrowserEvents2_NavigateComplete2Event e)
{
if (UpdateUI != null)
{
UpdateUI(this, new UpdateUIEventArgs(UpdateUIElement.Address));
}
} public void OnWebStatusChange(object sender, DWebBrowserEvents2_StatusTextChangeEvent e)
{
m_labelStatusBar.Text = e.text;
}
}