| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
VS2005打造简单分页浏览器
作者:佚名    文章来源:网络    点击数:    更新时间:2006-12-1
 



  辅助方法

#region //辅助方法
/// <summary>
/// 当在浏览器地址栏敲"回车"时当前浏览器重定向到指定url(tscbUrl.Tex)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tscbUrl_KeyDown(object sender, KeyEventArgs e)
{
 if (e.KeyCode == Keys.Enter)
 {
  newCurrentPageUrl(tscbUrl.Text);
 }
}
/// <summary>
/// 新建空白页
/// </summary>
private void newPage()
{
 tscbUrl.Text = "about:blank";
 TabPage mypage = new TabPage();
 WebBrowser tempBrowser = new WebBrowser();
 tempBrowser.Navigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
 tempBrowser.NewWindow += new CancelEventHandler(tempBrowser_NewWindow);

 tempBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
 tempBrowser.StatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
 tempBrowser.Dock = DockStyle.Fill;
 mypage.Controls.Add(tempBrowser);

 tabControl1.TabPages.Add(mypage);
 tabControl1.SelectedTab = mypage;
}
/// <summary>
/// 临时浏览器进度变化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void tempBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
 toolStripProgressBar1.Maximum = (int)e.MaximumProgress;
 toolStripProgressBar1.Value = (int)e.CurrentProgress;
}
/// <summary>
/// 新建一页并定向到指定url
/// </summary>
/// <param name="address">新一页的浏览器重新定向到的url</param>
private void newPage(string address)
{
 TabPage mypage = new TabPage();
 WebBrowser tempBrowser = new WebBrowser();
 tempBrowser.Navigated += new WebBrowserNavigatedEventHandler(tempBrowser_Navigated);
 tempBrowser.NewWindow += new CancelEventHandler(tempBrowser_NewWindow);
 tempBrowser.StatusTextChanged += new EventHandler(tempBrowser_StatusTextChanged);
 tempBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(tempBrowser_ProgressChanged);
 tempBrowser.Url = getUrl(address);
 tempBrowser.Dock = DockStyle.Fill;
 mypage.Controls.Add(tempBrowser);
 tabControl1.TabPages.Add(mypage);
}
/// <summary>
/// 获取当前浏览器
/// </summary>
/// <returns>当前浏览器</returns>
private WebBrowser getCurrentBrowser()
{
 WebBrowser currentBrowser = (WebBrowser)tabControl1.SelectedTab.Controls[0];
 return currentBrowser;
}
/// <summary>
/// 处理字符串为合法url
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
private Uri getUrl(string address)
{
 string tempaddress = address;
 if ((!address.StartsWith("http://")) && (!address.StartsWith("https://")) && (!address.StartsWith("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 (oldstring.Length < TITLE_COUNT)
 {
  temp = oldstring;
 }
 else
 {
  temp = oldstring.Substring(0, TITLE_COUNT);
 }
 return temp;
}
/// <summary>
/// 设置"前进","后退"button的可用状态
/// </summary>
private void setStatusButton()
{
 backButton.Enabled = getCurrentBrowser().CanGoBack;
 forwordButton.Enabled = getCurrentBrowser().CanGoForward;
}
#endregion


  说明:其中getCurrentBrowser()是获取当前页面的浏览器,这里把它叫当前浏览器,即getCurrentBrowser()为获取当前浏览器。

上一页  [1] [2] [3] [4] 下一页


  • 上一篇文章:

  • 下一篇文章: 没有了
  • 相关文章