2,543
社区成员
发帖
与我相关
我的任务
分享
System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Test20161123
{
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "PostMessage", CallingConvention = CallingConvention.Winapi)]
static extern bool PostMessage(IntPtr hwnd, int msg, uint wParam, uint lParam);
[STAThread]
static void Main(string[] args)
{
string path = "C:/xxx.exe(应用程序路径)";
Process p = Process.Start(path);
if (p == null)
{
MessageBox.Show("路径不能为空!");
}
IntPtr mainWindows = FindMainWindowHandle("xxx(你的应用程序窗体)", 100, 25);
Console.WriteLine("Handle to main window is " + mainWindows);
//有名字控件句柄
IntPtr butt = FindWindowEx(mainWindows, IntPtr.Zero, "Button", "Play!");//这里找play按钮
if (butt == IntPtr.Zero)
{
MessageBox.Show("没找到Play按钮!");
}
SendMessage(butt, 0xF5, butt, null); //给win发送点击按钮消息
}
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent, int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent, result, null, null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct < index && result != IntPtr.Zero);
return result;
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption, int delay, int maxTries)
{
IntPtr mwh = IntPtr.Zero;
bool formFound = false;
int attempts = 0;
while (!formFound && attempts < maxTries)
{
if (mwh == IntPtr.Zero)
{
Thread.Sleep(delay);
++attempts;
mwh = FindWindow(null, caption);
}
else
{
Console.WriteLine("主窗体没找到");
formFound = true;
}
}
if (mwh == IntPtr.Zero)
throw new Exception("主窗体没找到");
else
return mwh;
}
}
}
Dim Shell
Set Shell=WScript.CreateObject("WScript.Shell")
Shell.Run "C:/1.exe(这里是你的exe的路径)"
WScript.Sleep 1000
Shell.SendKeys "{ENTER}"
保存到1.vbs中,运行1.vbs试试,如果觉得直接暴露vbs不爽,可以用unity写个demo封装下