首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 實作簡易 Web 瀏覽器完整原始碼

     編輯:關於C#

功能清單

  • 網址列輸入/下拉歷史
  • 前進、後退、重新整理、首頁按鈕
  • Enter 鍵導航、自動補全 https://
  • 開啟本地 HTML 檔案
  • 視窗大小調整時自動調整瀏覽器區域
  • 搜尋按鈕

核心程式碼結構


public partial class BrowserForm : Form
{
    public BrowserForm() { InitializeComponent(); }

    private void Navigate(string address)
    {
        if (string.IsNullOrWhiteSpace(address)) return;
        if (address == "about:blank") return;
        if (!address.StartsWith("https://") && !address.StartsWith("https://"))
            address = "https://" + address;

        try 
        {
            Cursor.Current = Cursors.WaitCursor;
            webBrowser1.Navigate(new Uri(address));
        }
        catch (UriFormatException) { /* 忽略無效網址 */ }
        finally { Cursor.Current = Cursors.Default; }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // 工具列圖示綁定
        toolStrip1.ImageList = imageList1;
        btnBack.ImageIndex = 0; btnForward.ImageIndex = 1;
        btnRefresh.ImageIndex = 2; btnHome.ImageIndex = 3;
    }

    private void txtUrl_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Navigate(txtUrl.Text);
            if (!txtUrl.Items.Contains(txtUrl.Text)) txtUrl.Items.Add(txtUrl.Text);
        }
    }

    private void btnBack_Click(object sender, EventArgs e) => webBrowser1.GoBack();
    private void btnForward_Click(object sender, EventArgs e) => webBrowser1.GoForward();
    private void btnRefresh_Click(object sender, EventArgs e) 
    { 
        if (webBrowser1.Url?.ToString() != "about:blank") webBrowser1.Refresh(); 
    }
    private void btnHome_Click(object sender, EventArgs e) => webBrowser1.GoHome();

    private void txtUrl_SelectedIndexChanged(object sender, EventArgs e) => Navigate(txtUrl.Text);

    private void mnuOpen_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string html = File.ReadAllText(openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));
            webBrowser1.DocumentText = html;
        }
    }

    private void mnuExit_Click(object sender, EventArgs e) => Close();

    private void BrowserForm_Resize(object sender, EventArgs e)
    {
        webBrowser1.Width = ClientSize.Width - 25;
        webBrowser1.Height = ClientSize.Height - 130;
        txtUrl.Width = ClientSize.Width - 55;
    }

    private void btnSearch_Click(object sender, EventArgs e) => webBrowser1.GoSearch();
}

關鍵技術點

  1. WebBrowser 控制項:封裝 IE ActiveX (MSHTML),提供 Navigate, GoBack, GoForward, Refresh, GoHome, GoSearch, DocumentText 等高層 API。
  2. Uri 驗證:使用 new Uri(address) 攔截格式錯誤。
  3. 編碼處理:讀取本地 HTML 指定 Encoding.GetEncoding("gb2312") (或 UTF-8) 避免亂碼。
  4. 版面自適應:Resize 事件中依客戶區大小調整控制項尺寸。

已知限制與現代化建議

  • IE 核心限制:不支援 ES6+、CSS Grid/Flexbox、WebGL 等現代標準,部分網站顯示異常。
  • 替代方案:WebView2 (Microsoft.Web.WebView2)
    - 基於 Edge (Chromium) 核心,完整支援現代 Web。
    - API 相似:CoreWebView2.Navigate(), GoBack(), ExecuteScriptAsync()。
    - NuGet 安裝 Microsoft.Web.WebView2,需目標機器安裝 WebView2 Runtime (或隨應用程式部署 Fixed Version)。

本範例適合教學、簡易內部工具、舊版相容專案;新專案**強烈建議直接採用 WebView2**。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved