close
瀏覽器的控制 (Browser)
專案下載:2012 完整版 — WebBrowser2012.zip
專案下載:基本版 — Browser.zip
專案下載:有檢視原始碼功能 — BrowserSource.zip
專案下載:2011 簡單版 — MyBrowser.zip
簡介
在 C# 當中控制 Internet Explorer (IE) 瀏覽器是一件很簡單的事情,因為 .NET framework 當中已經將 IE 的 WebBrowser 內建成一個控制元件,只要利用這個控制元件中的網址 (Url) 欄位,以及瀏覽 Navigate(url)、向前 GoForward()、向後 GoBack()
等函數,就可以輕鬆的控制瀏覽器元件的行為了。
2012 年版
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonPrev_Click(object sender, EventArgs e)
{
webBrowser.GoBack();
}
private void buttonNext_Click(object sender, EventArgs e)
{
webBrowser.GoForward();
}
private void updateState()
{
buttonPrev.Enabled = webBrowser.CanGoBack;
buttonNext.Enabled = webBrowser.CanGoForward;
if (webBrowser.Url != null)
comboBoxUrl.Text = webBrowser.Url.ToString();
}
private void buttonGo_Click(object sender, EventArgs e)
{
webBrowser.Navigate(comboBoxUrl.Text);
}
private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
updateState();
}
private void comboBoxUrl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
webBrowser.Navigate(comboBoxUrl.Text);
}
}
}
版本一
首先您可以先用 Visual Studio 中的工具列,拉出一個 WebBrowser 元件,以及一些按鈕,像是 buttonSend、buttonNext、buttonPrev 等,然後參考下列程式,撰寫控制程式即可。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Browser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void toolStripButtonGo_Click(object sender, EventArgs e)
{
goUrl(toolStripComboBoxUrl.Text);
}
private void goUrl(String url)
{
toolStripComboBoxUrl.Text = url;
webBrowser.Navigate(url);
}
private void toolStripButtonNext_Click(object sender, EventArgs e)
{
webBrowser.GoForward();
}
private void toolStripButtonPrev_Click(object sender, EventArgs e)
{
webBrowser.GoBack();
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
toolStripComboBoxUrl.Text = webBrowser.Url.ToString();
}
private void toolStripComboBoxUrl_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
goUrl(toolStripComboBoxUrl.Text);
}
}
}
}
版本二
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Browser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
goUrl(toolStripComboBox1.Text);
}
private void goUrl(String url)
{
toolStripComboBox1.Text = url;
webBrowser1.Navigate(url);
}
private void toolStripButtonNext_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
toolStripComboBox1.Text = webBrowser1.Url.ToString();
}
private void toolStripButtonPrev_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
toolStripComboBox1.Text = webBrowser1.Url.ToString();
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
toolStripComboBox1.Text = webBrowser1.Url.ToString();
}
}
}
加上檢視原始碼功能
private void toolStripButtonSource_Click(object sender, EventArgs e)
{
FormSource formSource;
String html = webBrowser1.DocumentText;
formSource = new FormSource();
formSource.setSource(html);
formSource.Visible = true;
// MessageBox.Show(html);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Browser
{
public partial class FormSource : Form
{
public FormSource()
{
InitializeComponent();
}
public void setSource(String text)
{
richTextBox1.Text = text;
}
}
}
版本三:2011 年版
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 MyBrowser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser.Navigate("http://www.google.com.tw");
this.WindowState = FormWindowState.Maximized;
}
private void toolStripComboBoxUrl_Click(object sender, EventArgs e)
{
}
private void toolStripComboBoxUrl_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
webBrowser.Navigate(toolStripComboBoxUrl.Text);
}
}
private void toolStripButtonBack_Click(object sender, EventArgs e)
{
webBrowser.GoBack();
}
private void toolStripButtonNext_Click(object sender, EventArgs e)
{
webBrowser.GoForward();
}
private void toolStripButtonGo_Click(object sender, EventArgs e)
{
webBrowser.Navigate(toolStripComboBoxUrl.Text);
}
}
}
陳鍾誠 (2010年06月15日),(網頁標題) C# : 瀏覽器的控制 (Browser),(網站標題) 免費電子書:C# 程式設計,2010年06月15日,取自 http://cs0.wikidot.com/browser ,網頁修改第 16 版。
文章標籤
全站熱搜
留言列表