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

来源:互联网 发布:r语言编程艺术 编辑:程序博客网 时间:2024/06/05 08:18
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
原创粉丝点击