熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

使用VS2005打造簡單分頁浏覽器

2022-06-13   來源: .NET編程 

  引言

  很早就有搞一個浏覽器的想法了在vs上就試圖做過苦於經常會有這種情況出現當自治的浏覽器遇到彈出窗口時無法捕獲新的彈出窗口於是乎新的彈出窗口仍舊用ie(或其他系統默認浏覽器)打開在研究vs的WebBrowser控件時發現有NewWindow事件於是乎興奮不已決定用這個分頁浏覽器體驗一下vs

  實現功能

  預覽圖如下


 

  當前浏覽器的另存為打印打印御覽頁面設置刷新前進後退等等幾乎都是控件封裝好了的沒有幾句代碼

  浏覽器的分頁功能當浏覽器有NewWindow激發時產生新的一頁主要依靠NewWindow事件

  當前頁面的狀態例如標題狀態欄等

  實現過程以及關鍵點

  新建一個vs的windows applaction項目

  界面

  一個MenuStrip實現最上面的菜單

  兩個ToolStrip分別是工具欄和地址欄

  一個TabControl也就是浏覽器的主體了它的每個TabPage就是每一個分頁了

  一個StatusStrip也就是狀態欄了

  另外為了使窗體大小變化時控件也隨著變化注意使用控件的dock屬性

  搭成如下界面

 

  順便說一句和vs不同的是vs把例如

thistoolStripMenuItem = new SystemWindowsFormsToolStripMenuItem();
thissaveasToolStripMenuItem = new SystemWindowsFormsToolStripMenuItem();
thistoolStripSeparator = new SystemWindowsFormsToolStripSeparator();
thisprintToolStripMenuItem = new SystemWindowsFormsToolStripMenuItem();
thisprintPreToolStripMenuItem = new SystemWindowsFormsToolStripMenuItem();
thistoolStripSeparator = new SystemWindowsFormsToolStripSeparator();
thisexitToolStripMenuItem = new SystemWindowsFormsToolStripMenuItem();

  這些通過我們拖拽控件系統生成的界面代碼分離出來統一放在叫*Designercs文件裡了詳見事例代碼中的FormDesignercs文件

  輔助方法

#region //輔助方法
/// <summary>
/// 當在浏覽器地址欄敲回車時當前浏覽器重定向到指定url(tscbUrlTex)
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void tscbUrl_KeyDown(object sender KeyEventArgs e)
{
 if (eKeyCode == KeysEnter)
 {
  newCurrentPageUrl(tscbUrlText);
 }
}
/// <summary>
/// 新建空白頁
/// </summary>
private void newPage()
{
 tscbUrlText = about:blank;
 TabPage mypage = new TabPage();
 WebBrowser tempBrowser = new WebBrowser();
 tempBrowserNavigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
 tempBrowserNewWindow += new CancelEventHandler(tempBrowser_NewWindow);

 tempBrowserProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
 tempBrowserStatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
 tempBrowserDock = DockStyleFill;
 mypageControlsAdd(tempBrowser);

 tabControlTabPagesAdd(mypage);
 tabControlSelectedTab = mypage;
}
/// <summary>
/// 臨時浏覽器進度變化事件
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
void tempBrowser_ProgressChanged(object sender WebBrowserProgressChangedEventArgs e)
{
 toolStripProgressBarMaximum = (int)eMaximumProgress;
 toolStripProgressBarValue = (int)eCurrentProgress;
}
/// <summary>
/// 新建一頁並定向到指定url
/// </summary>
/// <param name=address>新一頁的浏覽器重新定向到的url</param>
private void newPage(string address)
{
 TabPage mypage = new TabPage();
 WebBrowser tempBrowser = new WebBrowser();
 tempBrowserNavigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
 tempBrowserNewWindow += new CancelEventHandler(tempBrowser_NewWindow);
 tempBrowserStatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
 tempBrowserProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
 tempBrowserUrl = getUrl(address);
 tempBrowserDock = DockStyleFill;
 mypageControlsAdd(tempBrowser);
 tabControlTabPagesAdd(mypage);
}
/// <summary>
/// 獲取當前浏覽器
/// </summary>
/// <returns>當前浏覽器</returns>
private WebBrowser getCurrentBrowser()
{
 WebBrowser currentBrowser = (WebBrowser)tabControlSelectedTabControls[];
 return currentBrowser;
}
/// <summary>
/// 處理字符串為合法url
/// </summary>
/// <param name=address></param>
/// <returns></returns>
private Uri getUrl(string address)
{
 string tempaddress = address;
 if ((!addressStartsWith(http://)) && (!addressStartsWith(https://)) && (!addressStartsWith(ftp://)))
 {
  tempaddress = http:// + address;
 }
 Uri myurl;
 try
 {
  myurl = new Uri(tempaddress);
 }
 catch
 {
  myurl = new Uri(about:blank);
 }
 return myurl;
}
/// <summary>
/// 截取字符串為指定長度
/// </summary>
/// <param name=oldstring></param>
/// <returns></returns>
private string newstring(string oldstring)
{
 string temp;
 if (oldstringLength < TITLE_COUNT)
 {
  temp = oldstring;
 }
 else
 {
  temp = oldstringSubstring( TITLE_COUNT);
 }
 return temp;
}
/// <summary>
/// 設置"前進""後退"button的可用狀態
/// </summary>
private void setStatusButton()
{
 backButtonEnabled = getCurrentBrowser()CanGoBack;
 forwordButtonEnabled = getCurrentBrowser()CanGoForward;
}
#endregion

  說明其中getCurrentBrowser()是獲取當前頁面的浏覽器這裡把它叫當前浏覽器即getCurrentBrowser()為獲取當前浏覽器

  菜單欄

#region//菜單欄
/// <summary>
/// 另存為
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void saveasToolStripMenuItem_Click(object sender EventArgs e)
{
 getCurrentBrowser()ShowSaveAsDialog();
}
/// <summary>
/// 打印
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void printToolStripMenuItem_Click(object sender EventArgs e)
{
 getCurrentBrowser()ShowPrintDialog();
}
/// <summary>
/// 打印御覽
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void printPreToolStripMenuItem_Click(object sender EventArgs e)
{
 getCurrentBrowser()ShowPrintPreviewDialog();
}
/// <summary>
/// 關閉浏覽器
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void exitToolStripMenuItem_Click(object sender EventArgs e)
{
 ApplicationExit();
}
/// <summary>
/// 頁面設置
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void pageSetupToolStripMenuItem_Click(object sender EventArgs e)
{
 getCurrentBrowser()ShowPageSetupDialog();
}
/// <summary>
/// 屬性設置
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void propeToolStripMenuItem_Click(object sender EventArgs e)
{
 getCurrentBrowser()ShowPropertiesDialog();
}
#region//關於
private void aboutToolStripMenuItem_Click(object sender EventArgs e)
{
 AboutBox myabout = new AboutBox();
 myaboutShow();
}

private void tipToolStripMenuItem_Click(object sender EventArgs e)
{
 MessageBoxShow(小提示雙擊分頁標題即可關閉當前頁面);
}
#endregion
#endregion

  說明其中文件菜單的功能大都是WebBrowser控件封裝好的僅僅是用上文提到的getCurrentBrowser()獲取一下當前浏覽器罷了

  工具欄

#region//工具欄
/// <summary>
/// 後退
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void backButton_Click(object sender EventArgs e)
{
 getCurrentBrowser()GoBack();
 setStatusButton();
}
/// <summary>
/// 前進
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void forwordButton_Click(object sender EventArgs e)
{
 getCurrentBrowser()GoForward();
 setStatusButton();
}
/// <summary>
/// 停止
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void stopButton_Click(object sender EventArgs e)
{
 getCurrentBrowser()Stop();
}
/// <summary>
/// 刷新
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void refreshButton_Click(object sender EventArgs e)
{
getCurrentBrowser()Refresh();
}
/// <summary>
/// 定向到主頁
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void homeButton_Click(object sender EventArgs e)
{
getCurrentBrowser()GoHome();
}
/// <summary>
/// 搜索
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void searchButton_Click(object sender EventArgs e)
{
 getCurrentBrowser()GoSearch();
}
/// <summary>
/// 打印
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void printButton_Click(object sender EventArgs e)
{
 getCurrentBrowser()Print();
}
/// <summary>
/// 新建空白頁
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void newButton_Click(object sender EventArgs e)
{
 newPage();
}
/// <summary>
/// 使當前的浏覽器定位到給定url
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void gotoButton_Click(object sender EventArgs e)
{
 newCurrentPageUrl(tscbUrlText);
}
#endregion

  說明和菜單欄實現的功能類似也是一些簡單的調用僅僅是表現形式不同
  
  初始化

#region//初始化
/// <summary>
/// 初始化
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void Form_Load(object sender EventArgs e)
{
 initMainForm();
}
/// <summary>
/// 初始化浏覽器
/// </summary>
private void initMainForm()
{
 TabPage mypage = new TabPage();
 WebBrowser tempBrowser = new WebBrowser();
 tempBrowserNavigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
 tempBrowserNewWindow += new CancelEventHandler(tempBrowser_NewWindow);
 tempBrowserStatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
 tempBrowserProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
 tempBrowserDock = DockStyleFill;
 tempBrowserGoHome();//和新建空白頁不同
 mypageControlsAdd(tempBrowser);
 tabControlTabPagesAdd(mypage);
}

#endregion
 

  說明分頁浏覽器初始化時要定向到主頁雖然我們的浏覽器暫時沒有提供設置主頁的功能

  臨時浏覽器事件

#region//臨時浏覽器事件
/// <summary>
/// 臨時浏覽器狀態變化事件
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
void tempBrowser_StatusTextChanged(object sender EventArgs e)
{
 WebBrowser myBrowser = (WebBrowser)sender;
 if (myBrowser != getCurrentBrowser())
 {
  return;
 }
 else
 {
  toolStripStatusLabelText = myBrowserStatusText;
 }
}
/// <summary>
/// 在當前頁面上重新定向
/// </summary>
/// <param name=address>url</param>
private void newCurrentPageUrl(String address)
{
 getCurrentBrowser()Navigate(getUrl(address));
}
/// <summary>
/// 臨時浏覽器產生新窗體事件
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
void tempBrowser_NewWindow(object sender CancelEventArgs e)
{
 //獲取觸發tempBrowser_NewWindow事件的浏覽器
 WebBrowser myBrowser = (WebBrowser)sender;
 //獲取觸發tempBrowser_NewWindow事件的浏覽器所在TabPage
 TabPage mypage = (TabPage)myBrowserParent;
 //通過StatusText屬性獲得新的url
 string NewURL = ((WebBrowser)sender)StatusText;
 //生成新的一頁
 TabPage TabPageTemp = new TabPage();
 //生成新的tempBrowser
 WebBrowser tempBrowser = new WebBrowser();
 //臨時浏覽器定向到新的url
 tempBrowserNavigate(NewURL);
 tempBrowserDock = DockStyleFill;
 //為臨時浏覽器關聯NewWindow等事件
 tempBrowserNewWindow += new CancelEventHandler(tempBrowser_NewWindow);
 tempBrowserNavigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
 tempBrowserProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
 tempBrowserStatusTextChanged+=new EventHandler(tempBrowser_StatusTextChanged);
 //將臨時浏覽器添加到臨時TabPage中
 TabPageTempControlsAdd(tempBrowser);
 //將臨時TabPage添加到主窗體中
 thistabControlTabPagesAdd(TabPageTemp);
 //使外部無法捕獲此事件
 eCancel = true;
}
/// <summary>
/// 臨時浏覽器定向完畢
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void tempBrowser_Navigated(object sender WebBrowserNavigatedEventArgs e)
{
 tscbUrlText = getCurrentBrowser()UrlToString();
 WebBrowser mybrowser = (WebBrowser)sender;
 TabPage mypage=(TabPage)mybrowserParent;
 //設置臨時浏覽器所在tab標題
 mypageText= newstring(mybrowserDocumentTitle);
}
#endregion

  說明臨時浏覽器實際上是用程序的方式先new出一個tempBrowser然後添加到一個分頁中去其中這個tempBrowser我稱它為臨時浏覽器其中void tempBrowser_NewWindow(object sender CancelEventArgs e){}事件是比較重要的我認為它是整個程序的核心部分

  tabControl事件

#region//tabControl事件
/// <summary>
/// 切換tab
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void tabControl_SelectedIndexChanged(object sender EventArgs e)
{
 WebBrowser mybor = (WebBrowser)tabControlSelectedTabControls[];
 if (myborUrl != null)
 {
  //地址輸入框
  tscbUrlText = myborUrlToString();
  tabControlSelectedTabText = newstring(myborDocumentTitle);
 }
 else
 {
  tscbUrlText = about:blank;
  tabControlSelectedTabText = 空白頁;
 }
 setStatusButton();
}
/// <summary>
/// 關閉當前tab
/// </summary>
/// <param name=sender></param>
/// <param name=e></param>
private void tabControl_DoubleClick(object sender EventArgs e)
{
 //僅僅剩下一個tab時返回
 if (tabControlTabPagesCount <= )
 {
  tabControlSelectedTabText = 空白頁;
  getCurrentBrowser()Navigate(about:blank);
 }
 else
 {
  //先將tabControl隱藏然後remove掉目標tab(如果不隱藏則出現閃爍即系統自動調轉到tabControl的第一個tab然後跳會)最後顯示tabControl

  tabControlVisible = false;
  WebBrowser mybor = getCurrentBrowser();
  //釋放資源
  myborDispose();
  myborControlsClear();
  thistabControlTabPagesRemove(thistabControlSelectedTab);
  //重新設置當前tab
  tabControlSelectedTab = tabControlTabPages[tabControlTabPagesCount ];
  tabControlVisible = true;
 }
}
#endregion

  說明:當雙擊當前Tabpage從而關閉當前頁面時tabControl 會首先定位到第一個tab然後再定位到指定的Tabpage上我采取隱藏tabControl處理顯示tabControl思路解決此問題

  總結

  分頁浏覽器所謂分頁從實現上講就是控件的動態添加當前浏覽器產生新窗體時先new出一個TabPage再new一個WebBrowser把這個WebBrowser加載了一些事件以後添加到先前的這個TabPage上然後把這個TabPage添加到主窗體tabControl

  不足之處

  最大的不足

  用((WebBrowser)sender)StatusText無法捕獲的url(例如StatusText 為javascript:void())目前無法解決這也是某些莫名其妙的問題的出處另外某些腳本不支持其他一些罪狀讓我們共同羅列…


From:http://tw.wingwit.com/Article/program/net/201311/15669.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.