引言
很早就有搞一個浏覽器的想法了
實現功能
預覽圖如下
當前浏覽器的
浏覽器的分頁功能
當前頁面的狀態
實現過程以及關鍵點
新建一個vs
界面
一個MenuStrip實現最上面的菜單
兩個ToolStrip分別是工具欄和地址欄
一個TabControl也就是浏覽器的主體了
一個StatusStrip也就是狀態欄了
另外為了使窗體大小變化時控件也隨著變化注意使用控件的dock屬性
搭成如下界面
順便說一句和vs
this
this
this
this
this
this
this
這些通過我們拖拽控件
輔助方法
#region //輔助方法
/// <summary>
/// 當在浏覽器地址欄敲
/// </summary>
/// <param name=
/// <param name=
private void tscbUrl_KeyDown(object sender
{
if (e
{
newCurrentPageUrl(tscbUrl
}
}
/// <summary>
/// 新建空白頁
/// </summary>
private void newPage()
{
tscbUrl
TabPage mypage = new TabPage();
WebBrowser tempBrowser = new WebBrowser();
tempBrowser
tempBrowser
tempBrowser
tempBrowser
tempBrowser
mypage
tabControl
tabControl
}
/// <summary>
/// 臨時浏覽器進度變化事件
/// </summary>
/// <param name=
/// <param name=
void tempBrowser_ProgressChanged(object sender
{
toolStripProgressBar
toolStripProgressBar
}
/// <summary>
/// 新建一頁並定向到指定url
/// </summary>
/// <param name=
private void newPage(string address)
{
TabPage mypage = new TabPage();
WebBrowser tempBrowser = new WebBrowser();
tempBrowser
tempBrowser
tempBrowser
tempBrowser
tempBrowser
tempBrowser
mypage
tabControl
}
/// <summary>
/// 獲取當前浏覽器
/// </summary>
/// <returns>當前浏覽器</returns>
private WebBrowser getCurrentBrowser()
{
WebBrowser currentBrowser = (WebBrowser)tabControl
return currentBrowser;
}
/// <summary>
/// 處理字符串為合法url
/// </summary>
/// <param name=
/// <returns></returns>
private Uri getUrl(string address)
{
string tempaddress = address;
if ((!address
{
tempaddress =
}
Uri myurl;
try
{
myurl = new Uri(tempaddress);
}
catch
{
myurl = new Uri(
}
return myurl;
}
/// <summary>
/// 截取字符串為指定長度
/// </summary>
/// <param name=
/// <returns></returns>
private string newstring(string oldstring)
{
string temp;
if (oldstring
{
temp = oldstring;
}
else
{
temp = oldstring
}
return temp;
}
/// <summary>
/// 設置"前進"
/// </summary>
private void setStatusButton()
{
backButton
forwordButton
}
#endregion
說明
菜單欄
#region//菜單欄
/// <summary>
/// 另存為
/// </summary>
/// <param name=
/// <param name=
private void saveasToolStripMenuItem_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 打印
/// </summary>
/// <param name=
/// <param name=
private void printToolStripMenuItem_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 打印御覽
/// </summary>
/// <param name=
/// <param name=
private void printPreToolStripMenuItem_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 關閉浏覽器
/// </summary>
/// <param name=
/// <param name=
private void exitToolStripMenuItem_Click(object sender
{
Application
}
/// <summary>
/// 頁面設置
/// </summary>
/// <param name=
/// <param name=
private void pageSetupToolStripMenuItem_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 屬性設置
/// </summary>
/// <param name=
/// <param name=
private void propeToolStripMenuItem_Click(object sender
{
getCurrentBrowser()
}
#region//關於
private void aboutToolStripMenuItem_Click(object sender
{
AboutBox
myabout
}
private void tipToolStripMenuItem_Click(object sender
{
MessageBox
}
#endregion
#endregion
說明
工具欄
#region//工具欄
/// <summary>
/// 後退
/// </summary>
/// <param name=
/// <param name=
private void backButton_Click(object sender
{
getCurrentBrowser()
setStatusButton();
}
/// <summary>
/// 前進
/// </summary>
/// <param name=
/// <param name=
private void forwordButton_Click(object sender
{
getCurrentBrowser()
setStatusButton();
}
/// <summary>
/// 停止
/// </summary>
/// <param name=
/// <param name=
private void stopButton_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 刷新
/// </summary>
/// <param name=
/// <param name=
private void refreshButton_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 定向到主頁
/// </summary>
/// <param name=
/// <param name=
private void homeButton_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 搜索
/// </summary>
/// <param name=
/// <param name=
private void searchButton_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 打印
/// </summary>
/// <param name=
/// <param name=
private void printButton_Click(object sender
{
getCurrentBrowser()
}
/// <summary>
/// 新建空白頁
/// </summary>
/// <param name=
/// <param name=
private void newButton_Click(object sender
{
newPage();
}
/// <summary>
/// 使當前的浏覽器定位到給定url
/// </summary>
/// <param name=
/// <param name=
private void gotoButton_Click(object sender
{
newCurrentPageUrl(tscbUrl
}
#endregion
說明
初始化
#region//初始化
/// <summary>
/// 初始化
/// </summary>
/// <param name=
/// <param name=
private void Form
{
initMainForm();
}
/// <summary>
/// 初始化浏覽器
/// </summary>
private void initMainForm()
{
TabPage mypage = new TabPage();
WebBrowser tempBrowser = new WebBrowser();
tempBrowser
tempBrowser
tempBrowser
tempBrowser
tempBrowser
tempBrowser
mypage
tabControl
}
#endregion
說明
臨時浏覽器事件
#region//臨時浏覽器事件
/// <summary>
/// 臨時浏覽器狀態變化事件
/// </summary>
/// <param name=
/// <param name=
void tempBrowser_StatusTextChanged(object sender
{
WebBrowser myBrowser = (WebBrowser)sender;
if (myBrowser != getCurrentBrowser())
{
return;
}
else
{
toolStripStatusLabel
}
}
/// <summary>
/// 在當前頁面上重新定向
/// </summary>
/// <param name=
private void newCurrentPageUrl(String address)
{
getCurrentBrowser()
}
/// <summary>
/// 臨時浏覽器產生新窗體事件
/// </summary>
/// <param name=
/// <param name=
void tempBrowser_NewWindow(object sender
{
//獲取觸發tempBrowser_NewWindow事件的浏覽器
WebBrowser myBrowser = (WebBrowser)sender;
//獲取觸發tempBrowser_NewWindow事件的浏覽器所在TabPage
TabPage mypage = (TabPage)myBrowser
//通過StatusText屬性獲得新的url
string NewURL = ((WebBrowser)sender)
//生成新的一頁
TabPage TabPageTemp = new TabPage();
//生成新的tempBrowser
WebBrowser tempBrowser = new WebBrowser();
//臨時浏覽器定向到新的url
tempBrowser
tempBrowser
//為臨時浏覽器關聯NewWindow等事件
tempBrowser
tempBrowser
tempBrowser
tempBrowser
//將臨時浏覽器添加到臨時TabPage中
TabPageTemp
//將臨時TabPage添加到主窗體中
this
//使外部無法捕獲此事件
e
}
/// <summary>
/// 臨時浏覽器定向完畢
/// </summary>
/// <param name=
/// <param name=
private void tempBrowser_Navigated(object sender
{
tscbUrl
WebBrowser mybrowser = (WebBrowser)sender;
TabPage mypage=(TabPage)mybrowser
//設置臨時浏覽器所在tab標題
mypage
}
#endregion
說明
tabControl
#region//tabControl
/// <summary>
/// 切換tab
/// </summary>
/// <param name=
/// <param name=
private void tabControl
{
WebBrowser mybor = (WebBrowser)tabControl
if (mybor
{
//地址輸入框
tscbUrl
tabControl
}
else
{
tscbUrl
tabControl
}
setStatusButton();
}
/// <summary>
/// 關閉當前tab
/// </summary>
/// <param name=
/// <param name=
private void tabControl
{
//僅僅剩下一個tab時返回
if (tabControl
{
tabControl
getCurrentBrowser()
}
else
{
//先將tabControl
tabControl
WebBrowser mybor = getCurrentBrowser();
//釋放資源
mybor
mybor
this
//重新設置當前tab
tabControl
tabControl
}
}
#endregion
說明:當雙擊當前Tabpage從而關閉當前頁面時
總結
分頁浏覽器所謂
不足之處
最大的不足
用((WebBrowser)sender)
From:http://tw.wingwit.com/Article/program/net/201311/15669.html