• 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();
            }
        }
    }
    放下电子产品,每天进步一点点
  • 相关阅读:
    Ubuntu中的解压缩文件的方式 分类: Java 2014-07-20 22:33 1264人阅读 评论(0) 收藏
    Ubuntu下安装JDK图文解析 分类: Java 2014-07-20 21:28 4565人阅读 评论(3) 收藏
    数据结构和算法设计专题之---24点游戏(穷举法和递归法) 分类: Java 2014-07-18 21:26 789人阅读 评论(0) 收藏
    数据结构和算法设计专题之---单链表的逆序 分类: Java 2014-07-18 21:24 562人阅读 评论(1) 收藏
    Android中实现静态的默认安装和卸载应用 分类: Android 2014-07-02 22:43 5952人阅读 评论(19) 收藏
    Git的使用方法 分类: Java 2014-06-18 18:34 520人阅读 评论(0) 收藏
    J2EE学习篇之--JQuery技术详解 分类: JavaWeb 2014-06-18 11:35 1958人阅读 评论(1) 收藏
    MyEclipse6.0中使用aptana插件,添加jquery提示功能 分类: JavaWeb 2014-06-04 15:35 648人阅读 评论(0) 收藏
    Android中如何搭建一个WebServer 分类: Android 2014-06-01 14:44 8708人阅读 评论(3) 收藏
    J2EE学习篇之--Spring技术详解 分类: JavaWeb 2014-05-28 10:15 1109人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/vienna/p/4034063.html
Copyright © 2020-2023  润新知