How to minimize an application in C#?
-
How do I minimize or close (for the tray) any application by C#? I'm trying to minimize TeamSpeak to the system tray.
-
You will need to search the application by name or ID via https://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocesses(v=vs.110).aspx , or https://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname(v=vs.110).aspx , or https://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessbyid(v=vs.110).aspx
An important detail has no way to minimize for SystemTray, this depends on the application, usually minimized applications that support the SystemTray natively can be configured to use SystemTray at the time they minimize, but not every external application will have this capability, yet yes 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
Searching an app by name and if it is found minimizes it:
HandleApp.Action("notepad", HandleApp.Actions.Minimize);
Search an app by name and if it is found maximizes it:
HandleApp.Action("notepad", HandleApp.Actions.Maximize);
Searching an app by name and if found leaves the normal size (standard):
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 https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx , in case I used 1, 2 and 3 that would beSW_SHOWNORMAL
,SW_SHOWMINIMIZED
andSW_SHOWMAXIMED
respectively, follows the list:Enum Value Description SW_HIDE
0 Hide the window and activate 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 top-level window in the Z order. SW_SHOWMINNOACTIVE
7 Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except that the window is not activated. 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 Defines the display state based on the SW_ 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 the windows of a different segment.