程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c#中模擬鍵盤(1)

c#中模擬鍵盤(1)

編輯:關於C語言

前一段時間瘋狂的迷上了魔獸世界,為之瘋狂,但是9c的5區,讓多少玩家郁悶啊,守護之劍每天排隊800+,還不停地卡機,掉線,本人上班族,每天晚上6點下班回家排隊,上線也都8點多了,MC啊,黑E啊,AQL啊等活動早開始了,十分的郁悶!

為了按時玩游戲,本著將魔獸進行到底的原則,決定開發一個魔獸的自動登陸器,以解決燃眉之急,哈哈!

首先定義一個類ViaStruct,用來存儲游戲的路徑,等待時間,以及用戶名,密碼!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace KeyBoardInput
{
  [Serializable]
  public class ViaStruct
  {
    private string timer = "";
    private string filepath = "";
    private string username = "";
    private string pwd = "";
    private string timer2 = "";
    public bool SuccessFlag = false;
    public ViaStruct()
    {
      SuccessFlag=ReadFormKBI();
    }
    #region 屬性
    public string Timer
    {
      get { return timer; }
      set { timer = value; }
    }
    public string FilePath
    {
      get { return filepath; }
      set { filepath = value; }
    }
    public string UserName
    {
      get { return username; }
      set { username = value; }
    }
    public string Pwd
    {
      get { return pwd; }
      set { pwd = value; }
    }
    public string Timer2
    {
      get { return timer2; }
      set { timer2 = value; }
    }
    #endregion
    private bool ReadFormKBI()
    {
      StreamReader sr = new StreamReader("info.txt");
      if ((this.filepath = sr.ReadLine()) == null || this.filepath == "")
      {
        return false;
      }
      this.timer = sr.ReadLine();
      this.timer2 = sr.ReadLine();
      this.username = sr.ReadLine();
      this.pwd = sr.ReadLine();
      sr.Close();
      sr.Dispose();
      GC.Collect();
      return true;
    }
    public void WriteToKBI()
    {
      Thread th = new Thread(new ThreadStart(NewMethod));
      th.Start();
    }
    private void NewMethod()
    {
      try
      {
        StreamWriter sw = new StreamWriter("info.txt");
        sw.WriteLine(this.filepath);
        sw.Flush();
        sw.WriteLine(this.timer);
        sw.Flush();
        sw.WriteLine(this.timer2);
        sw.Flush();
        sw.WriteLine(this.username);
        sw.Flush();
        sw.WriteLine(this.pwd);
        sw.Flush();
        sw.Close();
        MessageBox.Show("寫入成功!");
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }
  }
}
  
然後編寫Form1窗口
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace KeyBoardInput
{
  public partial class Form1 : Form
  {
    ViaStruct via;
    Thread th;
    Thread ti;
    public Form1()
    {
      InitializeComponent();
      via = new ViaStruct();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
      this.textBox1.Text = via.UserName;
      this.textBox2.Text = via.Pwd;
      this.textBox3.Text = via.FilePath;
      this.textBox4.Text = via.Timer2;
      this.comboBox1.Text = via.Timer;
      th = new Thread(new ThreadStart(NewMethod));
      th.Start();
    }
    private void NewMethod()
    {
      if (via.SuccessFlag)
      {
        try
        {
          Thread.Sleep(Convert.ToInt32((Convert.ToDecimal(via.Timer) * 60 * 1000)));//開起程序後等待
                                                                             //via.Timer時間內啟動程序
          Process.Start(via.FilePath);
          Thread.Sleep(Convert.ToInt32((Convert.ToDecimal(via.Timer2) * 60 * 1000)));//啟動程序後等待                                                                   //via.Timer2時間輸入用戶名密碼
        }
        catch (Exception ex)
        {
         if(ex.GetType().ToString()!="System.Threading.ThreadAbortException")
          MessageBox.Show(ex.Message+"1");
        }        
      }
      if (via.SuccessFlag)
      {
        MySendKeys();
      }
    }
    private void button1_Click(object sender, EventArgs e)
    {
      this.openFileDialog1.RestoreDirectory = true;
      if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
      {
        this.textBox3.Text = this.openFileDialog1.FileName;
      }
      // MessageBox.Show(Environment.CurrentDirectory);
    }
    private void notifyIcon1_DoubleClick(object sender, EventArgs e)
    {
      this.Show();
    }
    private void Form1_MinimumSizeChanged(object sender, EventArgs e)
    {
      this.Visible = false;
    }
    private void MySendKeys()//輸入用戶名密碼
    {
      foreach (char ArrayValue in via.UserName.ToCharArray())
      {
        SendKeys.SendWait(ArrayValue.ToString());
        Thread.Sleep(10);
      }
      SendKeys.SendWait("{Tab}");
      foreach (char ArrayValue in via.Pwd.ToCharArray())
      {
        SendKeys.SendWait(ArrayValue.ToString());
        Thread.Sleep(10);
      }
      SendKeys.SendWait("{Enter}");
    }
    private void button2_Click(object sender, EventArgs e)//給Via對象賦值
    {
      via.Timer = this.comboBox1.Text;
      via.Timer2 = this.textBox4.Text;
      via.FilePath = this.textBox3.Text;    
      via.UserName = this.textBox1.Text;
      via.Pwd = this.textBox2.Text;
      via.WriteToKBI();     
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)//關閉窗口後推出線程
    {
      if (th.ThreadState != System.Threading.ThreadState.Aborted)
        th.Abort();
      if (ti!=null&&ti.ThreadState !=System.Threading.ThreadState.Aborted)
        ti.Abort();
    }
    private void button3_Click(object sender, EventArgs e)
    {
      th.Abort();
      ti = new Thread(new ThreadStart(PrograssImmediately));
      ti.Start();
    }
    private void PrograssImmediately()
    {
      if (via.SuccessFlag)
      {
        try
        {
          Process.Start(via.FilePath);
          Thread.Sleep(Convert.ToInt32((Convert.ToDecimal(via.Timer2) * 60 * 1000)));
        }
        catch (Exception ex)
        {
          //MessageBox.Show(ex.GetType().ToString());
          if (ex.GetType().ToString() != "System.Threading.ThreadAbortException")
            MessageBox.Show(ex.Message + "1");
        }
      }
      if (via.SuccessFlag)
      {
        MySendKeys();
      }
    } 
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved