• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#使用webBrowser控件总结

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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)
        {
            
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#命名规范,控件命名规则发布时间:2022-07-13
下一篇:
Windows 7库(Library):C#快速参考发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap