Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring to forward window when minimized

I want to know how to bring forward a particular window. SetForegroundWindow works when the window is not minimized!! but when a minimize the window, SetForegroundWindow doesn't work...

this my code:

        int IdRemoto = int.Parse(textBoxID.Text);

        Process[] processlist = Process.GetProcessesByName("AA_v3.3");

        foreach (Process process in processlist)
        {
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                if (IdRemoto.ToString() == process.MainWindowTitle)
                    SetForegroundWindow(process.MainWindowHandle);  
            }
        }


[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
like image 478
Francisco Carvalho Avatar asked Nov 08 '13 12:11

Francisco Carvalho


2 Answers

You can check to see if the window is minimized using the IsIconic() API, then use ShowWindow() to restore it:

public const int SW_RESTORE = 9;

[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr handle);

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr handle, int nCmdShow);

[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr handle);

private void BringToForeground(IntPtr extHandle)
{
    if (IsIconic(extHandle))
    {
        ShowWindow(extHandle, SW_RESTORE);
    }
    SetForegroundWindow(extHandle);
}
like image 134
Idle_Mind Avatar answered Sep 22 '22 07:09

Idle_Mind


You can use ShowWindow combined with what you already have, here is your example with a little modification:

    int IdRemoto = int.Parse(textBoxID.Text);

    Process[] processlist = Process.GetProcessesByName("AA_v3.3");

    foreach (Process process in processlist)
    {
        if (!String.IsNullOrEmpty(process.MainWindowTitle))
        {
            if (IdRemoto.ToString() == process.MainWindowTitle)
            {
                ShowWindow(process.MainWindowHandle, 9);
                SetForegroundWindow(process.MainWindowHandle);  
            }
        }
    }


   [DllImport("user32.dll")]
   private static extern bool SetForegroundWindow(IntPtr hWnd);
   [DllImport("user32.dll")]
   private static extern bool ShowWindow(IntPtr hWind, int nCmdShow);
like image 36
Swift Avatar answered Sep 21 '22 07:09

Swift