Knowing when an external process window opens

How can I get an event regarding when the main window of the external process is displayed?

I start the process using

pi = new ProcessStartInfo(); [...] Process.Start(pi) 

Than I want to get the moment when the main window appears, resize it and move it using:

 [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 
+1
source share
1 answer

I solved it like this:

 async void RepositionWindow(Process process) { while ((int)process.MainWindowHandle == 0) { await Task.Delay(100); } IntPtr hWnd = process.MainWindowHandle; while (!IsWindowVisible(hWnd)) { await Task.Delay(100); } MoveWindow(hWnd, 0, 0, 500, 800, true); } [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); [System.Runtime.InteropServices.DllImport("user32.dll")] [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); 

This may not be the best solution, but a good starting point

+1
source

Source: https://habr.com/ru/post/985058/


All Articles