How to minimize an application in C#?

Asked

Viewed 916 times

2

How do I minimize or close (to the tray) any application by C#? I’m trying to minimize Teamspeak to the system tray.

  • Where is the code?

  • http://www.jasinskionline.com/windowsapi/ref/s/showwindow.html

2 answers

15


You will have to search the application by name or ID via Process.GetProcesses, or Process.GetProcessByName, or Process.GetProcessById

An important detail, there is no way to minimize to the Systemtray, this depends on the application, usually minimized applications that support the Systemtray natively can be configured to use Systemtray at the moment they minimize, but not every external application will have this capability, yet can try to minimize.

I created an example:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

...

public class HandleApp
{
    //Enumera os tipos para usar com o switch (o 1,2,3 são da API do user32)
    public enum Actions { Normal = 1, Minimize = 2, Maximize = 3 };

    //Importa o user32.dll para poder usar as APIs nativas
    [DllImport("user32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    //Busca um aplicativo pelo nome
    public static IntPtr FindWindow(string title)
    {
        Process[] pros = Process.GetProcessesByName(title);

        if (pros.Length == 0)
            return IntPtr.Zero;

        return pros[0].MainWindowHandle;
    }

    //Dispara a ação desejada, só tem 3 opções no exemplo
    public static void Action(string name, Actions act)
    {
        IntPtr hWnd = FindWindow(name);

        if (!hWnd.Equals(IntPtr.Zero))
            ShowWindowAsync(hWnd, (int) act);
    }
}

Examples of use

Searches an application by name and if found minimizes it:

HandleApp.Action("notepad", HandleApp.Actions.Minimize);

Search an app by name and if found maximizes it:

HandleApp.Action("notepad", HandleApp.Actions.Maximize);

Searches an application by name and if found leaves normal size (default):

HandleApp.Action("notepad", HandleApp.Actions.Normal);

Extra

You can add more features to the:

public enum Actions { Normal = 1, Minimize = 2, Maximize = 3 };

The numbers of each enum must follow these Showwindow Function , in the case I used 1, 2 and 3 that would be SW_SHOWNORMAL, SW_SHOWMINIMIZED and SW_SHOWMAXIMED respectively, follows the list:

Enum Valor Description
SW_HIDE 0 Hides the window and activates another window.
SW_SHOWNORMAL 1 Activates and displays a window. If the window is minimized or maximized, the system restores its original size and position. An application must specify this flag when displaying the window for the first time.
SW_SHOWMINIMIZED 2 Activates the window and displays it as a minimized window.
SW_SHOWMAXIMED 3 Activates the window and displays it as a maximized window.
SW_MAXIMIZE 3 Maximizes the specified window.
SW_SHOWNOACTIVATE 4 Displays a window in its latest size and position. This value is similar to SW_SHOWNORMAL , except that the window will not be activated.
SW_SHOW 5 Activates the window and displays it in its current size and position.
SW_MINIMIZE 6 Minimizes the specified window and activates the next upper level window in order Z.
SW_SHOWMINNOACTIVE 7 Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except that the window is not enabled.
SW_SHOWNA 8 Displays the window in its current size and position. This value is similar to SW_SHOW , except that the window is not activated.
SW_RESTORE 9 Activates and displays the window. If the window is minimized or maximized, the system restores its original size and position. An application must specify this flag when restoring a minimized window.
SW_SHOWDEFAULT 10 Sets the display state based on theSW_ value specified in the STARTUPINFO structure passed to the Createprocess function by the program that started the application.
SW_FORCEMINIMIZE 11 Minimizes a window, even if the segment that has the window is not responding. This flag should only be used when minimizing windows of a different segment.
  • 3

    Thanks! I found in SO en but had not understood, now we also have in SO pt.

  • 1

    Did you make this table? Be patient.

  • 1

    @Cypherpotato besides making the table the items were cluttered, it was a little laborious, but I thought it would be interesting for you to want to implement more features ;)

-3

private void button4_Click(object sender, EventArgs e)
{
  Process[] pros = Process.GetProcessesByName("excel");
  IntPtr hnd = pros[0].MainWindowHandle; 
  ShowWindow(hnd,2);
}

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); // função externa
  • 1

    A small explanation would help to understand your answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.