无法最大化外部进程的窗口

时间:2015-08-12 20:16:52

标签: c# .net

到目前为止,我在最大化外部窗口方面没有成功。 我可以拿到手柄并把它带到前台。 但它不会最大化。

Process[] processlist = Process.GetProcesses();
IntPtr handle;

foreach (Process process in processlist)
{
    if (process.ProcessName == "wfica32")
    {
        handle = process.MainWindowHandle;
        SetForegroundWindow(handle);
        ShowWindowAsync(handle, 3); // or SW_SHOWMAXIMIZED//  = 3;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试不安全的方法。

// Pinvoke declaration for ShowWindow
private const int SW_SHOWMAXIMIZED = 3;

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

// Sample usage
ShowWindow(proc.MainWindowHandle, SW_SHOWMAXIMIZED);

答案 1 :(得分:0)

您需要使用'ProcessID'代替流程。

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

    private const int SwShowmaximized = 3;
    private void Run()
    {
        Process[] processlist = Process.GetProcesses();

        foreach (Process process in processlist.Where(process => process.ProcessName == "wfica32"))
        {
            ShowWindow(Process.GetProcessById(process.Id).MainWindowHandle, SwShowmaximized);
        }
    }