C#中使用Windows API控制阿里旺旺自动登录 的程序源代码

本文档介绍了如何使用C#结合Windows API编写一个自动登录阿里旺旺的程序。程序包含了配置读取、登录逻辑、窗口控制等关键代码,并提供了解析和示例。作者提醒部分代码可能需要调试,且项目结构和代码未经整理,鼓励读者提出改进意见。

 前两天看论坛有人发帖子问关于自动登录阿里旺旺的软件的实现.自己正好赶到周末 闲暇无聊之时写了个demo 希望大家给点意见和建议.

 

项目名称TestLoginALiWangWang

包括以下命名空间

TestLoginALiWangWang

TestLoginALiWangWang.Control

TestLoginALiWangWang.Converts

TestLoginALiWangWang.Entity

TestLoginALiWangWang.Function

包括2个窗体

MainForm//主窗口

Select//登录/退出设置窗口

 

下面贴出主要代码分享

 

//MainForm.cs

 

using System;
using System.Windows.Forms;
using TestLoginALiWangWang.Entity;
using TestLoginALiWangWang.Function;
using System.Collections;

using System.IO;
using System.Diagnostics;
using System.Threading;

namespace TestLoginALiWangWang
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        #region 常量
        const string FileName = @"/config.cfg";
        const int MaxPassWordLength = 20;
        const int MaxUserNameLength = 20;
        const int MinPassWordLength = 6;
        const int MinUserNameLength = 4;
        const int MaxLogin = 5;
        #endregion

        #region 私有变量
        //启动数
        private int StartCount = 0;
        //启动的进程列表
        private MyWork[] WorkGroup = new MyWork[MaxLogin];
        //配置
        private AppConfig Config;
        //配置文件操作
        private ConfigFunction Function = new ConfigFunction();
        //定义一个登录事件的回调方法,当登录失败时回调此方法执行操作
        public delegate void UpdateLogin(User UserInfo);
        #endregion

        #region 事件响应
        private void MainForm_Load(object sender, EventArgs e)
        {
            //读取配置文件
            try
            {
                this.Config = this.Function.GetAppConfig(Application.StartupPath + FileName);
                this.TxtPath.Text = this.Config.ExePath;
                foreach (string key in this.Config.Users.Keys)
                {
                    this.CbxUserName.Items.Add(key);
                }
            }
            catch
            {
                if (MessageBox.Show(this, "配置文件产生错误,是删除现有配置文件而重新配置?",
                    "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (File.Exists(Application.StartupPath + FileName))
                    {
                        File.Delete(Application.StartupPath + FileName);
                    }
                }
                else
                {
                    Application.Exit();
                }
            }
            finally
            {
                if (this.Config == null)
                {
                    //防止程序运行时错误
                    this.Config = new AppConfig();
                }
                this.CbxUserType.SelectedIndex = 0;
                this.CbxUserName.Text = this.Config.Default;
            }
        }

        private void BtnPath_Click(object sender, EventArgs e)
        {
            if (this.FdgExePath.ShowDialog() == DialogResult.OK)
            {
                if (FileVersionInfo.GetVersionInfo(this.FdgExePath.FileName).FileVersion == "1, 0, 0, 1")
                {
                    this.TxtPath.Text = this.FdgExePath.FileName;
                }
                else
                {
                    if (MessageBox.Show(this, "文件版本高于[1.0.0.1],可能会无法执行操作!/r/n/r/n是否继续进行操作?",
                        "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        this.TxtPath.Text = this.FdgExePath.FileName;
                    }
                    else
                    {
                        this.TxtPath.Text = "";
                    }
                }
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            this.Config.ExePath = this.TxtPath.Text;
            if (this.Config.Users.Contains(this.CbxUserName.Text))
            {
                this.Config.Default = this.CbxUserName.Text;
            }
            this.Function.SetAppConfig(Application.StartupPath + @"/config.cfg", this.Config);
        }

        private void BtnAddUser_Click(object sender, EventArgs e)
        {
            if (this.BtnAddUser.Text == "添加")
            {
                if (this.CbxUserName.Text.Length >= MinUserNameLength &&
                    this.CbxUserName.Text.Length <= MaxUserNameLength)
                {
                    if (!this.CbxUserName.Items.Contains(this.CbxUserName.Text))
                    {
                        if (this.TxtPassWord.Text.Length >= MinPassWordLength &&
                            this.TxtPassWord.Text.Length <= MaxPassWordLength)
                        {
                            this.CbxUserName.Items.Add(this.CbxUserName.Text);
                            this.Config.Users.Add(this.CbxUserName.Text,
                                new User(this.CbxUserName.Text,this.TxtPassWord.Text, this.CbxUserType.SelectedItem.ToString()));
                            this.BtnAddUser.Text = "删除";
                        }
                        else
                        {
                            MessageBox.Show("请输入正确的密码,它的长度应该在" + MinPassWordLength + "-" + MaxPassWordLength + "个字符之间.", "提示");
                        }
                    }
                    else
                    {
                        MessageBox.Show("这个用户已经添加,无需再次添加。", "提示");
                    }
                }
                else
                {
                    MessageBox.Show("请输入正确的账号,它的长度应该在" + MinUserNameLength + "-" + MaxUserNameLength + "个字符之间.", "提示");
                }
            }
            else
            {
                if (this.Config.Users[this.CbxUserName.Text] != null)
                {
                    this.Config.Users.Remove(this.CbxUserName.Text);
                    this.CbxUserName.Items.Remove(this.CbxUserName.Text);
                }
                this.BtnAddUser.Text = "添加";
            }
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (!File.Exists(this.TxtPath.Text))
            {
                MessageBox.Show("执行文件路径错误,请重新选择.", "提示");
                BtnPath_Click(sender, e);
            }
            if (new Select(this.Config.Users, MaxLogin).ShowDialog(this) == DialogResult.OK)
            {
                foreach (User user in this.Config.Users.Values)
                {
                    if (!user.Login && HasRun(user.UserName))
                    {
                        //需要退出的
                        for (int i = 0; this.StartCount > 0 && i < this.WorkGroup.Length; i++)
                        {
                            if (this.WorkGroup[i] != null)
                            {
                                if (this.WorkGroup[i].UserInfo.UserName == user.UserName)
                                {
                                    this.WorkGroup[i].Dispose();
                                    this.WorkGroup[i] = null;
                                    this.StartCount--;
                                }
                            }
                        }
                    }
                }
                foreach (User user in this.Config.Users.Values)
                {
                    if (user.Login && !HasRun(user.UserName))
                    {
                        //需要登录的
                        if (this.StartCount < MaxLogin)
                        {
                            for (int i = 0; i < this.WorkGroup.Length; i++)
                            {
                                if (this.WorkGroup[i] == null)
                                {
                                    this.WorkGroup[i] =
                                        new MyWork(user, this.Config.ExePath, new UpdateLogin(CallBack));
                                    new Thread(new ThreadStart(this.WorkGroup[i].Start)).Start();
                                    this.StartCount++;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("最多只能登录" + MaxLogin + "个账号.", "提示");
                    }
                }

            }
        }

        //登录失败的回调方法
        void CallBack(User UserInfo)
        {
            for (int i = 0; this.StartCount > 0 && i < this.WorkGroup.Length; i++)
            {
                if (this.WorkGroup[i] != null)
                {
                    if (this.WorkGroup[i].UserInfo.UserName == UserInfo.UserName)
                    {
                        this.WorkGroup[i].Dispose();
                        this.WorkGroup[i] = null;
                        this.StartCount--;
                        MessageBox.Show("账号[" + UserInfo .UserName+ "]登录失败.", "提示");
                    }
                }
            }

评论 40
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值