109,891
社区成员




public partial class BrowserForm : Form
{
#region Fields
private bool responsed;
#endregion
#region Constructors
public BrowserForm()
{
InitializeComponent();
Application.Idle += new EventHandler(Application_Idle);
this.webBrowser.DocumentTitleChanged += new EventHandler(webBrowser_DocumentTitleChanged);
SHDocVw.WebBrowser_V1 browser = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;
browser.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnBrowserNewWindow);
}
#endregion
#region Functions
private void Application_Idle(object sender, EventArgs e)
{
this.pictureBoxBack.Enabled = this.webBrowser.CanGoBack;
this.pictureBoxForward.Enabled = this.webBrowser.CanGoForward;
}
private void pictureBox_MouseEnter(object sender, EventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
if (pictureBox.Enabled)
{
pictureBox.BackColor = Color.FromArgb(222, 222, 222);
}
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
if (pictureBox.Enabled)
{
pictureBox.BackColor = Color.FromArgb(192, 192, 192);
}
}
private void pictureBox_MouseLeave(object sender, EventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
pictureBox.BackColor = SystemColors.Control;
}
private void pictureBoxBack_Click(object sender, EventArgs e)
{
this.webBrowser.GoBack();
}
private void pictureBoxForward_Click(object sender, EventArgs e)
{
this.webBrowser.GoForward();
}
private void textBoxAddress_KeyDown(object sender, KeyEventArgs e)
{
Keys keys = e.KeyCode;
if (keys == Keys.Enter)
{
CommandGo();
}
}
private void pictureBoxGo_Click(object sender, EventArgs e)
{
CommandGo();
}
private void webBrowser_DocumentTitleChanged(object sender, EventArgs e)
{
string title = this.webBrowser.DocumentTitle;
if (string.IsNullOrEmpty(title))
{
this.Text = "浏览器";
}
else
{
this.Text = string.Format("{0} - 浏览器", title);
}
}
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
Uri url = e.Url;
if (url.Scheme == Uri.UriSchemeHttp)
{
this.textBoxAddress.Text = url.AbsoluteUri;
}
}
private void OnBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Processed = true;
this.webBrowser.Navigate(URL);
}
#endregion
#region Helper Functions
private void CommandGo()
{
string address = this.textBoxAddress.Text;
if (string.IsNullOrEmpty(address))
{
address = "about:blank";
}
this.webBrowser.Navigate(address);
AutoCompleteStringCollection collection = this.textBoxAddress.AutoCompleteCustomSource;
int index = collection.IndexOf(address);
if (index == -1)
{
collection.Insert(0, address);
if (collection.Count > 10)
{
collection.RemoveAt(collection.Count - 1);
}
}
else
{
collection.RemoveAt(index);
collection.Insert(0, address);
}
}
#endregion
}
SHDocVw.WebBrowser_V1 browser = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;
browser.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnBrowserNewWindow);
private void OnBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Processed = true;
this.webBrowser.Navigate(URL);
}