C# get window handle after starting a process

喜欢而已 提交于 2019-12-17 20:36:24

问题


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


回答1:


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




回答2:


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:


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.




回答4:


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



来源:https://stackoverflow.com/questions/1421166/c-sharp-get-window-handle-after-starting-a-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!