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

       每一次.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.     }  
  29. } 

  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();  
  25. } 

  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();  
  22. } 

  小结

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值