发布时间 : 星期六 文章51CTO下载-自己动手编写一个浏览器 - 分享C#项目开发案例更新完毕开始阅读
第三章 浏览器程序 }
}
}
axWebBrowser2.Stop();
67 private void axWebBrowser2_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) { System.IO.FileStream fs= new FileStream(\ FileAccess.Write); StreamWriter sw=new StreamWriter(fs); sw.WriteLine(DateTime.Now.ToShortDateString()+\ sw.Close(); fs.Close(); }
private void axWebBrowser2_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e) { e.cancel=bPop; }
private void OnBeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e) { for(int i=0;i private void GoBtn_Click(object sender, System.EventArgs e) { System.Object nullObject = 0; string str = \ System.Object nullObjStr = str; Cursor.Current = Cursors.WaitCursor; axWebBrowser2.Navigate(textBox1.Text, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr); Cursor.Current = Cursors.Default; } private void axWebBrowser2_DownloadBegin(object sender, System.EventArgs e) { bPop=false; } private void axWebBrowser2_DownloadComplete(object sender, System.EventArgs e) { bPop=true; } 68 C#网络应用案例导航 3.6网页源代码浏览程序 3.6.1 步骤1-建立程序框架 首先,新建一个程序项目,如图3-23。选择使用Windows Application模板。 图3-23:网页源码浏览程序 图3-24:程序界面布局效果 3.6.2 步骤2-建立程序界面 首先在Solution Explorer中双击Form1.cs,这时进入界面设计窗口。在这里,我们可以使用Toolbox里的工具组件直接添加到程序中。 分别使用Toolbox中的Label、Button、TextBox这些标准组件布局程序界面,达到图3-24的效果。 在本例中,除了要改变各个组件的Text属性,即组件显示的文字和前景背景色外,还要对某些组件做特殊的设置,见表3-5。 表3-5:组件的属性设置 对象名称 TextBox1 Button1 Button2 TextBox2 类型 TextBox Button Button TextBox 属性 Anchor Anchor Anchor WordWrap Anchor ht Multiline ScrollBars True Both 值 Top,Left,Right Top,Right Top,Right Yes Top,Bottom,Left,Rig此时,系统会自动添加代码,生成我们需要的布局和组件属性。 第三章 浏览器程序 69 3.6.3 步骤3-实现浏览源代码功能 步骤描述 前面曾讲到过,浏览器实际上是通过Http协议从网络上获得HTML文件,然后再解释这些文件,最后再在网页中显示出内容的。我们这个源代码浏览器,实际上就是完成了前半部分,即只通过Http协议把HTML文件下载下来,不经过解释直接显示给用户。这个工作其实很简单,可以直接由WebRequest类实现。 功能实现 引入如下的名字空间以供我们在接下来的编程中方便的引用其中的组件。 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.IO; using System.Text; 1、建立获得网页数据的类 我们首先建立一个GetWebPageSource类来获得指定网址的网页源代码: public class GetWebPageSource { public GetWebPageSource() { } public GetWebPageSource(string webAddress) { this.webAddress = webAddress; } public string GetSource() { StringBuilder strSource = new StringBuilder(\ try { WebRequest WReq = WebRequest.Create(this.webAddress); WebResponse WResp = WReq.GetResponse(); // 获得数据流 StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII); string strTemp = \ while ((strTemp = sr.ReadLine()) != null) { strSource.Append(strTemp + \ } sr.Close(); } catch (WebException WebExcp) { //发生错误则报错 MessageBox.Show(WebExcp.Message, \ } return strSource.ToString(); } 70 } C#网络应用案例导航 //访问变量的方法 public void setWebAddress(string strNewAddress) { this.webAddress = strNewAddress; } //私有变量 private string webAddress; 通过这个类的GetSource函数我们就可以方便的获得指定网址的网页代码。这些代码是作为string类型的变量返回的。 然后,我们的工作就是要整合用户界面了。 2、整合用户界面 为“退出”按钮添加单击事件(方法前面已经介绍过了),在这里只需要添加一行代码: private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } Exit函数会令程序退出。 下面如果你想让用户在编辑框中输入完URL地址后按回车就可以执行,而不需要挪动鼠标去点击“源码”按钮的话,则要响应KeyPress事件,并添加以下代码: private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { char chr = e.KeyChar; //13 = Enter key if (chr == 13) { GetPageSource(); } } 其中GetPageSource是我们定义函数,它帮助我们检查URL是否合法,并使用前面写的类来获得网页源代码并显示出来。 public void GetPageSource() { string strAddress = textBox1.Text.Trim(); strAddress = strAddress.ToLower(); if ( strAddress.Length <= HTTP.Length) { //URL地址不够长 MessageBox.Show(\ MessageBoxButtons.OK); textBox1.Text = HTTP; } else if (!strAddress.StartsWith(HTTP)) { //没有输入http://标识 MessageBox.Show(\ Identifier\ textBox1.Text = HTTP; } else { // 创建GetWebPageSource类对象