自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Visual Studio 2010構(gòu)建Web瀏覽器應(yīng)用程序

原創(chuàng)
開發(fā) 后端
今天我們將要介紹Visual Studio 2010如何構(gòu)建Web瀏覽器應(yīng)用程序,本文是基于.NET 4.0和Visual Studio 2010完成的。

【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控件,最終的界面效果如下圖所示。

創(chuàng)建WebBrowser

工具欄調(diào)整成圖1所示的樣子后,從工具箱拖動(dòng)一個(gè)WebBrowser控件到Form上,根據(jù)Form的大小調(diào)整WebBrowser控件的大小和??课恢?,我將其??吭诘撞?,如圖2所示。

調(diào)整WebBrowser控件的大小

接下來為WebBrowser控件設(shè)置一些默認(rèn)屬性,在WebBrowser控件上點(diǎn)擊右鍵,選擇“屬性”,打開屬性對(duì)話框,隨意設(shè)置你喜歡的屬性,Url屬性表示要在WebBrowser中顯示的Web頁面,如圖3所示,我將http://www.c-sharpcorner.com設(shè)為默認(rèn)頁面。

將http://www.c-sharpcorner.com設(shè)為默認(rèn)頁面

Navigate

Navigate是WebBrowser中用來打開URL的一個(gè)方法。

webBrowser1.Navigate(new Uri(url));

下面的代碼片段是“轉(zhuǎn)到”按鈕點(diǎn)擊事件處理程序的一部分。

  1. // GO button click event handler.  
  2. private void GoButton_Click(object sender, EventArgs e)  
  3. {  
  4.     if (String.IsNullOrEmpty(UrlTextBox.Text) ||  
  5.         UrlTextBox.Text.Equals("about:blank"))  
  6.     {  
  7.         MessageBox.Show("Enter a valid URL.");  
  8.         UrlTextBox.Focus();  
  9.         return;  
  10.     }  
  11.     OpenURLInBrowser(UrlTextBox.Text);          
  12. }  
  13.    
  14. private void OpenURLInBrowser(string url)  
  15. {         
  16.     if (!url.StartsWith("http://") &&  
  17.         !url.StartsWith("https://"))  
  18.     {  
  19.         url = "http://" + url;  
  20.     }  
  21.     try 
  22.     {  
  23.         webBrowser1.Navigate(new Uri(url));  
  24.     }  
  25.     catch (System.UriFormatException)  
  26.     {  
  27.         return;  
  28.     }  

WebBrowser控件也內(nèi)置了一些瀏覽器功能,如轉(zhuǎn)到主頁,前進(jìn),后退,刷新,保存,打印和其它功能,下面的代碼片段顯示了如何使用GoForeward,GoBack,GoHome和Refresh方法。

  1. // Home button takes user home  
  2. private void HomeButton_Click(object sender, EventArgs e)  
  3. {  
  4.     webBrowser1.GoHome();  
  5. }  
  6.    
  7. // Go back  
  8. private void BackButton_Click(object sender, EventArgs e)  
  9. {  
  10.     if (webBrowser1.CanGoBack)  
  11.         webBrowser1.GoBack();  
  12. }  
  13.    
  14. // Next  
  15. private void NextButton_Click(object sender, EventArgs e)  
  16. {  
  17.     if (webBrowser1.CanGoForward)  
  18.         webBrowser1.GoForward();  
  19. }        
  20.    
  21. // Refresh  
  22. private void RefreshButton_Click(object sender, EventArgs e)  
  23. {  
  24.     webBrowser1.Refresh();  

ShowSaveAsDialog,ShowPrintDialog,ShowPrintPreviewDialog和ShowProperties方法分別用于顯示另存為,打印,打印預(yù)覽和屬性對(duì)話框,下面的代碼片段展示了如何調(diào)用這些方法。

  1. // Save button launches SaveAs dialog  
  2. private void SaveButton_Click(object sender, EventArgs e)  
  3. {  
  4.     webBrowser1.ShowSaveAsDialog();  
  5. }  
  6.    
  7. // PrintPreview button launches PrintPreview dialog  
  8. private void PrintPreviewButton_Click(object sender, EventArgs e)  
  9. {  
  10.     webBrowser1.ShowPrintPreviewDialog();  
  11. }  
  12.    
  13. // Show Print dialog  
  14. private void PrintButton_Click(object sender, EventArgs e)  
  15. {  
  16.     webBrowser1.ShowPrintDialog();  
  17. }  
  18. // Properties button  
  19. private void PropertiesButton_Click(object sender, EventArgs e)  
  20. {  
  21.     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

【編輯推薦】 

  1. Visual Studio自定義調(diào)整窗體的兩個(gè)小技巧
  2. Visual Studio 2010中關(guān)于C#的幾點(diǎn)改進(jìn)
  3. Visual Studio 2010及.Net 4新功能一覽
  4. 提高效率 用好Visual Studio 2010自定義代碼段
     

 

責(zé)任編輯:彭凡 來源: 51CTO
相關(guān)推薦

2010-01-22 09:51:31

Visual Stud

2010-11-19 12:40:12

Visual Stud云應(yīng)用程序

2011-01-12 11:56:36

Visual Stud

2010-01-15 09:30:22

Visual Stud

2010-04-01 15:10:06

Visual Stud

2010-01-08 12:14:44

ibmdwAndroid

2009-07-01 16:52:47

增加瀏覽器Visual Stud

2009-12-16 15:39:37

Visual Stud

2011-02-13 17:10:28

Visual Stud

2011-11-17 10:14:52

瀏覽器應(yīng)用程序Web App

2010-06-13 09:22:37

jQuery

2012-04-19 09:34:21

ibmdw

2012-09-05 15:20:51

Visual Stud

2012-09-24 13:23:30

Visual Stud

2013-11-22 09:58:36

2015-04-30 12:37:13

Visual Stud

2009-12-02 09:43:38

Visual Stud

2013-06-14 17:16:44

WP開發(fā)Windows PhoWP應(yīng)用

2012-03-21 09:36:33

ibmdw

2009-09-22 12:59:07

ibmdwWeb
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)