Minimizing All Open Windows in C#

Minimizing all open windows in C#

PInvoke.net is your friend :-)

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1 {
class Program {
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;

static void Main(string[] args) {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
System.Threading.Thread.Sleep(2000);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
}
}
}

Minimize all running windows when application runs

check this post:

How to programmatically minimize opened window folders

You'll have to enumerate all processes and exclude the current one (yours) instead of getting the "explorer" one. I'd suggest also putting exception handling and some checks in place since not all processes have windows to be minimized

Also this post for the log off part:

Log off user from Win XP programmatically in C#

How to programmatically minimize opened window folders

There is a less 'hacky' solution than the accepted answer available here: Minimize a folder

It's based on the Shell Objects for Scripting. Sample:

const int SW_SHOWMINNOACTIVE = 7;

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

static void MinimizeWindow(IntPtr handle)
{
ShowWindow(handle, SW_SHOWMINNOACTIVE);
}

//call it like:

foreach (IWebBrowser2 window in new Shell().Windows())
{
if (window.Name == "Windows Explorer")
MinimizeWindow((IntPtr)window.HWND);
}

The same thing can be achieved using the Internet Explorer Object Model

// add a reference to "Microsoft Internet Controls" COM component
// also add a 'using SHDocVw;'
foreach (IWebBrowser2 window in new ShellWindows())
{
if (window.Name == "Windows Explorer")
MinimizeWindow((IntPtr)window.HWND);
}

How to minimize/maximize opened Applications

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

C# Minimize Whole Application

Try something like this.

foreach (Form form in Application.OpenForms)
{
form.WindowState = FormWindowState.Minimized;
}

Minimize a borderless form by Win + M windows shortcut

As an option you can enable system menu (having minimize window style) for your borderless form, then while it doesn't show the control box, but the minimize command will work as expected.

Add the following piece of code to your Form and then Win + M will work as expected:

private const int WS_SYSMENU = 0x80000;
private const int WS_MINIMIZEBOX = 0x20000;
protected override CreateParams CreateParams
{
get
{
CreateParams p = base.CreateParams;
p.Style = WS_SYSMENU | WS_MINIMIZEBOX;
return p;
}
}

Possible to start process, minimize window, on click open same window process?

You can maximize process window through P/Invoke ShowWindow method.
Add this code to your class:

const int SW_SHOWMAXIMIZED = 3;

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

You need to get process handle, which could be achived like:

IntPtr handle = Process.GetProcesses()
.FirstOrDefault(p => p.ProcessName == "YourProcessName")
.MainWindowHandle;

Then call ShowWindow with passing handle and const value to it:

ShowWindow(handle, SW_SHOWMAXIMIZED);

Complete:

static void Main(string[] args)
{
Process yourProcess = Process.GetProcesses()
.FirstOrDefault(p => p.ProcessName == "Viber");
IntPtr handle = yourProcess.MainWindowHandle;

ShowWindow(handle, SW_SHOWMAXIMIZED);

Console.WriteLine("Process " + yourProcess.ProcessName + " window maximized!");
Console.ReadKey();
}

About ShowWindow you can read here:
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow



Related Topics



Leave a reply



Submit