I am launching a remote app by using the Process.Start method and passing an .rdp file. This works fine except that this .rdp file causes two windows to open. I need to get a handle on these two windows so that I can set them to be TopMost windows. I need them both to stay in front of the host application.
I beleive I should use FindWindowByCaption(IntPtr.Zero, "targetProcess.exe") but when I call this after Process.Start it fails because the window has not yet opened. How do I wait for this window to open before calling FindWindowByCaption. Here is what I have so far:
if (gcsMenuItem.ItemUrl.EndsWith(".rdp", StringComparison.InvariantCultureIgnoreCase))
{
string fileName = gcsMenuItem.ItemUrl.Split(':')[1];
targetProcess = Process.Start(GetProjectDataPath() + fileName);
targetProcess.WaitForInputIdle();
while (true)
{
IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, "targetProcess.exe");
if (hWnd != IntPtr.Zero)
{
break;
}
}
targethWnd = targetProcess.MainWindowHandle;
}
My while loop is no good because it blocks the UI. When the .rdp is launched, the user gets a login window first, then after logging in, it opens two windows. These are the ones I need to set as TopMost.
To set the TopMost, I believe I need to use something like:
SetWindowPos(hostWpfWindowHandle, HWND_TOPMOST, x, y, width, height, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOREDRAW);
question from:
https://stackoverflow.com/questions/65904391/wating-for-a-window-to-open-to-get-its-handle-when-launching-an-rdp-file 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…