Visual Studio 2010构建Web浏览器应用程序
2012-05-14 10:56
260 查看
每一次.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控件,最终的界面效果如下图所示。
工具栏调整成图1所示的样子后,从工具箱拖动一个WebBrowser控件到Form上,根据Form的大小调整WebBrowser控件的大小和停靠位置,我将其停靠在底部,如图2所示。
接下来为WebBrowser控件设置一些默认属性,在WebBrowser控件上点击右键,选择“属性”,打开属性对话框,随意设置你喜欢的属性,Url属性表示要在WebBrowser中显示的Web页面,如图3所示,我将http://www.c-sharpcorner.com设为默认页面。
Navigate
Navigate是WebBrowser中用来打开URL的一个方法。
下面的代码片段是“转到”按钮点击事件处理程序的一部分。
WebBrowser控件也内置了一些浏览器功能,如转到主页,前进,后退,刷新,保存,打印和其它功能,下面的代码片段显示了如何使用GoForeward,GoBack,GoHome和Refresh方法。
ShowSaveAsDialog,ShowPrintDialog,ShowPrintPreviewDialog和ShowProperties方法分别用于显示另存为,打印,打印预览和属性对话框,下面的代码片段展示了如何调用这些方法。
小结
在这篇文章中,我们介绍了在设计以及运行时如何在Windows Forms中创建WebBrowser控件,随后我们介绍了如何使用各种属性和方法,本文仅仅做了一些简要的介绍,更多的功能还得等待你在实际工作中去发现。
WebBrowser控件允许开发人员在Windows Forms应用程序内构建Web浏览功能,本文将介绍在Windows Forms应用程序中如何使用WebBrowser控件。
创建WebBrowser
首先使用Visual Studio 2010或Visual C# 2010 Express创建一个Windows Forms应用程序,在这个程序中,我将会给窗体(Form)添加一个ToolStrip和一个WebBrowser控件,在ToolStrip控件中,我添加了一个Label,TextBox和一些Button控件,最终的界面效果如下图所示。
工具栏调整成图1所示的样子后,从工具箱拖动一个WebBrowser控件到Form上,根据Form的大小调整WebBrowser控件的大小和停靠位置,我将其停靠在底部,如图2所示。
接下来为WebBrowser控件设置一些默认属性,在WebBrowser控件上点击右键,选择“属性”,打开属性对话框,随意设置你喜欢的属性,Url属性表示要在WebBrowser中显示的Web页面,如图3所示,我将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控件,随后我们介绍了如何使用各种属性和方法,本文仅仅做了一些简要的介绍,更多的功能还得等待你在实际工作中去发现。
相关文章推荐
- 一起谈.NET技术,Visual Studio 2010构建Web浏览器应用程序
- Visual Studio 2010构建Web浏“.NET研究”览器应用程序
- 使用 Visual Studio 2005中的ASP.NET 移动控件创建电子书浏览器应用程序
- SharePoint 2010 创建SPSite对象出错,提示“找不到位于 http://xxxx 的 Web 应用程序。请确认正确键入了此 URL
- MOSS 2010:Visual Studio 2010开发体验(4)——应用程序页面(ApplicationPage)
- 使用ASP.NET 构建 Web 应用程序快速入门-8小时的免费培训视频
- Spring学习笔记之构建Spring Web应用程序
- 使用Docker 1.12.x构建多容器Web应用程序
- Visual Studio 2010 发布应用程序到其他计算机上时 mfc100ud.dll丢失无法运行
- Web 应用程序的基本安全做法 (Visual Studio)
- 使用Visual Studio 2010本地构建libxml-python
- 使用AJAX技术构建更优秀的Web应用程序
- 让Visual Studio 2008 和 2010支持Web Services Enhancements (WSE) 3.0
- 使用缓存构建更快的 Web 应用程序
- MOSS 2010:Visual Studio 2010开发体验(18)——在独立的Silverlight应用程序中通过WCF访问SharePoint数据
- Web(浏览器)打开运行WinForm应用程序
- 使用ASP.NET MVC构建HTML5离线web应用程序
- visual studio 2010下配置OpenCV2.1,应用程序无法正常启动(0xc0150002)
- Web 浏览器样式的 MFC 应用程序 资料收集
- 构建Web应用程序结构