Visual Studio 2010構(gòu)建Web瀏覽器應(yīng)用程序
原創(chuàng)【51CTO獨(dú)家特稿】2001年,我使用C#中的WebBrowser ActiveX控件編寫了我的***個(gè)應(yīng)用程序,點(diǎn)此閱讀,Kapil Sony寫了一篇文章介紹了C# 2.0中的WebBrowser控件,每一次.NET新版本發(fā)布,控件和功能都會(huì)發(fā)生一些變化,現(xiàn)在,WebBrowser控件已屬于Windows Forms控件的一部分,本文是基于.NET 4.0和Visual Studio 2010完成的,如果你使用的不是Visual Studio 2010,可以去MSDN網(wǎng)站下載免費(fèi)的Visual C# 2010 Express。
WebBrowser控件允許開發(fā)人員在Windows Forms應(yīng)用程序內(nèi)構(gòu)建Web瀏覽功能,本文將介紹在Windows Forms應(yīng)用程序中如何使用WebBrowser控件。
創(chuàng)建WebBrowser
首先使用Visual Studio 2010或Visual C# 2010 Express創(chuàng)建一個(gè)Windows Forms應(yīng)用程序,在這個(gè)程序中,我將會(huì)給窗體(Form)添加一個(gè)ToolStrip和一個(gè)WebBrowser控件,在ToolStrip控件中,我添加了一個(gè)Label,TextBox和一些Button控件,最終的界面效果如下圖所示。
工具欄調(diào)整成圖1所示的樣子后,從工具箱拖動(dòng)一個(gè)WebBrowser控件到Form上,根據(jù)Form的大小調(diào)整WebBrowser控件的大小和??课恢?,我將其??吭诘撞?,如圖2所示。
接下來為WebBrowser控件設(shè)置一些默認(rèn)屬性,在WebBrowser控件上點(diǎn)擊右鍵,選擇“屬性”,打開屬性對(duì)話框,隨意設(shè)置你喜歡的屬性,Url屬性表示要在WebBrowser中顯示的Web頁面,如圖3所示,我將http://www.c-sharpcorner.com設(shè)為默認(rèn)頁面。
Navigate
Navigate是WebBrowser中用來打開URL的一個(gè)方法。
webBrowser1.Navigate(new Uri(url));
下面的代碼片段是“轉(zhuǎn)到”按鈕點(diǎn)擊事件處理程序的一部分。
- // GO button click event handler.
- private void GoButton_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(UrlTextBox.Text) ||
- UrlTextBox.Text.Equals("about:blank"))
- {
- MessageBox.Show("Enter a valid URL.");
- UrlTextBox.Focus();
- return;
- }
- OpenURLInBrowser(UrlTextBox.Text);
- }
- private void OpenURLInBrowser(string url)
- {
- if (!url.StartsWith("http://") &&
- !url.StartsWith("https://"))
- {
- url = "http://" + url;
- }
- try
- {
- webBrowser1.Navigate(new Uri(url));
- }
- catch (System.UriFormatException)
- {
- return;
- }
- }
WebBrowser控件也內(nèi)置了一些瀏覽器功能,如轉(zhuǎn)到主頁,前進(jìn),后退,刷新,保存,打印和其它功能,下面的代碼片段顯示了如何使用GoForeward,GoBack,GoHome和Refresh方法。
- // Home button takes user home
- private void HomeButton_Click(object sender, EventArgs e)
- {
- webBrowser1.GoHome();
- }
- // Go back
- private void BackButton_Click(object sender, EventArgs e)
- {
- if (webBrowser1.CanGoBack)
- webBrowser1.GoBack();
- }
- // Next
- private void NextButton_Click(object sender, EventArgs e)
- {
- if (webBrowser1.CanGoForward)
- webBrowser1.GoForward();
- }
- // Refresh
- private void RefreshButton_Click(object sender, EventArgs e)
- {
- webBrowser1.Refresh();
- }
ShowSaveAsDialog,ShowPrintDialog,ShowPrintPreviewDialog和ShowProperties方法分別用于顯示另存為,打印,打印預(yù)覽和屬性對(duì)話框,下面的代碼片段展示了如何調(diào)用這些方法。
- // Save button launches SaveAs dialog
- private void SaveButton_Click(object sender, EventArgs e)
- {
- webBrowser1.ShowSaveAsDialog();
- }
- // PrintPreview button launches PrintPreview dialog
- private void PrintPreviewButton_Click(object sender, EventArgs e)
- {
- webBrowser1.ShowPrintPreviewDialog();
- }
- // Show Print dialog
- private void PrintButton_Click(object sender, EventArgs e)
- {
- webBrowser1.ShowPrintDialog();
- }
- // Properties button
- private void PropertiesButton_Click(object sender, EventArgs e)
- {
- webBrowser1.ShowPropertiesDialog();
- }
小結(jié)
在這篇文章中,我們介紹了在設(shè)計(jì)以及運(yùn)行時(shí)如何在Windows Forms中創(chuàng)建WebBrowser控件,隨后我們介紹了如何使用各種屬性和方法,本文僅僅做了一些簡(jiǎn)要的介紹,更多的功能還得等待你在實(shí)際工作中去發(fā)現(xiàn)。
原文標(biāo)題:Building Web Browser Application using Visual Studio 2010
【編輯推薦】
- Visual Studio自定義調(diào)整窗體的兩個(gè)小技巧
- Visual Studio 2010中關(guān)于C#的幾點(diǎn)改進(jìn)
- Visual Studio 2010及.Net 4新功能一覽
- 提高效率 用好Visual Studio 2010自定義代碼段