none
关于 WebBrowser 控件的 问题 RRS feed

  • 问题

  • 我是 新手 求教 一个 问题

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


    namespace IE
    {
        public partial class Form1 : Form
        {
        
            public Form1()
            {
              
                InitializeComponent();
                SetTempPage();
               
            }

            private void goB_Click(object sender, EventArgs e)
            {   if(cmb.Text.Length>0)
                Connection(cmb.Text);
            }

            private void SetTempPage()
            {
            
                var tp = new TabPage();
                tp.Text = "Blank Page";
                var wb = new WebBrowser();
                wb.Dock = DockStyle.Fill;
                wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
                wb.NewWindow += new CancelEventHandler(wb_NewWindow);
                wb.Navigating += new WebBrowserNavigatingEventHandler(wb_Navigating);
                wb.ProgressChanged += new WebBrowserProgressChangedEventHandler(wb_ProgressChanged);
                wb.Navigated += new WebBrowserNavigatedEventHandler(wb_Navigated);
                tp.Controls.Add(wb);
                tc.Controls.Add(tp);
                tc.SelectedTab = tp;
            }

            void wb_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
                var tp = tc.SelectedTab;
                var title = ((WebBrowser)tp.Controls[0]).DocumentTitle;
                if (title.Length > 9)
                {
                    tp.Text = title.Substring(0, 5);
                }
                else
                {
                    tp.Text = title;
                }
                this.Text = title;
                cmb.Text = ((WebBrowser)tp.Controls[0]).Url.ToString();
            }

            void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
            {
                prb.Visible = true;
                prb.Maximum = (int)e.MaximumProgress;
                prb.Value = (int)e.CurrentProgress;
                if (prb.Value == (int)e.MaximumProgress)
                    prb.Visible = false;
            }

            void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
            {
             
            }

            void wb_NewWindow(object sender, CancelEventArgs e)
            {
                SetTempPage();
                Connection(((WebBrowser)sender).StatusText);
                e.Cancel = true;
            }

             void wb_StatusTextChanged(object sender, EventArgs e)
            {
                var tp = tc.SelectedTab;
                 var wb=(WebBrowser)tp.Controls[0];
                // if (((WebBrowser)tp.Controls[0]).DocumentTitle.Length > 9)
                // {
                //     tp.Text = ((WebBrowser)tp.Controls[0]).DocumentTitle.Substring(0, 5);
                // }
                // else
                // {
                //     tp.Text = ((WebBrowser)tp.Controls[0]).DocumentTitle.Substring(0, 5);
                // }
                               
                   
                //tslab.Text = GetCurrentPage().StatusText;
                //this.Text = ((WebBrowser)tp.Controls[0]).DocumentTitle;
              
            }
            private WebBrowser GetCurrentPage()
            {
                if (tc == null)
                    return null;
                var tp = tc.SelectedTab;
                var wb = (WebBrowser)tp.Controls[0];
                return wb;
            }

           public  void Connection(string str_Uri)
            {
                try
                {
                   
                    GetCurrentPage().Navigate(GetUri(str_Uri));
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message, "Error Information:");
                }
            }
            private static Uri GetUri(string str_Uri)
            {
                if (String.IsNullOrEmpty(str_Uri.Trim()))
                    return null;
                if (str_Uri.StartsWith("http://") || str_Uri.StartsWith("https://") || str_Uri.StartsWith("ftp://"))
                    return new Uri(str_Uri);
                return new Uri("http://" + str_Uri);
            }

            private void tc_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    this.Text = GetCurrentPage().DocumentTitle;
                    cmb.Text = GetCurrentPage().Url.ToString();
                }
                catch
                {

                    this.Text = "Browser";
                    cmb.Text = "about:Blank";
                }
              
            }

            private void hts_Click(object sender, EventArgs e)
            {
                GetCurrentPage().GoHome();
            }

            private void sts_Click(object sender, EventArgs e)
            {
                GetCurrentPage().Stop();
            }

            private void seh_Click(object sender, EventArgs e)
            {
                GetCurrentPage().GoSearch();
            }

            private void rfh_Click(object sender, EventArgs e)
            {
                GetCurrentPage().Refresh();
            }

            private void fwd_Click(object sender, EventArgs e)
            {
                if (!GetCurrentPage().CanGoBack)
                    return;
                GetCurrentPage().GoForward();

            }

            private void gbk_Click(object sender, EventArgs e)
            {
                if (!GetCurrentPage().CanGoBack)
                    return;
                GetCurrentPage().GoBack();
            }

            private void newPageToolStripMenuItem_Click(object sender, EventArgs e)
            {
                SetTempPage();
            }

            private void closeThisPageToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (tc.TabCount == 1)
                    return;
                var tp = tc.SelectedTab;
                tc.Visible = false;
               
                GetCurrentPage().Controls.Clear();
                GetCurrentPage().Dispose();
                tc.TabPages.Remove(tp);
                tc.SelectedIndex = tc.TabCount - 1;
                tc.Visible = true;
            }

            private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
                var  o = new OpenFileDialog();
                o.Filter = "All Files(*.*)|*.*";
                try
                {
                    if (o.ShowDialog() == DialogResult.OK)
                        GetCurrentPage().Navigate(o.FileName);
                }
                catch
                {
                    MessageBox.Show("No Support this File!","Information");
                }
            }

            private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
                GetCurrentPage().ShowSaveAsDialog();
            }

            private void printToolStripMenuItem_Click(object sender, EventArgs e)
            {
                GetCurrentPage().ShowPrintDialog();
            }

            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

     

            private void toolStripButton1_ButtonClick(object sender, EventArgs e)
            {
                var exists = false;
                if (String.IsNullOrEmpty(cmb.Text.Trim()))
                    return;
                if (cmb.Text == "about:Blank")
                    return;
                string filePath=Environment.CurrentDirectory+"\\"+"url.xml";
                if (!System.IO.File.Exists(filePath))
                {
                    var doc = new XElement("url", "");
                    doc.Save(filePath);
                }
             
                var xdo =   XDocument.Load(filePath);
                if (GetUri(cmb.Text) == null)
                    return;
                try
                {
                    var link = xdo.Descendants("url").Select(l => l.Elements("link"));
                    foreach (var r in link)
                    {
                        foreach (var i in r.Select(v => v.Value))
                        {
                            if (i == GetUri(cmb.Text).ToString())
                                exists = true;
                        }
                    }
                }
                catch { }
                finally {
                    if (!exists)
                    {
                        var doc = XElement.Load(filePath);
                        doc.AddFirst(new XElement("link", GetUri(cmb.Text).ToString()));
                        doc.Save(filePath);
                    }
                }
              

            }

            private void showToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Favorite f = new Favorite();
                f.Show();
                f.GetMethod(Link);
            }
            private void Link(string address)
            {
                GetCurrentPage().Navigate(address);
            }

            private void longinToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Process.Start(@"d:\My Documents\Visual Studio 2008\Projects\QQ\QQ\bin\Debug\QQ.exe");
            }


        }
    }

     

     

     

     

     

    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.Xml.Linq;

    namespace IE
    {
        public partial class Favorite : Form
        {
           public  delegate void Link(string address);
            Link l = null;
            public Favorite()
            {
            
                InitializeComponent();
                try
                {
                    var doc = XElement.Load(Environment.CurrentDirectory + "\\" + "url.xml");
                    var r = from u in doc.Descendants("link")
                            select u;
                    foreach (var v in r)
                        listBox1.Items.Add(v.Value);
                }
                catch
                {
                    this.Close();
                }
            }

            private void listBox1_DoubleClick(object sender, EventArgs e)
            {
                if (l == null)
                    return;
                l(listBox1.SelectedItem.ToString());
                this.Close();
            }
            public void GetMethod(Link l)
            {
                if (l == null)
                    return;
                this.l = new Link(l);
     
            }

        }
    }

    可是 在  好123  网站 那个 百度 搜索框 里 进行 搜索 时 老是会 报错   

      

     还有  当  WebBrowser,StatusText 不是 为一个  标准的 Uri 时  新建 窗口 连接 就 出错

      请  大家  指教 下 谢谢

    2007年12月16日 17:29

答案