detecting when a form is minimizing

L

Lee

Hi,

How do I detect when a form is minimizing? specifically when a user clicks
the show desktop button on the taskbar, rather than the minimize button on a
form.

thanks in advance
 
J

Jeff Gaines

Hi,

How do I detect when a form is minimizing? specifically when a user
clicks the show desktop button on the taskbar, rather than the
minimize button on a form.

thanks in advance

I spent yesterday puzzling this out and ended up over-riding the
WndProc as follows:

protected override void WndProc(ref Message message)
{
base.WndProc(ref message);

switch (message.Msg)
{
case WM_SYSCOMMAND:
if ((int)message.WParam == SC_MINIMIZE)
{
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
this.Visible = false;
m_blnInTray = true;
}
break;
}
}

The constants are:
const int SC_RESTORE = 0xF120;
const int WM_SYSCOMMAND = 0x0112;
const int SC_MINIMIZE = 0xF020;

To restore the Window I use:
SendMessage(this.Handle, WM_SYSCOMMAND, SC_RESTORE, IntPtr.Zero);

I couldn't find a .NET way, but if there is one I would appreciate
knowing about it:)
 
M

Mattias Sjögren

Jeff & Lee,
I couldn't find a .NET way, but if there is one I would appreciate
knowing about it:)

Just override the OnResize or OnSizeChanged methods (or handle the
corresponding events). The WindowState will tell you whether the form
is minimized or not.


Mattias
 
L

Lee

I cannot seem to detect the minimizing event (even by overriding WndProc as
you have), is the minimize event triggered by clicking on the minimize
button on a form somehow different to the show desktop event?

I have noticed however that a form that is set as Topmost does actually stay
displayed when the show desktop button is pressed! Could this be
manipulated somehow?

thanks
 
L

Lee

I have tried this;-

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (this.WindowState.Equals(FormWindowState.Minimized))
{
MessageBox.Show("minimized");
}
}


however it still does not fire, just the same with overriding WndPrc, and
OnSizeChanged. Am i missing something obvious?

thanks
 
J

Jeff Gaines

Jeff & Lee,


Just override the OnResize or OnSizeChanged methods (or handle the
corresponding events). The WindowState will tell you whether the form
is minimized or not.


Mattias

Thanks Mattias, don't know why I couldn't get that working a couple of
days ago but it works fine now :)
 
J

Jeff Gaines

I cannot seem to detect the minimizing event (even by overriding
WndProc as you have), is the minimize event triggered by clicking on
the minimize button on a form somehow different to the show desktop
event?

I have noticed however that a form that is set as Topmost does
actually stay displayed when the show desktop button is pressed!
Could this be manipulated somehow?

Hi Lee

I have now got my form to minimise using the Resize event as Mattias
suggested,and I restore it when its NotifyIcon is Left Clicked.

You're right though about the Show Desktop thing, if I click that my
form disappears but it stays in the TaskBar and its NotifyIcon doesn't
appear so I wonder if it is made hidden rather than minimised? I'm
afraid you need somebody more techy than me to give a definitive answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top