Process 后, 如何获取打开窗口的句柄?

dongfangshang2 2012-07-31 08:57:37

myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = txt_ClientPath.Text;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
...全文
1250 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongfangshang2 2012-08-03
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

GW_HWNDNEXT 是获取下一个
不是获取第二个
[/Quote]

学习了,多谢
SocketUpEx 2012-08-03
  • 打赏
  • 举报
回复
GW_HWNDNEXT 是获取下一个
不是获取第二个


dongfangshang2 2012-08-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

C# code
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace Text
{
public class Program
{
[DllImport("user32.dll")]
st……
[/Quote]

多谢提供代码, 你用 private static readonly UInt32 GW_HWNDNEXT = 2; 来控制第二个? 那如果有N个怎么办?
事理 2012-08-01
  • 打赏
  • 举报
回复
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = path;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaiteForExit();
IntPtr temp = myProcess.MainWindowHandle;
MessageBox.Show(temp.ToString());

如果等于0,就要枚举窗口了。
http://www.cnblogs.com/slyzly/articles/2331487.html
SocketUpEx 2012-08-01
  • 打赏
  • 举报
回复
[DllImport("coredll.dll", EntryPoint="FindWindowW", SetLastError=true)]
private static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);


iyomumx 2012-08-01
  • 打赏
  • 举报
回复
那就只能调用EnumWindows API来枚举每个窗口并确定其所属进程以确定需要的窗口句柄
dongfangshang2 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

myProcess.MainWindowHandle
[/Quote]


myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = path;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Thread.Sleep(5000);
IntPtr temp = myProcess.MainWindowHandle;
MessageBox.Show(temp.ToString());



返回值0, 抓不到哦
SocketUpEx 2012-08-01
  • 打赏
  • 举报
回复
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace Text
{
public class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetTopWindow(IntPtr hWnd);

[DllImport("user32.dll")]
static extern Int32 GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

private static readonly UInt32 GW_HWNDNEXT = 2;

static Int32 Run()
{
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit(2000);

return myProcess.Id;
}

static IntPtr GetWnd(Int32 pID,String className, String text)
{
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;
}

static void Main(string[] args)
{
Console.WriteLine(GetWnd(Run(), "Notepad", "无标题 - 记事本"));

Console.ReadKey();
}
}
}





iyomumx 2012-08-01
  • 打赏
  • 举报
回复
Process.Id就是PID啊
dongfangshang2 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

同名窗口又怎样,你已经获得PID了,拿到窗口句柄以后GetWindowThreadProcessId验证下就可以了
[/Quote]

我怎么获得PID了? EnumWindows 枚举的时候 N个都是同名的窗口, 我如何能知道哪一个是我刚刚打开的呢。
iyomumx 2012-08-01
  • 打赏
  • 举报
回复
同名窗口又怎样,你已经获得PID了,拿到窗口句柄以后GetWindowThreadProcessId验证下就可以了
dongfangshang2 2012-08-01
  • 打赏
  • 举报
回复
各位枚举不太行哦, 因为有几个同名的窗口, 是多线程的操作
iyomumx 2012-07-31
  • 打赏
  • 举报
回复
myProcess.MainWindowHandle
我将带领大家来系统学习Windows的窗口编程,包括消息、窗口、GDI绘图、游戏开发等。本课程比较基础,非常适合初学者入门,读者可以边学习边实践。具体的章节目录和课程内容如下所示:---------------------------------------------Windows游戏编程系列之1:GUI界面编程及游戏入门实战1、Windows创建第一个窗口 WinMain入口函数 5进行Windows编程的调试手法 6窗口从哪里来? 7窗口编程的步骤 7窗口编程需要的主要结构 8窗口编程需要的主要API 92、Windows的窗口过程与消息机制 如何留住窗口? 121)Windows的消息与消息循环 142)消息处理函数与常用消息 17)Windows的窗口过程函数 19 3、GDI编程之设备上下文 1)GDI的通用编程框架 222)GDI的绘图步骤 253)GDI获取设备句 254、GDI编程之绘制几何图形 画点、线 28颜色COLORREF 29矩形 29画圆、饼图、弦图 305、GDI编程之自定义画笔画刷画笔简介 32画刷简介 33画笔案例 33画刷案例 346、GDI编程之绘制文字 DrawText函数 35TextOut 函数 (wingdi.h) 36CreateFont函数 37绘制文本案例 377、GDI编程之绘制位图 位图简介 381)在资源中添加位图资源 392)从资源中加载位图: LoadBitmap 393)创建一个与当前DC相匹配的DC(内存DC) 394)将bitmap放入匹配的DC中:SelectObject 405)成像(1:1 比例 ) 406)取出位图 407)释放位图 418)释放匹配的DC 41绘制位图案例 41   8、Windows鼠标键盘消息 一、键盘消息 421、键盘消息 422、消息参数: 423、消息的使用: 424、键盘消息的案例代码 43二、鼠标消息 441、基本鼠标消息 442、双击消息 443、滚轮消息 454、不响应双击消息 45 9、Windows定时器消息 定时器消息介绍 47创建定时器 47关闭定时器 47定时器消息案例代码 4810、GDI游戏之跳舞动画 11、GDI游戏之走路动画 12、GDI贪吃蛇游戏实战  

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧