使用C#实现的一个游戏练级外挂,基本方法和源代码

简单的用c#实现自动打怪的按键精灵

并不是很高深的文章,就是一个按键精灵的C#实现,大家有兴趣继续研究,并欢迎讨论一下

首先,这不是介绍如何破解IP封包的文章,当时写这个的外挂只是做自动练级用,出于技术不够,便想了一个很笨的方法,从屏幕上的HP值的变化来判断是否自 动补血,而打怪更简单,这个游戏本身就带有寻找怪的功能,而弓箭手就只需要不断的瞄准和射击就可以了,而瞄准和射击也就是不断的按下(keydown)玩 家指定的键,如F1. 从这点来看,说是外挂其实有点勉强了. 

对于自动找怪和打怪, 说白了就是一个按键精灵. 在.net下发送 键盘消息可以用这个方法来实现System.Windows.Forms.SendKeys.Send(strKey),strKey是参数,代表要按下 的键比如’a’就代表要按下A键,而 “{F4}” 就是ALT + F4,我们利用这个函数来实现模拟按键,但是这个有一个不好的地方,就是他只把模拟 按键发送给激活窗口,如果是vc的话当然可以用event-keyboard来实现,在这个地方,我们可以强制把游戏窗口置顶.我们用下面的API来实 现. 

//这个函数是查找窗口句柄的, 参数sClassName,是类名, 参数sWindowTitle是窗口名 
[DllImport(\"user32.dll\", CharSet=CharSet.Auto)] 
public static extern IntPtr FindWindow(IntPtr sClassName, string sWindowTitle);   



//这个函数用来置顶显示,参数hwnd为窗口句柄 
[DllImport(\"user32.dll\", CharSet=CharSet.Auto)] 
public static extern void SetForegroundWindow(int hwnd); 



//这个函数用来显示窗口,参数hwnd为窗口句柄,nCmdShow是显示类型的枚举 
[DllImport(\"user32.dll\")] 
public static extern bool ShowWindow(int hWnd, nCmdShow nCmdShow); 


public enum nCmdShow:uint{ 
              SW_FORCEMINIMIZE=0x0, 
              SW_HIDE=0x1, 
              SW_MAXIMIZE=0x2, 
              SW_MINIMIZE=0x3, 
              SW_RESTORE=0x4, 
              SW_SHOW=0x5, 
              SW_SHOWDEFAULT=0x6, 
              SW_SHOWMAXIMIZED=0x7, 
              SW_SHOWMINIMIZED=0x8,  [Page]
              SW_SHOWMINNOACTIVE=0x9, 
              SW_SHOWNA=0xA, 
              SW_SHOWNOACTIVATE=0xB, 
              SW_SHOWNORMAL=0xC, 
              WM_CLOSE=0x10, 
         }   



现在让我们看看如何实现自动键盘精灵. 

我先定义了一个Timer,用于每各一段时间就向窗口发送一次按键.由于攻击是要先瞄准在攻击,所以在我在定义了一个bool whichKey,用来指示现在是在瞄准还是攻击. 

private System.Windows.Forms.Timer timer1; //timer1主要控制瞄准与打怪 
private System.Windows.Forms.Timer timer2; //timer2 主要监视当前血量和补血 
private string strCollimation;    //瞄准键 
private string strAttack;         //攻击键 
private string strHP;             //补血键 
private string strMP;             //补蓝键 
private bool whichKey;            //瞄准和攻击的状态 
private int count;                //定义一个变化指示 

// 桌面工作区的尺寸 
Size workingArea; 

// Form 的初始位置和在左下角,右下角的位置 
Point formLoc, ptLeftBottom, ptRightBottom; 



//初始化设置 
private void Init(){ 
     this.timer1.Interval =Convert.ToInt32(Convert.ToDouble(this.txtTime.Text) * 1000); 
     this.whichKey = false;   strCollimation = this.txtCollimation.Text; 
     strAttack = this.txtAttack.Text; 
     strHP = this.txtHP.Text; 
     strMP = this.txtMP.Text; 


我们在进行如上的初始化设置,其中txtTime,txtCollimation,txtAttack…为TextBox.    [Page]

timer1的Tick事件如下   ( timer2 见后面) 


private void timer1_Tick(object sender, System.EventArgs e){ 
     if(this.whichKey == false){ 
         System.Windows.Forms.SendKeys.Send(strCollimation); 
         this.whichKey = true; 
     } 
     else{ 
         System.Windows.Forms.SendKeys.Send(strAttack); 
         this.whichKey = false; 
         } 
     } 
}   
whichKey变量使timer1的TICK事件不断的交替发送瞄准和攻击按键. 

刚 才我们说过, System.Windows.Forms.SendKeys.Send(string str) 函数只能发送给当前的激活窗口,我们还 需要把窗口强制激活了才行.我定义了一个按钮(Button)name=”btnOK”,按下后既开启外挂.下面我们来看看btnOK的事件处理,中国自 学编程网,www.zxbc.cn . 


private void btnOK_Click(object sender, System.EventArgs e){ 
     Init(); 
     string strWindowsName = \"Tantra Launcher\"; //窗口标题 
     IntPtr hWnd = FindWindow( “” ,strWindowsName);//窗口句柄 
     if(hWnd.ToInt32()>0){ 
          SetForegroundWindow(hNext.ToInt32());//置顶显示 
          ShowWindow(hNext.ToInt32(),nCmdShow.SW_SHOWMINNOACTIVE); //显示窗口   
          this.timer1.Enabled = true;       //开始发送按键 
          this.timer2.Enabled = true;       //开始监控当前HP值 
     } 
     else 
                   MessageBox.Show(\"错误\",\"未发现游戏,请先启动游戏后在打开外挂!\",MessageBoxButtons.OK,MessageBoxIcon.Error); 
     formLoc         = this.Location;  [Page]
     this.Location   = ptRightBottom; 
}   
如 上,当点下确定按钮后,首先查找一个叫Tantra Launcher窗口,这是密传的窗口名,返回该窗口的句柄,然后 SetForegroundWindow函数把他置顶显示,又由ShowWindow将他显示,最后 this.timer1.Enabled = true. 

这样我们用来实现自动打怪的按键精灵就实现了.

Tags:  C#外挂 练级外挂 外挂程序源代码 外挂源代码

延伸阅读

最新评论

发表评论