首先,實現環境VS2013+windows10。 程式界面預覽: 瀏覽器可以實現搜索,新建,關閉功能。簡單的基礎功能都能實現。 下麵是代碼: 從上到下的講解: 1.標題 2.頂部菜單 ToolStrip控制項 (1)步驟: 1)拖入ToolStirp控制項。 2)編輯ToolStrip選項button。 ...
首先,實現環境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