Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Maximize an external window using Win32 API

Posted on 2006-04-27
3
Medium Priority
?
11,199 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:raheelasadkhan
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16553745
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);
            }
        }
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16553773
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);
            }
        }
0
 

Author Comment

by:raheelasadkhan
ID: 16554176
Works like a charm. Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question