贵州大学实验报告学院:计算机学院专业:软件工程班级:软件123班姓名***学号1***实验组实验时间2015-5-3指导教师蔡丽成绩实验项目名称Web浏览器编程实验目的通过本实验掌握C#中Web浏览器编程的方法,了解其区别与适用场合。实验要求掌握C#中Web浏览器编程的方法,了解其区别与适用场合。实验原理使用VS中的WebBrowser控件。实验环境熟悉VisualStudio开发环境,了解C#的UDP编程方法。实验步骤1.设计程序界面。2.实现程序功能。实验内容实现简单的Web浏览器,要求使用VS中的WebBrowser控件。实验数据核心代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net;usingSystem.IO;namespaceSelfBrowser{publicpartialclassfrmMain:Form{publicfrmMain(){InitializeComponent();pageWebBrowser.Navigate();this.Text=pageWebBrowser.DocumentTitle+-SelfBrowser网页浏览器1.0演示版;}privatevoidtoolStrip1_ItemClicked(objectsender,ToolStripItemClickedEventArgse){switch(toolStrip1.Items.IndexOf(e.ClickedItem)){case0:pageSave.PerformClick();break;case1:pageBack.PerformClick();break;case2:pageForward.PerformClick();break;case3:pageStop.PerformClick();break;case4:pageRefresh.PerformClick();break;case5:pageHome.PerformClick();break;case6:localView.PerformClick();break;case9:this.Text=urlAddress.Text+-SelfBrowser网页浏览器1.0演示版;toolStripStatusView.Text=正在打开网页+urlAddress.Text+...;pageWebBrowser.Navigate(urlAddress.Text);AddItem_urlAddress();break;}}privatevoidbrowserExit_Click(objectsender,EventArgse){Application.Exit();}privatevoidpageBack_Click(objectsender,EventArgse){pageWebBrowser.GoBack();}privatevoidpageForward_Click(objectsender,EventArgse){pageWebBrowser.GoForward();}privatevoidpageStop_Click(objectsender,EventArgse){pageWebBrowser.Stop();}privatevoidpageRefresh_Click(objectsender,EventArgse){pageWebBrowser.Refresh();}privatevoidpageHome_Click(objectsender,EventArgse){pageWebBrowser.GoHome();}//自定义,用于添加地址项privatevoidAddItem_urlAddress(){intAddressIndex=urlAddress.FindStringExact(urlAddress.Text);if(AddressIndex0){urlAddress.Items.Add(urlAddress.Text);}}privatevoidurlAddress_KeyPress(objectsender,KeyPressEventArgse){if(e.KeyChar==(char)13){this.Text=urlAddress.Text+-SelfBrowser网页浏览器1.0演示版;toolStripStatusView.Text=正在打开网页+urlAddress.Text+...;pageWebBrowser.Navigate(urlAddress.Text);AddItem_urlAddress();}}privatevoidpageWebBrowser_DocumentCompleted(objectsender,WebBrowserDocumentCompletedEventArgse){urlAddress.Text=pageWebBrowser.Url.ToString();this.Text=pageWebBrowser.DocumentTitle+-SelfBrowser网页浏览器1.0演示版;toolStripStatusView.Text=完毕;}privatevoidpageSave_Click(objectsender,EventArgse){pageWebBrowser.ShowSaveAsDialog();}privatevoidlocalView_Click(objectsender,EventArgse){pageWebBrowser.Navigate(c:/);//pageWebBrowser.Navigate(::{20D04FE0-3AEA-1069-A2D8-08002B30309D});urlAddress.Text=我的电脑;this.Text=我的电脑-SelfBrowser网页浏览器1.0演示版;}//使浏览器显示的页面随着用户选择网址栏中地址更新privatevoidurlAddress_SelectedIndexChanged(objectsender,EventArgse){pageWebBrowser.Navigate(urlAddress.Text);}privatevoidbrowserHelp_Click(objectsender,EventArgse){frmAboutboxdlgAboutbox=newfrmAboutbox();dlgAboutbox.Show();}privatevoidtoolbtnBaiduSearch_Click(objectsender,EventArgse){Encodinggb2312Encoding=Encoding.GetEncoding(UTF-8);stringuri==+System.Web.HttpUtility.UrlEncode(tooltbxKeyword.Text,gb2312Encoding);//pageWebBrowser.Navigate(uri);HttpWebRequestrequest=(HttpWebRequest)HttpWebRequest.Create(uri);HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();Streamstream=response.GetResponseStream();StreamReadersr=newStreamReader(stream,Encoding.UTF8);pageWebBrowser.DocumentText=sr.ReadToEnd();sr.Close();stream.Close();}privatevoidgb2312Encode_Click(objectsender,EventArgse){frmCodeViewdlgCodeView=newfrmCodeView();dlgCodeView.Text=网页源码(采用GB2312编码);dlgCodeView.Show();StreamReadersr=newStreamReader(pageWebBrowser.DocumentStream,Encoding.Default);//首先必须解码dlgCodeView.setCode=sr.ReadToEnd();sr.Close();}privatevoidUtf8Encode_Click(objectsender,EventArgse){frmCodeViewdlgCodeView=newfrmCodeView();dlgCodeView.Text=网页源码(采用UTF-8编码);dlgCodeView.Show();StreamReadersr=newStreamReader(pageWebBrowser.DocumentStream,Encoding.UTF8);//解析采用UTF-8编码形式的网页dlgCodeView.setCode=sr.ReadToEnd();sr.Close();}privatevoidtooltbxKeyword_KeyPress(objectsender,KeyPressEventArgse){if(e.KeyChar==(char)13){toolbtnBaiduSearch.PerformClick();}}//使得本浏览器可以独立自主地显示网页。而无须依赖外部浏览器privatevoidpageWebBrowser_NewWindow(objectsender,CancelEventArgse){stringnewUrl=pageWebBrowser.StatusText;toolStripStatusView.Text=正在打开网页+newUrl+...;pageWebBrowser.Navigate(newUrl);e.Cancel=true;//使外部无法捕获此事件}privatevoidtoolStripButton1_Click(objectsender,EventArgse){}privatevoidtoolStripButton7_Click(objectsender,EventArgse){}}}实验运行及调试结果:主页:点击页面:后退:实验总结通过实验掌握了C#中Web浏览器编程的方法。指导教师意见签名:年月日