最近有个需求,需要开发一个程序,从第三方接口读取数据,第三方接口提供专门的VPN,连接后,才可能正常读取数据.连接方式是通过EasyConnect 连接,这家公司真有钱,居然购买的正版EasyConnect授权,然后给下面的子公司使用.
程序写好后,发现EasyConnect每隔12个小时左右会掉线,这不扯吗,你掉线了,程序还怎么读,发现这个VPN软件没有断开重连的功能,要疯了,于是只能自己写程序,定时判断是否连接,如果断开了,就让重新连接
1.首先要获取这个程序
/// <summary>
/// 获取当前正在运行的应用程序
/// </summary>
/// <param name="name">应用程序名称</param>
/// <returns></returns>
public Process ProcessGet(string name)
{
var processes = Process.GetProcessesByName(name);
if (processes == null || processes.Length <= 0) return null;
foreach (var thisproc in processes)
{
var strBuilder = new StringBuilder(256);
Win32.GetWindowText(thisproc.MainWindowHandle, strBuilder, 256);
var txt = strBuilder.ToString();
if (string.IsNullOrEmpty(txt)) return thisproc;
if ("BOSSLOGIN:" + name == txt) return thisproc;
if (txt.IndexOf("BOSSLOGIN:") == 0) continue;
return thisproc;
}
return null;
}
获取这个进程
SiteProcess item = new SiteProcess();
var process = item.ProcessGet("EasyConnect");
2.将窗体置于最前方
public Point ProcessShow(Process pross)
{
Win32.RECT rect;
Win32.GetWindowRect(pross.MainWindowHandle, out rect);
Point _point = new Point(rect.Left, rect.Top);
Win32.ShowWindow(pross.MainWindowHandle, Win32.WindowAction.Show);
Win32.SetForegroundWindow(pross.MainWindowHandle);
return _point;
}
3.在指定的坐标位置,给它来个点击
public void ProcessClick(int x, int y, Point point)
{
x += point.X;
y += point.Y;
Win32.SetCursorPos(x, y);
Win32.mouse_event((int)(Win32.MouseEventFlags.LeftDown | Win32.MouseEventFlags.Absolute), x, y, 0, 0);
Thread.Sleep(50);
Win32.mouse_event((int)(Win32.MouseEventFlags.LeftUp | Win32.MouseEventFlags.Absolute), x, y, 0, 0);
Thread.Sleep(150);
}