0
Process[] procs = Process.GetProcesses();
IntPtr hWnd;
foreach (Process proc in procs)
{
    if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
    {
        Debug.WriteLine("{0} : {1}", proc.ProcessName, hWnd);
    }
}

My question is, how can I restore minimized window having its handle (hWnd variable).

I would be grateful if you could also supply with some documentation of window handlers so I can see how to manipulate them.

1

1 Answer 1

2

Unless you're in the same application domain, you'll have to use an unmanaged API to do this.

private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private void restoreWindow(IntPtr hWnd)
{
    if (!hWnd.Equals(IntPtr.Zero))
    {
        ShowWindowAsync(hWnd, SW_SHOWMAXIMIZED);
    }
}

https://msdn.microsoft.com/en-us/library/ms633549(VS.85).aspx

4
  • Got it working, but with SW_RESTORE = 9;
    – stil
    Commented Aug 4, 2015 at 21:17
  • Sorry about that, should have looked closer at your question. msdn.microsoft.com/en-us/library/windows/desktop/… Has all of the commands
    – Teagan42
    Commented Aug 4, 2015 at 21:19
  • To make it work when window is not minimized but behind another window in foreground, I did a simple trick: explicitly called SW_MINIMIZE, and then SW_RESTORE. It forced window to minimize and then brought it to foreground. Correct me if there is a better way to approach such situation.
    – stil
    Commented Aug 4, 2015 at 21:29
  • 1
    @stil: If the window IsIconic, call ShowWindowAsync with SW_RESTORE. Otherwise, bring it to the top of the Z-order by calling SetWindowPos. Commented Aug 4, 2015 at 21:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.