tijian_tieying/web/testautoinput/Form1.cs
2025-02-20 12:14:39 +08:00

278 lines
10 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace testautoinput
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_test_Click(object sender, EventArgs e)
{
Process[] procs = Process.GetProcesses();
//StreamWriter sw = new StreamWriter(Application.StartupPath + "\\" + Guid.NewGuid().ToString("N") + ".txt", true, Encoding.Default);
Process proc_EpiRootG4 = null;
foreach (var p in procs)
{
if (p.ProcessName == "EpiRootG4")
{
proc_EpiRootG4 = p;
break;
}
}
if (proc_EpiRootG4 == null)
{
MessageBox.Show("疫苗接种程序没有启用!");
return;
}
//MessageBox.Show( proc_EpiRootG4.MainWindowTitle);
if (string.IsNullOrEmpty(proc_EpiRootG4.MainWindowTitle))
{
MessageBox.Show("没有找到疫苗接种主程序");
return;
}
NativeRECT rect = new NativeRECT();
GetWindowRect(proc_EpiRootG4.MainWindowHandle, out rect);
//MessageBox.Show(rect.left + "," + rect.top + "," + rect.right + "," + rect.bottom);
SetCursorPos(rect.left + 311, rect.top + 107);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
keybd_event(Keys.D3, 0, 0, 0);
keybd_event(Keys.D7, 0, 0, 0);
keybd_event(Keys.D1, 0, 0, 0);
keybd_event(Keys.D4, 0, 0, 0);
keybd_event(Keys.D0, 0, 0, 0);
keybd_event(Keys.D2, 0, 0, 0);
keybd_event(Keys.D0, 0, 0, 0);
keybd_event(Keys.D4, 0, 0, 0);
keybd_event(Keys.D0, 0, 0, 0);
keybd_event(Keys.D1, 0, 0, 0);
keybd_event(Keys.D2, 0, 0, 0);
keybd_event(Keys.D0, 0, 0, 0);
keybd_event(Keys.D1, 0, 0, 0);
keybd_event(Keys.D9, 0, 0, 0);
keybd_event(Keys.D0, 0, 0, 0);
keybd_event(Keys.D1, 0, 0, 0);
keybd_event(Keys.D7, 0, 0, 0);
keybd_event(Keys.D2, 0, 0, 0);
Thread.Sleep(1500);
SetCursorPos(rect.left + 416, rect.top + 350);
Thread.Sleep(300);
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(300);
SetCursorPos(rect.left + 520, rect.top + 497);
Thread.Sleep(300);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(1000);
keybd_event(Keys.Enter, 0, 0, 0);
Thread.Sleep(1000);
IntPtr jzck = IntPtr.Zero;
int counttime = 0;
while (true)
{
Application.DoEvents();
counttime++;
jzck = FindWindow("TetChildBespShot", "受种者接种处理");
if (jzck == IntPtr.Zero)
{
Thread.Sleep(100);
if(counttime <= 300)
continue;
else
break;
}
else
{
break;
}
}
if (jzck == IntPtr.Zero)
{
MessageBox.Show("没有找到受种者接种处理界面!");
return;
}
else
{
GetWindowRect(jzck, out rect);
//MessageBox.Show(rect.left + "," + rect.top + "," + rect.right + "," + rect.bottom);
//EnumChildWindow ecw = new EnumChildWindow(EnumChild);
//EnumChildWindows(jzck, ecw, "");
Thread.Sleep(300);
SetCursorPos(rect.left + 970, rect.top + 313);
Thread.Sleep(300);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event( MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
//mouse_event(, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(500);
SetCursorPos(rect.left + 948, rect.top + 361);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event( MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
//mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
//371402040120190172
//SendKeys.Send("{3}");
/*
while (true)
{
EnumChildWindow ecw = new EnumChildWindow(EnumChild);
EnumChildWindows(proc_EpiRootG4.Handle, ecw, "");
// GetWindowRect(mainHwnd.ToInt32(), ref rectMain);
int pid = 0;
GetWindowThreadProcessId(mainHwnd, out pid);
//rectMain.Height - rectMain.Y < 300说明是登录窗口
waitTime++;
if((int)mainHwnd!=0)
{
MessageBox.Show(mainHwnd.ToString());
}
//30秒没打开程序登录失败
if (waitTime >= 30)
return ;
Thread.Sleep(1000);
}*/
}
/// <summary>
/// 用于枚举子窗体是的委托
/// </summary>
/// <param name="WindowHandle">窗体句柄</param>
/// <param name="num">自定义</param>
/// <returns></returns>
public delegate bool EnumChildWindow(IntPtr WindowHandle, string num);
/// <summary>
/// 获取指定窗体的所有子窗体
/// </summary>
/// <param name="WinHandle">窗体句柄</param>
/// <param name="ec">回调委托</param>
/// <param name="name">自定义</param>
/// <returns></returns>
[DllImport("User32.dll")]
public static extern int EnumChildWindows(IntPtr WinHandle, EnumChildWindow ecw, string name);
/// <summary>
/// 获取指定窗体的标题
/// </summary>
/// <param name="WinHandle">窗体句柄</param>
/// <param name="Title">缓冲区取用于存储标题</param>
/// <param name="size">缓冲区大小</param>
/// <returns></returns>
[DllImport("User32.dll")]
public static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
/// <summary>
/// 获取窗体类型
/// </summary>
/// <param name="WinHandle">窗体句柄</param>
/// <param name="Type">类型</param>
/// <param name="size">缓冲区大小</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr WinHandle, StringBuilder Type, int size);
/// <summary>
/// 根据句柄获得进程id值
/// </summary>
/// <param name="handle">句柄</param>
/// <param name="pid"></param>
/// <returns></returns>
[DllImport("user32")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int pid);
IntPtr mainHwnd = IntPtr.Zero;//登录窗口句柄
string typeName = "山东省预防接种信息管理系统-门诊";//启动程序的窗口标题
/// <summary>
/// 枚举窗体
/// </summary>
/// <param name="handle"></param>
/// <param name="num"></param>
/// <returns></returns>
private bool EnumChild(IntPtr handle, string num)
{
StringBuilder type = new StringBuilder();
//StringBuilder type = new StringBuilder();
type.Length = 100;
//type.Length = 100;
//GetWindowText(handle, title, 100);//取标题
GetClassName(handle, type, 100);//取类型
MessageBox.Show(type.ToString());
if (type.ToString() == typeName)
{
mainHwnd = handle;
return false;
}
return true;
}
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[StructLayout(LayoutKind.Sequential)]
struct NativeRECT
{
public int left;
public int top;
public int right;
public int bottom;
}
//将枚举作为位域处理
[Flags]
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
}
//设置鼠标位置
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
//设置鼠标按键和动作
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy,
uint data, UIntPtr extraInfo); //UIntPtr指针多句柄类型
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow);
//该函数获取一个窗口句柄,该窗口雷鸣和窗口名与给定字符串匹配 hwnParent=Null从桌面窗口查找
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string strClass, string strWindow);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out NativeRECT rect);
}
}