Visual Studio 2010构建Web浏览器应用程序

原创
开发 后端
今天我们将要介绍Visual Studio 2010如何构建Web浏览器应用程序,本文是基于.NET 4.0和Visual Studio 2010完成的。

【51CTO独家特稿】2001年,我使用C#中的WebBrowser ActiveX控件编写了我的***个应用程序,点此阅读,Kapil Sony写了一篇文章介绍了C# 2.0中的WebBrowser控件,每一次.NET新版本发布,控件和功能都会发生一些变化,现在,WebBrowser控件已属于Windows Forms控件的一部分,本文是基于.NET 4.0和Visual Studio 2010完成的,如果你使用的不是Visual Studio 2010,可以去MSDN网站下载免费的Visual C# 2010 Express。

WebBrowser控件允许开发人员在Windows Forms应用程序内构建Web浏览功能,本文将介绍在Windows Forms应用程序中如何使用WebBrowser控件。

创建WebBrowser

首先使用Visual Studio 2010或Visual C# 2010 Express创建一个Windows Forms应用程序,在这个程序中,我将会给窗体(Form)添加一个ToolStrip和一个WebBrowser控件,在ToolStrip控件中,我添加了一个Label,TextBox和一些Button控件,最终的界面效果如下图所示。

创建WebBrowser

工具栏调整成图1所示的样子后,从工具箱拖动一个WebBrowser控件到Form上,根据Form的大小调整WebBrowser控件的大小和停靠位置,我将其停靠在底部,如图2所示。

调整WebBrowser控件的大小

接下来为WebBrowser控件设置一些默认属性,在WebBrowser控件上点击右键,选择“属性”,打开属性对话框,随意设置你喜欢的属性,Url属性表示要在WebBrowser中显示的Web页面,如图3所示,我将http://www.c-sharpcorner.com设为默认页面。

将http://www.c-sharpcorner.com设为默认页面

Navigate

Navigate是WebBrowser中用来打开URL的一个方法。

webBrowser1.Navigate(new Uri(url));

下面的代码片段是“转到”按钮点击事件处理程序的一部分。

  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控件也内置了一些浏览器功能,如转到主页,前进,后退,刷新,保存,打印和其它功能,下面的代码片段显示了如何使用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方法分别用于显示另存为,打印,打印预览和属性对话框,下面的代码片段展示了如何调用这些方法。

  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();  

小结

在这篇文章中,我们介绍了在设计以及运行时如何在Windows Forms中创建WebBrowser控件,随后我们介绍了如何使用各种属性和方法,本文仅仅做了一些简要的介绍,更多的功能还得等待你在实际工作中去发现。

原文标题:Building Web Browser Application using Visual Studio 2010

【编辑推荐】 

  1. Visual Studio自定义调整窗体的两个小技巧
  2. Visual Studio 2010中关于C#的几点改进
  3. Visual Studio 2010及.Net 4新功能一览
  4. 提高效率 用好Visual Studio 2010自定义代码段
     

 

责任编辑:彭凡 来源: 51CTO
相关推荐

2010-01-22 09:51:31

Visual Stud

2010-04-01 15:10:06

Visual Stud

2011-01-12 11:56:36

Visual Stud

2010-11-19 12:40:12

Visual Stud云应用程序

2010-01-15 09:30:22

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

浏览器应用程序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

2012-03-21 09:36:33

ibmdw

2009-09-22 12:59:07

ibmdwWeb

2012-05-14 17:35:28

移动Web

2009-01-03 14:25:10

ibmdwWeb
点赞
收藏

51CTO技术栈公众号