7

我想知道如何提出一个特定的窗口。SetForegroundWindow 在窗口未最小化时有效!!但是当最小化窗口时,SetForegroundWindow 不起作用......

这是我的代码:

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

2 回答 2

13

您可以使用IsIconic() API检查窗口是否已最小化,然后使用ShowWindow()将其恢复:

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);
}
于 2013-11-08T15:48:08.073 回答
1

您可以将ShowWindow与您已有的结合使用,这是您的示例,稍作修改:

    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);
于 2013-11-08T13:16:06.163 回答