Eux
Eux

Reputation: 512

Knowing when an external process' window shows

How can I get an event relative to when the main window of an external process is showed?

I start the process using

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

Than I want to get the moment the main window appears and resize 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);

Upvotes: 1

Views: 144

Answers (1)

Eux
Eux

Reputation: 512

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);

Probably not the best solution, but a good starting point

Upvotes: 1

Related Questions