写程序帮助我打星际
由于喜欢打星际,但手速APM不够快,水平难以提高,总是前方战争时后方忘记造兵,忙不过来。看到高手的第一视角,唯有惊叹的份儿了。巧的是最近编程技巧稍有长进,觉得编个小程序,实现每隔几秒钟控制各快捷键切换一遍,这样不就相当于提高了APM了嘛,嘿嘿。
其实利用API函数很容易实现的,首先利用FindWindow函数找到星际争霸的窗口,然后利用PostMessage函数向窗口发送选定的快捷键的值就行了。
另外,可以用GetAsyncKeyState控制实现关闭或打开此程序,我这里是用的是Backspace键。
刚开始可能有点不适应,习惯了应该有一点帮助。其他游戏也一样适用。
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(
System.Windows.Forms.Keys vKey); 
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(
System.Int32 vKey);

IntPtr hwd;
const int WM_CHAR = 0x0102;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
ArrayList alchar = new ArrayList();
int i = 0;
private System.Timers.Timer myTimer=new System.Timers.Timer();
private System.Timers.Timer myTimer2 = new System.Timers.Timer();
bool IsRun = true;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
hwd = FindWindow(null, this.textBox3.Text.Trim());
if (hwd.Equals((IntPtr.Zero)))
MessageBox.Show("未能获得窗口!");
StringBuilder vBuffer = new StringBuilder(256);
GetClassName(hwd, vBuffer, vBuffer.Capacity);
this.myTimer.Interval = 10;
this.myTimer2.Interval = 4000;
this.myTimer2.Elapsed += new System.Timers.ElapsedEventHandler(this.myTimer2_Tick);
i = 0;
}
private void myTimer2_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
if (alchar.Count > 0)
{
char c;
UnicodeEncoding encode = new UnicodeEncoding();
if (i >= alchar.Count)
i = 0;
if (alchar[i].ToString().Substring(0, 1).Equals("F"))
{
IntPtr wParam = (IntPtr)118;
switch (alchar[i].ToString().Trim())
{
case "F1": wParam = (IntPtr)112; break;
case "F2": wParam = (IntPtr)113; break;
case "F3": wParam = (IntPtr)114; break;
case "F4": wParam = (IntPtr)115; break;
case "F5": wParam = (IntPtr)116; break;
case "F6": wParam = (IntPtr)117; break;
}
PostMessage(hwd, WM_KEYDOWN, wParam, (IntPtr)0);
PostMessage(hwd, WM_KEYUP, wParam, (IntPtr)0);
}
else
{
c = encode.GetChars(encode.GetBytes(alchar[i].ToString().TrimEnd()))[0];
Message msg = Message.Create(hwd, WM_CHAR, new IntPtr(c), new IntPtr(0));
int result = PostMessage(hwd, msg.Msg, msg.WParam, msg.LParam);
result = PostMessage(hwd, msg.Msg, msg.WParam, msg.LParam);
}
i++;
}
}
private void button1_Click(object sender, EventArgs e)
{
int count=0;
alchar.Clear();
ArrayList al = new ArrayList();
al.Add(this.checkBox1);
al.Add(this.checkBox2);
al.Add(this.checkBox3);
al.Add(this.checkBox4);
al.Add(this.checkBox5);
al.Add(this.checkBox6);
al.Add(this.checkBox7);
al.Add(this.checkBox8);
al.Add(this.checkBox9);
al.Add(this.checkBox10);
al.Add(this.checkBox11);
al.Add(this.checkBox12);
al.Add(this.checkBox13);
al.Add(this.checkBox14);
al.Add(this.checkBox15);
al.Add(this.checkBox16);
foreach(CheckBox cb in al)
{
if (cb.Checked)
{
count++;
alchar.Add(cb.Text.ToString().TrimEnd());
}
}

this.myTimer2.Interval = Convert.ToInt32(this.textBox1.Text.Trim()) * 1000;
this.myTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.myTimer_Elapsed);
this.myTimer.Start();
this.myTimer2.Start();
}
private void myTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
foreach (System.Int32 k in Enum.GetValues(typeof(Keys)))
{
if (GetAsyncKeyState(k) == -32767)
{
if (k == 8)
{

if (IsRun)
{
IsRun = false;
this.myTimer2.Stop();
i = 0;
}
else
{
IsRun = true;
i = 0;
this.myTimer2.Start();
}
}
}
}
}

}

浙公网安备 33010602011771号