C# get window handle after starting a process

2019-01-20 10:26发布

Is there a way to get the window handle (IntPtr) for a window after its launched from a C# app with Process.Start()?

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-20 11:16

Use

process.MainWindowHandle;

It probably is 0 when launching the app, so you might want to loop and sleep until it is filled up.

查看更多
爷的心禁止访问
3楼-- · 2019-01-20 11:18

If it's the main window you're after, Process.MainWindowHandle will give you what you need.

查看更多
爷的心禁止访问
4楼-- · 2019-01-20 11:21

This is not a recent topic but the answers are incomplete.

I agree with the Process.MainWindowHandle solution and to wait for the value but not with Sleep.

If you have just started a process and want to use its main window handle, consider using the WaitForInputIdle method to allow the process to finish starting, ensuring that the main window handle has been created.

Process.WaitForInputIdle

This overload applies only to processes with a user interface and, therefore, a message loop.

查看更多
来,给爷笑一个
5楼-- · 2019-01-20 11:23

You could also call Refresh() on the process to be sure the info in accurate

查看更多
登录 后发表回答