C# 调用外部程序 程序又打开了一个窗口 怎么取又打开那个窗口的句柄??
C#调用外部程序 程序又打开了一个窗口 怎么取又打开那个窗口的句柄??根据进程名字取句柄 本帖最后由 Empty风 于 2017-6-19 14:17 编辑
static extern IntPtr GetTopWindow(IntPtr hWnd);
static extern Int32 GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private static readonly UInt32 GW_HWNDNEXT = 2;
//运行方法 返回 PID
public Int32 Run()
{
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "运行的程序";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit(2000);
return myProcess.Id;
}
//获取当前打开窗口的句柄ID
public IntPtr GetWnd(Int32 pID, String className, String text)
{
pid = pID;
IntPtr h = GetTopWindow(IntPtr.Zero);
while (h != IntPtr.Zero)
{
UInt32 newID;
GetWindowThreadProcessId(h, out newID);
if (newID == pID)
{
StringBuilder sbClassName = new StringBuilder(200);
StringBuilder sbText = new StringBuilder(200);
GetClassName(h, sbClassName, 200);
GetWindowText(h, sbText, 200);
if (sbClassName.ToString().IndexOf(className, StringComparison.CurrentCultureIgnoreCase) >= 0 &&
sbText.ToString().IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0)
{
break;
}
}
h = GetWindow(h, GW_HWNDNEXT);
}
return h;
}
页:
[1]