Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
696 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...