C# 如何最小化/最大化打开的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18652162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:50:56  来源:igfitidea点击:

How to minimize/maximize opened Applications

c#

提问by sandeep.gosavi

I have list of open Applications. To get this list i have used following code

我有打开的应用程序列表。为了获得这个列表,我使用了以下代码

 internal static class NativeMethods
{
    public static readonly Int32 GWL_STYLE = -16;
    public static readonly UInt64 WS_VISIBLE = 0x10000000L;
    public static readonly UInt64 WS_BORDER = 0x00800000L;
    public static readonly UInt64 DESIRED_WS = WS_BORDER | WS_VISIBLE;

    public delegate Boolean EnumWindowsCallback(IntPtr hwnd, Int32 lParam);

    public static List<WindowWrapper> GetAllWindows()
    {
        List<WindowWrapper> windows = new List<WindowWrapper>();
        StringBuilder buffer = new StringBuilder(100);
        EnumWindows(delegate(IntPtr hwnd, Int32 lParam)
        {
            if ((GetWindowLongA(hwnd, GWL_STYLE) & DESIRED_WS) == DESIRED_WS)
            {
                GetWindowText(hwnd, buffer, buffer.Capacity);
                WindowWrapper wnd = new WindowWrapper();
                wnd.handle = hwnd;
                wnd.title = buffer.ToString();
                windows.Add(wnd);
            }
            return true;
        }, 0);

        return windows;
    }

    [DllImport("user32.dll")]
    static extern Int32 EnumWindows(EnumWindowsCallback lpEnumFunc, Int32 lParam);

    [DllImport("user32.dll")]
    public static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);

    [DllImport("user32.dll")]
    static extern UInt64 GetWindowLongA(IntPtr hWnd, Int32 nIndex);
}

public class WindowWrapper : IWin32Window
{
    internal IntPtr handle;
    internal String title;

    public IntPtr Handle
    {
        get { return handle; }
    }

    public String Title
    {
        get { return title; }
    }
}

to call this i used following code

调用这个我使用了以下代码

foreach (var wnd in NativeMethods.GetAllWindows())
       {
               string caption = wnd.title;
               string handle = wnd.Handle
               // Add this caption and handle to list
       }

Now, User will select any of the opened window from the list and my task is to read caption of the selected window, get handle of process and maximize/minimize or close window. How can I do this.

现在,用户将从列表中选择任何打开的窗口,我的任务是读取所选窗口的标题,获取进程句柄并最大化/最小化或关闭窗口。我怎样才能做到这一点。

采纳答案by Nicolas Tyler

You can use findwindowbycaptionto get the handle then maximize or minimize with showwindow

您可以使用findwindowbycaption获取句柄然后最大化或最小化showwindow

private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
// more here: http://www.pinvoke.net/default.aspx/user32.showwindow

[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

Then in your code you use this:

然后在你的代码中使用这个:

IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "The window title");
ShowWindow(hwnd, SW_MAXIMIZE);


Although it seems you already have the window handle by using EnumWindowsin that case you would only need:

尽管EnumWindows在这种情况下您似乎已经拥有了窗口句柄,但您只需要:

ShowWindow(windows[i].handle, SW_MAXIMIZE);

iis the index of the window.

i是窗口的索引。



to close the window you will use:

要关闭您将使用的窗口:

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);

in the code:

在代码中:

DestroyWindow(hwnd) //or DestroyWindow(windows[i].handle)

this is the unmanaged version of system.windows.forms.form.close()

这是非托管版本 system.windows.forms.form.close()



or you can use:

或者你可以使用:

Process [] proc Process.GetProcessesByName("process name");
proc[0].Kill();


or you can use:

或者你可以使用:

static uint WM_CLOSE = 0x0010;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

in code:

在代码中:

PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

回答by Max Markov

You may use native method ShowWindow with SW_MAXIMIZE, SW_MINIMIZE for ncmdShow Take a look at http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

您可以将本机方法 ShowWindow 与 SW_MAXIMIZE、SW_MINIMIZE 一起用于 ncmdShow 看看http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);


// in your code
ShowWindow(wnd.Handle, SW_MAXIMIZE);

回答by Alyafey

you can use ShowWindowAsync

你可以使用 ShowWindowAsync

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


ShowWindowAsync(wnd.Handle, SW_SHOWMINIMIZED );

and it's better and to use

并且更好地使用

    var openWindows = Process.GetProcesses().Where(process=> String.IsNullOrEmpty(process.MainWindowTitle)==false);

to get opened windows

打开窗户

I have test MainWindowTitle in Porcess and it helps to search on window given it's caption.

我在 Porcess 中测试了 MainWindowTitle 并且它有助于在窗口上搜索它的标题。

 var handles = Process.GetProcesses().Where(x => x.MainWindowTitle == "Untitled - Notepad").Select(y=>y.Handle).ToList();