how to notify my application when show desktop/minimize all/ all windows minimized using c#
views:
791answers:
4The following might get you started. This is just a standard form with a ListBox on it (named listMessages). When I perform a desktop minimize/showall, the form catches the WM_SIZE messages and outputs the Message m values to the ListBox. Your form may not respond to typical minimize and maximize events but it should receive these messages from the windows message pump. As far as detecting if any another window has been shown that's a bit more involved but can be done as well....
using System;
using System.Windows.Forms;
namespace MinimizeAll
{
public partial class Form1 : Form
{
private const int WmSize = 5;
private const int SizeRestored = 0;
private const int SizeMinimized = 1;
private const int SizeMaximized = 2;
private const int SizeShow = 3;
private const int SizeHide = 4;
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
try
{
if (m.Msg == WmSize)
{
var wparam = m.WParam.ToInt32();
switch (wparam)
{
case SizeRestored:
case SizeMinimized:
case SizeMaximized:
case SizeShow:
case SizeHide:
var output = string.Format("{0}{1:X} {2:X} {3:X} {4:X} {5:X}", prefix, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32(), m.HWnd.ToInt32(), m.Result.ToInt32());
listMessages.Items.Add(output);
break;
default:
// this is just a demo (code police)...
base.WndProc(ref m);
return;
}
}
else
{
base.WndProc(ref m);
}
}
catch (Exception)
{
listMessages.Items.Add("err");
base.WndProc(ref m);
}
}
}
}
So... in order to minimize all the windows you can use the following:
Add to your project the "Microsoft Shell Controls And Automation" COM reference(References=>Add=>COM).
then do the folowing:
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll(); // can also do: shell.UndoMinimizeAll();
or with the late binding:
Object shell;
shell = CreateObject("Shell.Application");
shell.MinimizeAll();
Now I am not sure if you can use some events of this COM (like AllMinimized)...
In order to prevent a WinForm application from minimizing(via Minimize button):
void Form1_Resize(object sender, System.EventArgs e) // Handles Form1.Resize
{
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
}
You can also take a look here: http://pinvoke.net/default.aspx/user32.EnumDesktopWindows
In order for you application to know when it is minimized (the most probable event for how desktop/minimize all/ all windows minimized, You need to check on the
this.WindowState
property of your application's current form. if it is minimized then it should be equal to
FormWindowState.Minimized
Edited: try this out:
foreach (Process proc in Process.GetProcesses())
{
/// check proc.StartInfo.ProcessWindowStyle here
/// it Gets window state to use when the process is started.
}
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo_properties.aspx
I completely agree with Ian Boyd's comment. In no way should you try to circumvent defined system behavior. However, to abide by defined system behavior and still (maybe) get what you are looking for, you might want to look into using appbars for your main window which you do not want to have hidden. An appbar, is like the taskbar, it stays visible all of the time except when a fullscreen application is running.