Maximize an external window using Win32 API

Hello,

I use the following code to bring an external window to the foreground, making it active:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SetForegroundWindow (int hwnd);

   SetForegroundWindow(System.Diagnostics.Process.GetProcessById(pId).MainWindowHandle.ToInt32());

This works fine unless the window is minimized. In that case, the taskbar button gets the focus but the window does not restore to normal or maximized.

I tried the ShowWindow(pId, SW_MAXIMIZE) and that does not work. Nor does ShowWindowLong as follow:
   int      windowStyle;
   windowStyle      = GetWindowLong(process.MainWindowHandle.ToInt32(), GWL_STYLE);
   windowStyle      = windowStyle ^ WS_MAXIMIZE; // Tried with OR(|) as well as XOR(^)
   SetWindowLong(process.MainWindowHandle.ToInt32(), GWL_STYLE, windowStyle);

Any ideas?

Thanks,

Khan
raheelasadkhanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Try something like...

        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
       
        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        private static extern int SetForegroundWindow (int hwnd);

        private const int SW_RESTORE = 9;

        private void ShowPID(int pId)
        {
            IntPtr hWnd = System.Diagnostics.Process.GetProcessById(pId).MainWindowHandle();
            if (!hWnd.Equals(IntPtr.Zero))
            {
                if (IsIconic(hWnd))
                {
                    ShowWindow(hWnd, SW_RESTORE);
                }
                SetForegroundWindow(hWnd);
            }
        }
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Shoulda compiled first...

        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
       
        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        private static extern int SetForegroundWindow (IntPtr hWnd);

        private const int SW_RESTORE = 9;

        private void ShowPID(int pId)
        {
            IntPtr hWnd = System.Diagnostics.Process.GetProcessById(pId).MainWindowHandle;
            if (!hWnd.Equals(IntPtr.Zero))
            {
                if (IsIconic(hWnd))
                {
                    ShowWindow(hWnd, SW_RESTORE);
                }
                SetForegroundWindow(hWnd);
            }
        }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raheelasadkhanAuthor Commented:
Works like a charm. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.