using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Web;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace HeavenBenchmarkTest
{
public static class SendMouseEvent
{
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[Flags]
public enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
public static void Send(MouseEventFlag mouseEventFlag, int dx, int dy, uint dwData)
{
mouse_event(mouseEventFlag | MouseEventFlag.Absolute, dx, dy, dwData, UIntPtr.Zero);
}
public static void MoveTo(uint scceenTop, uint screenLeft)
{
int x = scceenTop == 0 ? 0 : (int)((float)scceenTop / (float)Screen.PrimaryScreen.Bounds.Height * (float)65535);
int y = screenLeft == 0 ? 0 : (int)((float)screenLeft / (float)Screen.PrimaryScreen.Bounds.Width * (float)65535);
mouse_event(MouseEventFlag.Move | MouseEventFlag.Absolute, x, y, 0, UIntPtr.Zero);
}
public static void Click()
{
LeftDown(); LeftUp();
}
public static void DoubleClick()
{
Click(); Click();
}
public static void LeftDown()
{
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
}
public static void LeftUp()
{
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
public static void RightDown()
{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
}
public static void RightUp()
{
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
public static void MiddleDown()
{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
}
public static void MiddleUp()
{
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.None;//设置窗体无边框样式
}
string[] Config = { "RunPorgramName", "RunTimer","Terminator1","Terminator2","CoordinateY", "CoordinateX" };
int TestToalTimer = 0;
private void Form1_Load(object sender, EventArgs e)
{
if(RedCfg("config.ini")==false)
{
WriteFile("FAIL.TXT");
MessageBox.Show("Config.ini Read Err!!","系统提示");
Application.Exit();
}
CallBath(Config[0]);
label1.BackColor = Color.Green;
label1.ForeColor = Color.GreenYellow;
label1.Font = new Font("宋体",20f,FontStyle.Bold);
label1.Text = "测试倒计时:" + Config[1] + "秒";
TestToalTimer = Convert.ToInt32(Config[1]);
timer2.Enabled = true;
}
public void CallBath(string CallBathName)
{
string targetDir = string.Empty;
targetDir = System.IO.Directory.GetCurrentDirectory() + @"\";
Process proc = null;
try
{
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = CallBathName;
proc.Start();
proc.WaitForExit();
}
catch(Exception ex)
{
MessageBox.Show("批处理执行出错"+ex.Message+ex.StackTrace.ToString());
WriteFile("FAIL.TXT");
Application.Exit();
}
}
public void WriteFile(string CfgFileNmae)
{
try
{
FileStream fs = new FileStream(CfgFileNmae,FileMode.Create,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
sw.WriteLine(CfgFileNmae);
sw.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show("Write File Err!!"+ex.Message);
Application.Exit();
}
}
public bool RedCfg(string CfgFileName)
{
bool Flag = false;
string[] KeyWord = { "RunPorgramName", "RunTimer", "Terminator1", "Terminator2", "CoordinateY", "CoordinateX" };
FileStream fs = new FileStream(CfgFileName,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);//默认读取文件可识别中文
try
{
string Temp = string.Empty;
while((Temp=sr.ReadLine())!=null)
{
string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
int n = 0;
foreach(string k in KeyWord)
{
if(k==Array[0])
{
Config[n] = Array[1];
break;
}
n++;
}
}
sr.Close();
fs.Close();
Flag = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Flag = false;
return Flag;
}
return Flag;
}
public void KillProcess(string RunBathName)
{
Process[] pro = Process.GetProcesses(); //获取已开启的所有进程
for(int i=0;i
{
//判断此进程是否是要查看的进程
if(pro[i].ProcessName.ToString().ToString()==RunBathName)
{
pro[i].Kill();//结束进程
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if(TestToalTimer==0)
{
timer1.Enabled = false;
KillProcess(Config[2]);
KillProcess(Config[3]);
WriteFile("PASS.TXT");
Application.Exit();
}
else
{
TestToalTimer--;
label1.Text = "测试倒计时:" + Convert.ToString(TestToalTimer) + "秒";
}
}
private void timer2_Tick(object sender, EventArgs e)
{
uint x = 0, y = 0;
x = Convert.ToUInt32(Config[4]);
y = Convert.ToUInt32(Config[5]);
SendMouseEvent.MoveTo(x,y);
SendMouseEvent.Click();
timer1.Enabled = true;
timer2.Enabled = false;
}
}
}