【WinForm】WinForm中使用webBrowser1

文章目录

  • 前言
  • 一、效果
  • 二、界面设计
  • 三、代码
  • 总结


前言


一、效果

【WinForm】WinForm中使用webBrowser1_第1张图片

二、界面设计

【WinForm】WinForm中使用webBrowser1_第2张图片
【WinForm】WinForm中使用webBrowser1_第3张图片
【WinForm】WinForm中使用webBrowser1_第4张图片

  • WebBrowser:webBrowser1
  • ComboBox:comboBox1
  • MenuStrip:menuStrip1

三、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            webBrowser1.StatusTextChanged+=new EventHandler(webBrowser1_StatusTextChanged);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.baidu.com");
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)       //点回车键进入
            {
                webBrowser1.Navigate(comboBox1.Text);
                if (comboBox1.FindStringExact(comboBox1.Text) == -1)
                {
                    comboBox1.Items.Add(comboBox1.Text);
                }
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            comboBox1.Text = e.Url.AbsoluteUri;     //页面加载后地址栏显示
        }

        private void tsmiSaveAs_Click(object sender, EventArgs e)   
        {
            webBrowser1.ShowSaveAsDialog();         //另存为
        }

        private void tsmiProperty_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPropertiesDialog();      //查看网页属性
        }

        private void tsmiGoback_Click(object sender, EventArgs e)  
        {
            if (webBrowser1.CanGoBack)          //后退
                webBrowser1.GoBack();
        }

        private void tsmiGoForward_Click(object sender, EventArgs e)   
        {
            if (webBrowser1.CanGoForward)       //前进
                webBrowser1.GoForward();
        }

        private void tsmiGoto_DropDownOpening(object sender, EventArgs e)   //判断是否可以前进后退,若能则显示
        {
            tsmiGoback.Enabled = webBrowser1.CanGoBack;
            tsmiGoForward.Enabled = webBrowser1.CanGoForward;
        }

        private void tsmiGoHome_Click(object sender, EventArgs e)  
        {
            webBrowser1.GoHome();       //转到主页
        }

        private void tamiStop_Click(object sender, EventArgs e)   
        {
            webBrowser1.Stop();         //停止
        }

        private void tsmiRefresh_Click(object sender, EventArgs e) 
        {
            webBrowser1.Refresh();       //刷新
        }

        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) //下载进度时发生
        {
            if (e.CurrentProgress >= 0) //判断是否正在加载
            {
                toolStripProgressBar1.Visible = true;

                //并非所有MaximumProgress都比CurrentProgress大
                if (e.CurrentProgress >= e.MaximumProgress)
                    toolStripProgressBar1.Maximum = (int)e.CurrentProgress;
                else
                    toolStripProgressBar1.Maximum = (int)e.MaximumProgress;

                toolStripProgressBar1.Value = (int)e.CurrentProgress;

                //如果完成,则隐藏进度条
                if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
                    toolStripProgressBar1.Visible = false;
            }
            else
                toolStripProgressBar1.Visible = false;
        }

        public void webBrowser1_StatusTextChanged(object sender, EventArgs e)
        {
            this.toolStripStatusLabel1.Text = webBrowser1.StatusText;       //将网站的状态显示到状态栏上
        }
    }
}


总结

你可能感兴趣的:(#,WinForm案例,edge浏览器)