c#使用实例之我的c#浏览器

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;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string oldtitle;
        private Boolean isHasShow=false;

        public Form1()
        {
            InitializeComponent();
            //窗体居中
            //this.StartPosition = FormStartPosition.CenterScreen;
             this.Width = 1024;
             this.Height = 600;

             //调用Resources里面的国际化资源
             this.Text = Properties.Resources.title;

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            webBrowser1.Navigate(textBox1.Text);
            
           // MessageBox.Show("ok");
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            textBox1.Text = "http://www.baidu.com/";
            webBrowser1.Navigate(textBox1.Text);
            isHasShow = true;          
            Debug.WriteLine("浏览器版本:" + webBrowser1.Version.ToString());
            this.Text = this.Text + ",浏览器版本:" + webBrowser1.Version.ToString();
            this.oldtitle = this.Text;
           
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                webBrowser1.Navigate(textBox1.Text);
            }
        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            //禁止调用外部浏览器打开网页,使用本控件打开
            try
            {

                string newurl = webBrowser1.Document.ActiveElement.GetAttribute("href");
                if (!newurl.Contains("://"))
                {

                    newurl = "http://" + newurl;
                }
                this.webBrowser1.Url = new System.Uri(newurl, System.UriKind.Absolute);
                e.Cancel = true;
               
            }
            catch (System.Exception ex)
            {

            }

        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {

            if (isHasShow)
            {
                //加载中
                this.Text = this.oldtitle + ":正在加载中...";
            }
       
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

            //在加载完成后,获取新打开窗体的url
            this.textBox1.Text = e.Url.ToString();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            if (isHasShow)
            {

                this.Text = this.oldtitle + ":加载完成...";
            }
            //不用弹出新窗口
            foreach (HtmlElement archor in this.webBrowser1.Document.Links)
            {
                archor.SetAttribute("target", "_self");
            }
            

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            //窗体居中
            Rectangle rect=Screen.GetWorkingArea(this);   
            Debug.WriteLine(rect.ToString());
            this.Location = new Point((rect.Width - this.Width) / 2, (rect.Height - this.Height) / 2);
        }


        private void button1_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

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

        private void 上一页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.GoForward();
        }

        private void 后退一页ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            webBrowser1.GoBack();
        }

        private void 主页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.GoHome();
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }
    }
}

源码:http://pan.baidu.com/s/1i3gjvRf


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值