How to minimize/maximize opened Applications

前端 未结 3 1414
再見小時候
再見小時候 2020-12-16 16:48

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

 internal static class NativeMethods
{
    public static readonly Int32 GWL_STY         


        
3条回答
  •  粉色の甜心
    2020-12-16 16:59

    You can use findwindowbycaption to get the handle then maximize or minimize with 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 EnumWindows in that case you would only need:

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

    i is the index of the window.


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


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

提交回复
热议问题