看範例學C#-19 自製簡易IE瀏覽器

  • 13565
  • 0
  • C#
  • 2011-10-16

看範例學C#-19 自製簡易IE瀏覽器

今天的範例是做一個簡易IE瀏覽器,用了畫面上一個Webbroser(用來顯示網頁畫面),
六個button(按鈕),一個textbox,一個label
底下用了一個ToolTip 用來顯示狀態列,底下左下角那個棒狀物是ProgessBar

1_thumb
下面程式碼是webBrowser1用到的事件,目得是避免網頁會自動開新分頁,這樣會變得由ie開啟,所以我們一般都要把這種自動開新分頁
給擋下來,轉由我們程式內的webBrowser1去顯示

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach (HtmlElement archor in this.webBrowser1.Document.Links)
            {
                archor.SetAttribute("target", "_self");
            }
            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {       // 把目前的網址顯示在網址輸入框中
                textBox1.Text = webBrowser1.Url.ToString();
            }
        }
        private void webBrowser1_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

以下為完整原始碼


using System;
using System.Windows.Forms;
namespace ex06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // 預設載入的頁面
            webBrowser1.Navigate("http://tw.yahoo.com");
        }
        private void backBtn_Click(object sender, EventArgs e)
        {
            // 上一頁
            webBrowser1.GoBack();
        }
        private void forwardBtn_Click(object sender, EventArgs e)
        {
            // 下一頁
            webBrowser1.GoForward();
        }
        private void refreshBtn_Click(object sender, EventArgs e)
        {
            // 重新整理
            webBrowser1.Refresh();
        }
        private void stopBtn_Click(object sender, EventArgs e)
        {
            // 停止
            webBrowser1.Stop();
        }
        private void homeBtn_Click(object sender, EventArgs e)
        {
            // 首頁
            webBrowser1.GoHome();
        }
        private void goBtn_Click(object sender, EventArgs e)
        {
            // 前往網址輸入框中的網址 
            textBox1.Text = textBox1.Text.Trim();
            bool chkdisk = false;//防止進入磁碟機
            if (textBox1.Text.Length >= 0)
            {
                if (textBox1.Text.Length >= 0)
                {
                    if (textBox1.Text.Length >= 4 && textBox1.Text.ToString().Substring(0, 4).ToUpper() == "FILE")
                    {
                        textBox1.Text = "";
                        chkdisk = true;
                    }
                    for (int i = 0; i < 26; i++)
                    {
                        if (textBox1.Text.Length >= 2 && textBox1.Text.ToString().Substring(0, 2).ToUpper() == Convert.ToChar(65 + i).ToString() + ":")
                        {
                            textBox1.Text = "";
                            chkdisk = true;
                            break;
                        }
                    }
                }
                if (!chkdisk)
                    webBrowser1.Navigate(textBox1.Text);
            }
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach (HtmlElement archor in this.webBrowser1.Document.Links)
            {
                archor.SetAttribute("target", "_self");
            }
            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {       // 把目前的網址顯示在網址輸入框中
                textBox1.Text = webBrowser1.Url.ToString();
            }
        }
        private void webBrowser1_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }
        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
            toolStripProgressBar1.Value = Convert.ToInt32(e.CurrentProgress);
            toolStripLabel1.Text = webBrowser1.StatusText;
        }
    }
}

0_thumb[1]

ex19.rar


如有錯誤 歡迎指正