C#使用 webBrowser 控件总结

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
          
        }
        void mouse_OnMouseActivity(object sender, MouseEventArgs e)
        {
            string str = "X:" + e.X + "  Y:" + e.Y;
            this.Text = str;
        }  
        #region  API单击
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
                [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        private void DoMouseClick(int ex, int ey)
        {
            IntPtr handle = this.webBrowser1.Handle;
            StringBuilder lpClassName = new StringBuilder(100);
            while (lpClassName.ToString() != "Internet Explorer_Server")
            {
                handle = GetWindow(handle, 5);
                GetClassName(handle, lpClassName, lpClassName.Capacity);
            }
            IntPtr lParam = (IntPtr)((ey << 0x10) | ex);
            IntPtr zero = IntPtr.Zero;
            SendMessage(handle, 0x201, zero, lParam);
            SendMessage(handle, 0x202, zero, lParam);
        }
        private void DoMouseClick(IntPtr handle, int ex, int ey)
        {
            StringBuilder lpClassName = new StringBuilder(100);
            while (lpClassName.ToString() != "Internet Explorer_Server")
            {
                handle = GetWindow(handle, 5);
                GetClassName(handle, lpClassName, lpClassName.Capacity);
            }
            IntPtr lParam = (IntPtr)((ey << 0x10) | ex);
            IntPtr zero = IntPtr.Zero;
            SendMessage(handle, 0x201, zero, lParam);
            SendMessage(handle, 0x202, zero, lParam);
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        #endregion

        int i = 0;
        private void Form4_Load(object sender, EventArgs e)
        {
            txt_uid.Text = listBox1.Items[0].ToString().Replace("----", "-").Split('-')[0];
            txt_pwd.Text = listBox1.Items[0].ToString().Replace("----", "-").Split('-')[1];
            //timer1.Interval = 20;
            //timer1.Interval = 1000;
            //timer1.Interval = 2000;
        }
        MouseHook mouse = new MouseHook();  
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
              //  i++;
                //if (i > listBox1.Items.Count)
                //{
                //    MessageBox.Show("全部已打开一次");
                //    return;
                //}
                //txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
                //txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];

                HtmlDocument cd = webBrowser1.Document;
                HtmlElement element = webBrowser1.Document.GetElementById("uin");//id或者是name
                element.InnerText = txt_uid.Text;
                element = webBrowser1.Document.GetElementById("pwd");//id或者是name
                element.InnerText = txt_pwd.Text;

                // 第一种情况butten 按钮有id或者name
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("submitBtn");
                buttonSubmit.InvokeMember("click");
            }
            catch (Exception)
            {
                
               // throw;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(new Uri("https://w.mail.qq.com/cgi-bin/loginpage?f=xhtml"));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Rectangle rt = webBrowser1.Document.Body.ScrollRectangle;
            webBrowser1.Document.Window.ScrollTo(0, rt.Height);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            i++;
            if (i > listBox1.Items.Count)
                return;
            txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
            txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            Read("1.txt");
            labcout.Text = "总共:"+listBox1.Items.Count.ToString();
            i = 0;
        }
        public void Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                listBox1.Items.Add(line.ToString());
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
           //HtmlElementCollection link = this.webBrowser1.Document.GetElementsByTagName("a");
           // for (int ii = 0; ii < link.Count; ii++)
           // {
           //     if (link[ii].GetAttribute("href").ToLower().IndexOf("http://mail.qq.com/cgi-bin/frame_html?") == 0 && link[ii].GetAttribute("href").ToLower().IndexOf("&st=0&p=") > 0)
                
           //         link[ii].InvokeMember("click");
           //     }
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
           // http://mail.qq.com/cgi-bin/frame_html?f=html&sid=9YMxbM2xOc2PCHMnUfnT5vNw
        }
        private void herfclick(string url)
        {

            for (int i = 0; i < webBrowser1.Document.All.Count; i++)
            {
                if (webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim().Length > 52)
                {
                    if (webBrowser1.Document.All[i].TagName == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim().Substring(0, 49) == url.Substring(0, 49))
                    {

                        webBrowser1.Document.All[i].InvokeMember("click");//引发”CLICK”事件

                        break;

                    }
                }
            }

        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            txt_uid.Text =listBox1.Items[listBox1.SelectedIndex].ToString().Replace("----", "-").Split('-')[0];
            txt_pwd.Text = listBox1.Items[listBox1.SelectedIndex].ToString().Replace("----", "-").Split('-')[1];
            timer1.Start();
            timer2.Start();
            timer3.Start();
            timer4.Start();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //1
            webBrowser1.Navigate(new Uri("https://w.mail.qq.com/cgi-bin/loginpage?f=xhtml"));
            Thread.Sleep(1000);
            Rectangle rt = webBrowser1.Document.Body.ScrollRectangle;
            webBrowser1.Document.Window.ScrollTo(0, rt.Height);
            //2
            try
            {
                //  i++;
                //if (i > listBox1.Items.Count)
                //{
                //    MessageBox.Show("全部已打开一次");
                //    return;
                //}
                //txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
                //txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];

                HtmlDocument cd = webBrowser1.Document;
                HtmlElement element = webBrowser1.Document.GetElementById("uin");//id或者是name
                element.InnerText = txt_uid.Text;
                element = webBrowser1.Document.GetElementById("pwd");//id或者是name
                element.InnerText = txt_pwd.Text;

                // 第一种情况butten 按钮有id或者name
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("submitBtn");
                buttonSubmit.InvokeMember("click");
            }
            catch (Exception)
            {

                // throw;
            }
            //3
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            webBrowser1.Navigate(new Uri("https://w.mail.qq.com/cgi-bin/loginpage?f=xhtml"));
            timer1.Stop();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                //  i++;
                //if (i > listBox1.Items.Count)
                //{
                //    MessageBox.Show("全部已打开一次");
                //    return;
                //}
                //txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
                //txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];

                HtmlDocument cd = webBrowser1.Document;
                HtmlElement element = webBrowser1.Document.GetElementById("uin");//id或者是name
                element.InnerText = txt_uid.Text;
                element = webBrowser1.Document.GetElementById("pwd");//id或者是name
                element.InnerText = txt_pwd.Text;

                // 第一种情况butten 按钮有id或者name
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("submitBtn");
                buttonSubmit.InvokeMember("click");
            }
            catch (Exception)
            {

                // throw;
            }
            timer2.Stop();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
//模拟点击某个链接
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
            timer3.Stop();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Start();
            timer3.Start();
        
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
//模拟点击某个链接
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
           // timer4.Stop();
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
          
        }
        void mouse_OnMouseActivity(object sender, MouseEventArgs e)
        {
            string str = "X:" + e.X + "  Y:" + e.Y;
            this.Text = str;
        }  
        #region  API单击
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
                [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        private void DoMouseClick(int ex, int ey)
        {
            IntPtr handle = this.webBrowser1.Handle;
            StringBuilder lpClassName = new StringBuilder(100);
            while (lpClassName.ToString() != "Internet Explorer_Server")
            {
                handle = GetWindow(handle, 5);
                GetClassName(handle, lpClassName, lpClassName.Capacity);
            }
            IntPtr lParam = (IntPtr)((ey << 0x10) | ex);
            IntPtr zero = IntPtr.Zero;
            SendMessage(handle, 0x201, zero, lParam);
            SendMessage(handle, 0x202, zero, lParam);
        }
        private void DoMouseClick(IntPtr handle, int ex, int ey)
        {
            StringBuilder lpClassName = new StringBuilder(100);
            while (lpClassName.ToString() != "Internet Explorer_Server")
            {
                handle = GetWindow(handle, 5);
                GetClassName(handle, lpClassName, lpClassName.Capacity);
            }
            IntPtr lParam = (IntPtr)((ey << 0x10) | ex);
            IntPtr zero = IntPtr.Zero;
            SendMessage(handle, 0x201, zero, lParam);
            SendMessage(handle, 0x202, zero, lParam);
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        #endregion

        int i = 0;
        private void Form4_Load(object sender, EventArgs e)
        {
            txt_uid.Text = listBox1.Items[0].ToString().Replace("----", "-").Split('-')[0];
            txt_pwd.Text = listBox1.Items[0].ToString().Replace("----", "-").Split('-')[1];
            //timer1.Interval = 20;
            //timer1.Interval = 1000;
            //timer1.Interval = 2000;
        }
        MouseHook mouse = new MouseHook();  
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
              //  i++;
                //if (i > listBox1.Items.Count)
                //{
                //    MessageBox.Show("全部已打开一次");
                //    return;
                //}
                //txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
                //txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];

                HtmlDocument cd = webBrowser1.Document;
                HtmlElement element = webBrowser1.Document.GetElementById("uin");//id或者是name
                element.InnerText = txt_uid.Text;
                element = webBrowser1.Document.GetElementById("pwd");//id或者是name
                element.InnerText = txt_pwd.Text;

                // 第一种情况butten 按钮有id或者name
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("submitBtn");
                buttonSubmit.InvokeMember("click");
            }
            catch (Exception)
            {
                
               // throw;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(new Uri("https://w.mail.qq.com/cgi-bin/loginpage?f=xhtml"));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Rectangle rt = webBrowser1.Document.Body.ScrollRectangle;
            webBrowser1.Document.Window.ScrollTo(0, rt.Height);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            i++;
            if (i > listBox1.Items.Count)
                return;
            txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
            txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            Read("1.txt");
            labcout.Text = "总共:"+listBox1.Items.Count.ToString();
            i = 0;
        }
        public void Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                listBox1.Items.Add(line.ToString());
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
           //HtmlElementCollection link = this.webBrowser1.Document.GetElementsByTagName("a");
           // for (int ii = 0; ii < link.Count; ii++)
           // {
           //     if (link[ii].GetAttribute("href").ToLower().IndexOf("http://mail.qq.com/cgi-bin/frame_html?") == 0 && link[ii].GetAttribute("href").ToLower().IndexOf("&st=0&p=") > 0)
                
           //         link[ii].InvokeMember("click");
           //     }
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
           // http://mail.qq.com/cgi-bin/frame_html?f=html&sid=9YMxbM2xOc2PCHMnUfnT5vNw
        }
        private void herfclick(string url)
        {

            for (int i = 0; i < webBrowser1.Document.All.Count; i++)
            {
                if (webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim().Length > 52)
                {
                    if (webBrowser1.Document.All[i].TagName == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim().Substring(0, 49) == url.Substring(0, 49))
                    {

                        webBrowser1.Document.All[i].InvokeMember("click");//引发”CLICK”事件

                        break;

                    }
                }
            }

        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            txt_uid.Text =listBox1.Items[listBox1.SelectedIndex].ToString().Replace("----", "-").Split('-')[0];
            txt_pwd.Text = listBox1.Items[listBox1.SelectedIndex].ToString().Replace("----", "-").Split('-')[1];
            timer1.Start();
            timer2.Start();
            timer3.Start();
            timer4.Start();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //1
            webBrowser1.Navigate(new Uri("https://w.mail.qq.com/cgi-bin/loginpage?f=xhtml"));
            Thread.Sleep(1000);
            Rectangle rt = webBrowser1.Document.Body.ScrollRectangle;
            webBrowser1.Document.Window.ScrollTo(0, rt.Height);
            //2
            try
            {
                //  i++;
                //if (i > listBox1.Items.Count)
                //{
                //    MessageBox.Show("全部已打开一次");
                //    return;
                //}
                //txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
                //txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];

                HtmlDocument cd = webBrowser1.Document;
                HtmlElement element = webBrowser1.Document.GetElementById("uin");//id或者是name
                element.InnerText = txt_uid.Text;
                element = webBrowser1.Document.GetElementById("pwd");//id或者是name
                element.InnerText = txt_pwd.Text;

                // 第一种情况butten 按钮有id或者name
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("submitBtn");
                buttonSubmit.InvokeMember("click");
            }
            catch (Exception)
            {

                // throw;
            }
            //3
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            webBrowser1.Navigate(new Uri("https://w.mail.qq.com/cgi-bin/loginpage?f=xhtml"));
            timer1.Stop();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                //  i++;
                //if (i > listBox1.Items.Count)
                //{
                //    MessageBox.Show("全部已打开一次");
                //    return;
                //}
                //txt_uid.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[0];
                //txt_pwd.Text = listBox1.Items[i].ToString().Replace("----", "-").Split('-')[1];

                HtmlDocument cd = webBrowser1.Document;
                HtmlElement element = webBrowser1.Document.GetElementById("uin");//id或者是name
                element.InnerText = txt_uid.Text;
                element = webBrowser1.Document.GetElementById("pwd");//id或者是name
                element.InnerText = txt_pwd.Text;

                // 第一种情况butten 按钮有id或者name
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("submitBtn");
                buttonSubmit.InvokeMember("click");
            }
            catch (Exception)
            {

                // throw;
            }
            timer2.Stop();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
//模拟点击某个链接
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
            timer3.Stop();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Start();
            timer3.Start();
        
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
//模拟点击某个链接
            herfclick("http://mail.qq.com/cgi-bin/frame_html?f=html&sid=rxOuQ-CEBfe1KCx4Uc-T5vNw");
           // timer4.Stop();
        }
    }
}
放下电子产品,每天进步一点点
原文地址:https://www.cnblogs.com/vienna/p/4034063.html