C#QQ号批量注册(二)

 

第二部:编写主窗体代码:

要实现自动保存,自动打开QQ注册网页,自动填写表单等功能:

如图:

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 mshtml;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace QQ号批量注册
{
    public partial class Form1 : Form
    {
        public const int INTERNET_CONNECTION_MODEM = 1;
        public const int INTERNET_CONNECTION_LAN = 2;
        [System.Runtime.InteropServices.DllImport("wininet.dll")]
        public static extern bool InternetGetConnectedState(ref  int dwFlag, int dwReserved); 
        public Form1()
        {
            InitializeComponent();
        }
        #region QQ申请页面资料提交处理
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
            {
                for (int i = 1; i < 1; i++)
                {
                    if (webBrowser1.ReadyState== WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                    {
                        break;
                    }
                }
            }

            try
            {
                if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                {
                    webBrowser1.Document.GetElementById("nick").SetAttribute("value", "minot");
                    webBrowser1.Document.GetElementById("password").SetAttribute("value", "minotmin");
                    webBrowser1.Document.GetElementById("password").Focus();
                    webBrowser1.Document.GetElementById("password_again").SetAttribute("value", "minotmin");
                    webBrowser1.Document.GetElementById("year_value").Focus();
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{DOWN}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{DOWN}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{ENTER}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    webBrowser1.Document.GetElementById("month_value").Focus();
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{DOWN}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{DOWN}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{ENTER}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    webBrowser1.Document.GetElementById("day_value").Focus();
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{DOWN}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{DOWN}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                    SendKeys.SendWait("{ENTER}");
                    if (webBrowser1.ReadyState != WebBrowserReadyState.Complete && webBrowser1.IsBusy == true)
                    {
                        for (int i = 1; i < 1; i++)
                        {
                            if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
        #region 窗体加载
        private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Url=new Uri("http://zc.qq.com/chs/index.html");
        }
        #endregion
        #region 注册按钮,提交注册
        private void btnRegister_Click(object sender, EventArgs e)
        {
            register();
        }
        public void register()
        {

            System.Int32 dwFlag = new int();
            do{
                string register = webBrowser1.Document.Body.OuterHtml;
                if (register.Contains("我已阅读并同意相关服务条款"))
                {
                    webBrowser1.Document.GetElementById("submit").InvokeMember("click");
                }
                do
                {
                    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && webBrowser1.IsBusy == false)
                    {
                        string htmlcode = webBrowser1.Document.Body.OuterHtml;
                        if (htmlcode.Contains("申请成功"))
                        {
                            this.qqNumber.Text = this.qqNumber.Text + "\n" + "QQ号:" + webBrowser1.Document.GetElementById("newUin").InnerText
                            + "注册时间" + DateTime.Now.ToString();//显示QQ号
                            saveFile();//保存到本地
                        }
                        else
                        {
                           // ADSLAgain();
                            MessageBox.Show("重新连接!");
                        }
                        break;
                    }
                } while (2 > 1);
            } while ((dwFlag & INTERNET_CONNECTION_MODEM) != 0);
            //this.webBrowser1.Url = new Uri("http://zc.qq.com/chs/index.html");
        }
        #endregion
        #region 只在当前窗口中打开,禁止ie打开
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            this.webBrowser1.Url = new Uri(((WebBrowser)sender).StatusText);
            e.Cancel = true;
        }
        #endregion
        #region QQ号的保存操作
        private void btnSave_Click(object sender, EventArgs e)
        {
            saveFile();
        }
        public void saveFile()
        {
            string filename = Application.StartupPath + @"\qq.txt";
            try
            {

                if (!File.Exists(filename))//判断文件是否存在
                {
                    FileStream fs1 = new FileStream(filename, FileMode.Create, FileAccess.Write);//创建写入文件
                    fs1.Close();
                }
                else
                {
                    ;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            try
            {
                StreamWriter writer = new StreamWriter(filename, true);
                //writer.WriteLine(qqNumber.Text + "\n");
                writer.WriteLine("\n" + "QQ号:" + webBrowser1.Document.GetElementById("newUin").InnerText
                + "注册时间:" + DateTime.Now.ToString() + "\n");
                writer.Close();
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
        #region 宽带重新连接
        private void btnADSLConnectAgain_Click(object sender, EventArgs e)     
        {
            ADSLAgain();
           // register();
        }
        #endregion
        public void ADSLAgain()
        {
            ADSLHelper adsl = new ADSLHelper();
            adsl.Disconnect("宽带连接");
            Thread.Sleep(3000);//停顿三秒
            adsl.Connect(this.txtADSLName.Text, this.txtADSLNumber.Text, this.txtADSLPwd.Text);
            //MessageBox.Show("宽带连接成功!");
        }//宽带重新连接
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void 最大化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible = true;
            this.Visible = true;
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible = true;
            this.Visible = true;
        }

        private void 官网ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = "iexplore.exe";
            myProcess.StartInfo.Arguments = " http://www.cn244.com/";
            myProcess.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确实要退出本系统吗?", "系统提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Dispose();
                Application.Exit();
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;//阻止退出系统
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Url = new Uri("http://zc.qq.com/chs/index.html");
        }//刷新网页

        private void btnDisplay_Click(object sender, EventArgs e)
        {
  this.qqNumber.Text = this.qqNumber.Text + "\n" + "QQ号:" + webBrowser1.Document.GetElementById("newUin").InnerText
  + "注册时间" + DateTime.Now.ToString();
        }
        #region 判断网络连接状态
      
        private void judgeADSL_Click(object sender, EventArgs e)
        {
            System.Int32 dwFlag = new int(); 
            if(!InternetGetConnectedState(ref dwFlag,   0)) 
            MessageBox.Show("未联网");
            if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
                MessageBox.Show("采用调治解调器上网。"); 
            if((dwFlag   &   INTERNET_CONNECTION_LAN)!=0) 
            MessageBox.Show("采用网卡上网。");
        }
        #endregion
       
    }
}

posted on 2012-09-23 23:44  程序猴chengxuhou.com  阅读(1234)  评论(4编辑  收藏  举报

导航