由于最近在做毕业设计,需要用到一些简单的浏览器功能,于是学习了一下,顺便写篇博客~~大牛请勿喷,菜鸟练练手~
实现界面如下:(简单朴素版@_@||)
button_go实现如下:
private void button_go_Click(object sender, EventArgs e) { string url = comboBox_url.Text.Trim(); webBrowser1.Navigate(url); }
//每次浏览网页后,combox_url中的文本变成当前网页的url private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { comboBox_url.Text = webBrowser1.Url.ToString(); }
button_back实现如下:
//返回上一个浏览过的网页 private void button_back_Click(object sender, EventArgs e) { webBrowser1.GoBack(); }
点击一个新的链接时,会弹出新的窗口,默认是ie浏览器打开,应该使它还在此界面显示,而不是在用默认浏览器打开。
//让webBrowser具有IE特性 private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { e.Cancel = true; if (webBrowser1.Document.ActiveElement != null) { webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href")); comboBox_url.Text = webBrowser1.Document.ActiveElement.GetAttribute("href"); }
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboBox_url.Items.Clear();//清空所有项目 comboBox_url.Text = "http://www.baidu.com/";//列表框初始文本 } private void button_go_Click(object sender, EventArgs e) { string url = comboBox_url.Text.Trim(); webBrowser1.Navigate(url); } //每次浏览网页后,combox_url中的文本变成当前网页的url private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { comboBox_url.Text = webBrowser1.Url.ToString(); } //返回上一个浏览过的网页 private void button_back_Click(object sender, EventArgs e) { webBrowser1.GoBack(); } private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { e.Cancel = true; if (webBrowser1.Document.ActiveElement != null) { webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href")); comboBox_url.Text = webBrowser1.Document.ActiveElement.GetAttribute("href"); } } } }