winform实例(2)-简单浏览器

首先,实现环境VS2013+windows10。

程序界面预览:

 

浏览器可以实现搜索,新建,关闭功能。简单的基础功能都能实现。

下面是代码:

从上到下的讲解:

1.标题

Text = "我的winform浏览器";

2.顶部菜单----ToolStrip控件

(1)步骤:

1)拖入ToolStirp控件。

2)编辑ToolStrip选项button。如下图所示:

3)实例化webbrowser。有一个websbrows控件,但这里是直接用代码实现的。

//实例化WebBrowser
WebBrowser wbActivityBrowser;

4)初始化菜单

 1         //初始化值
 2         //初始化go功能
 3         private void cb_Url_SelectedIndexChanged(object sender, EventArgs e)
 4         {
 5             btn_Go_Click(null, null);
 6         }
 7         //初始化下一页,并实现其功能。
 8         private void btn_GoPrv_Click(object sender, EventArgs e)
 9         {
10             wbActivityBrowser.GoBack();
11         }
12         //初始化上一页,并实现其功能。
13         private void btn_GoNext_Click(object sender, EventArgs e)
14         {
15             wbActivityBrowser.GoForward();
16         }
17         //初始化刷新功能
18         private void btn_Refresh_Click(object sender, EventArgs e)
19         {
20             wbActivityBrowser.Refresh();
21         }

(2)各功能详解

1)其中上一页,下一页和刷新功能实现是直接应用webBrowser控件的属性。见上的代码。

2)建立一个label和dropdownbutton。用于输入网址。

dropdownbutton事件实现按键控制

//enter键输入跳转url
        private void cb_Url_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == '\r')
                btn_Go_Click(null, null);
        }

 

3)跳转功能。

 1 //跳转button
 2         private void btn_Go_Click(object sender, EventArgs e)
 3         {
 4             string strUrl = cb_Url.Text;
 5             if (strUrl=="")
 6             {
 7                 OpenFileDialog openFileDialog1 = new OpenFileDialog();
 8                 openFileDialog1.Filter = "文本文件|*.txt|网页|*.htm*";
 9                 var s = openFileDialog1.ShowDialog();
10                 if (s == System.Windows.Forms.DialogResult.OK)
11                 {
12                     strUrl = openFileDialog1.FileName;
13                 }
14             }
15             wbActivityBrowser.Navigate(strUrl);
16         }

 

4)新建页面功能

 1 //新建页面
 2         private void btn_New_Click(object sender, EventArgs e)
 3         {
 4             TabPage tbPage = new TabPage();
 5             tbPage.Text = "新建页面";
 6             WebBrowser wbNewWebBrz = new WebBrowser();
 7             wbNewWebBrz.Dock = DockStyle.Fill;
 8             wbNewWebBrz.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
 9             wbNewWebBrz.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
10             tbPage.Controls.Add(wbNewWebBrz);
11             tabControl1.TabPages.Add(tbPage);
12             tabControl1.SelectedTab = tbPage;
13         }

 5)关闭功能

1 //关闭页面
2         private void btn_Close_Click(object sender, EventArgs e)
3         {
4             TabPage tpPage = tabControl1.SelectedTab;
5             if (tpPage != null)
6                 tabControl1.TabPages.Remove(tpPage);
7             if (tabControl1.TabPages.Count == 0)
8                 this.Close();
9         }

3.网页正文部分

1)拖入tabControl控件。tabControl用于显示网页内容。

1 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
2         {
3             TabPage tpPage = tabControl1.SelectedTab;
4             if (tpPage != null)
5                 wbActivityBrowser = (WebBrowser)tpPage.Controls[0];
6         }

2)将url的title显示在tab上

//将url的title显示在tab上
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            TabPage tpPage = tabControl1.SelectedTab;            
            tpPage.Text = wbActivityBrowser.DocumentTitle;
            
        }

 

4. 浏览器状态底栏

控件:一个toolstrip和一个label。

 1 //web状态
 2         private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
 3         {
 4             if(wbActivityBrowser.Url != null)
 5                 cb_Url.Text = wbActivityBrowser.Url.ToString();
 6             switch (wbActivityBrowser.ReadyState)
 7             {
 8                 case WebBrowserReadyState.Complete:
 9                     lbl_status.Text = "完成";
10                     break;
11                 case WebBrowserReadyState.Interactive:
12                     lbl_status.Text = "部分完成";
13                     break;
14                 case WebBrowserReadyState.Loaded:
15                     lbl_status.Text = "已初始化";
16                     break;
17                 case WebBrowserReadyState.Loading:
18                     lbl_status.Text = "正在加载";
19                     break;
20                 case WebBrowserReadyState.Uninitialized:
21                     lbl_status.Text = "未加载";
22                     break;
23             }
24         }

5.浏览器配置方面

//启动浏览器的配置
        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader dr = new StreamReader("config.text");
            string strUrl = "";
            while ((strUrl = dr.ReadLine()) != null)
            {
                btn_New_Click(null, null);
                cb_Url.Text = strUrl;
                tabControl1_SelectedIndexChanged(null, null);
                btn_Go_Click(null, null);
                webBrowser1_ProgressChanged(null, null);
            }
            dr.Close();
        }

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //将每个选项卡浏览器当前网址保存到文本文件
            StreamWriter sw = new StreamWriter("config.text");
            foreach (TabPage tp in tabControl1.TabPages)
            {
                WebBrowser wb = (WebBrowser)tp.Controls[0];
                if (wb.Url != null)
                    sw.WriteLine(wb.Url.ToString());
            }
            sw.Close();
        }

其中,把config.text文件放入bin目录下,内容为网址。功能实现其打开首页网址。可以自定义你的程序启动的网址。

 

6.最后,所有代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Diagnostics;
  6 using System.Drawing;
  7 using System.IO;
  8 using System.Linq;
  9 using System.Text;
 10 using System.Threading.Tasks;
 11 using System.Windows.Forms;
 12 
 13 namespace Browsers
 14 {
 15     public partial class Form1 : Form
 16     {
 17         
 18         public Form1()
 19         {
 20             InitializeComponent();
 21             Text = "我的winform浏览器";
 22         }
 23         //默认浏览器打开链接
 24         private void button1_Click(object sender, EventArgs e)
 25         {
 26             Process.Start("www.baidu.com");
 27         }
 28 
 29         
 30         
 31         //跳转button
 32         private void btn_Go_Click(object sender, EventArgs e)
 33         {
 34             string strUrl = cb_Url.Text;
 35             if (strUrl=="")
 36             {
 37                 OpenFileDialog openFileDialog1 = new OpenFileDialog();
 38                 openFileDialog1.Filter = "文本文件|*.txt|网页|*.htm*";
 39                 var s = openFileDialog1.ShowDialog();
 40                 if (s == System.Windows.Forms.DialogResult.OK)
 41                 {
 42                     strUrl = openFileDialog1.FileName;
 43                 }
 44             }
 45             wbActivityBrowser.Navigate(strUrl);
 46         }
 47 
 48         //初始化值
 49         //初始化go功能
 50         private void cb_Url_SelectedIndexChanged(object sender, EventArgs e)
 51         {
 52             btn_Go_Click(null, null);
 53         }
 54         //初始化下一页,并实现其功能。
 55         private void btn_GoPrv_Click(object sender, EventArgs e)
 56         {
 57             wbActivityBrowser.GoBack();
 58         }
 59         //初始化上一页,并实现其功能。
 60         private void btn_GoNext_Click(object sender, EventArgs e)
 61         {
 62             wbActivityBrowser.GoForward();
 63         }
 64         //初始化刷新功能,并实现其功能。
 65         private void btn_Refresh_Click(object sender, EventArgs e)
 66         {
 67             wbActivityBrowser.Refresh();
 68         }
 69 
 70         //将url的title显示在tab上
 71         private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
 72         {
 73             TabPage tpPage = tabControl1.SelectedTab;            
 74             tpPage.Text = wbActivityBrowser.DocumentTitle;
 75             
 76         }
 77         //web状态
 78         private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
 79         {
 80             if(wbActivityBrowser.Url != null)
 81                 cb_Url.Text = wbActivityBrowser.Url.ToString();
 82             switch (wbActivityBrowser.ReadyState)
 83             {
 84                 case WebBrowserReadyState.Complete:
 85                     lbl_status.Text = "完成";
 86                     break;
 87                 case WebBrowserReadyState.Interactive:
 88                     lbl_status.Text = "部分完成";
 89                     break;
 90                 case WebBrowserReadyState.Loaded:
 91                     lbl_status.Text = "已初始化";
 92                     break;
 93                 case WebBrowserReadyState.Loading:
 94                     lbl_status.Text = "正在加载";
 95                     break;
 96                 case WebBrowserReadyState.Uninitialized:
 97                     lbl_status.Text = "未加载";
 98                     break;
 99             }
100         }
101 
102 
103         //启动浏览器的配置
104         private void Form1_Load(object sender, EventArgs e)
105         {
106             StreamReader dr = new StreamReader("config.text");
107             string strUrl = "";
108             while ((strUrl = dr.ReadLine()) != null)
109             {
110                 btn_New_Click(null, null);
111                 cb_Url.Text = strUrl;
112                 tabControl1_SelectedIndexChanged(null, null);
113                 btn_Go_Click(null, null);
114                 webBrowser1_ProgressChanged(null, null);
115             }
116             dr.Close();
117         }
118         //实例化WebBrowser
119         WebBrowser wbActivityBrowser;
120         private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
121         {
122             TabPage tpPage = tabControl1.SelectedTab;
123             if (tpPage != null)
124                 wbActivityBrowser = (WebBrowser)tpPage.Controls[0];
125         }
126         //enter键输入跳转url
127         private void cb_Url_KeyDown(object sender, KeyEventArgs e)
128         {
129             if (e.KeyValue == '\r')
130                 btn_Go_Click(null, null);
131         }
132 
133         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
134         {
135             //将每个选项卡浏览器当前网址保存到文本文件
136             StreamWriter sw = new StreamWriter("config.text");
137             foreach (TabPage tp in tabControl1.TabPages)
138             {
139                 WebBrowser wb = (WebBrowser)tp.Controls[0];
140                 if (wb.Url != null)
141                     sw.WriteLine(wb.Url.ToString());
142             }
143             sw.Close();
144         }
145         
146         //新建页面
147         private void btn_New_Click(object sender, EventArgs e)
148         {
149             TabPage tbPage = new TabPage();
150             tbPage.Text = "新建页面";
151             WebBrowser wbNewWebBrz = new WebBrowser();
152             wbNewWebBrz.Dock = DockStyle.Fill;
153             wbNewWebBrz.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
154             wbNewWebBrz.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
155             tbPage.Controls.Add(wbNewWebBrz);
156             tabControl1.TabPages.Add(tbPage);
157             tabControl1.SelectedTab = tbPage;
158         }
159 
160 
161         //关闭页面
162         private void btn_Close_Click(object sender, EventArgs e)
163         {
164             TabPage tpPage = tabControl1.SelectedTab;
165             if (tpPage != null)
166                 tabControl1.TabPages.Remove(tpPage);
167             if (tabControl1.TabPages.Count == 0)
168                 this.Close();
169         }
170         
171     }
172 }
View Code

 

posted @ 2016-10-29 14:56  智博的日常  阅读(3781)  评论(4编辑  收藏  举报