Detecting Minimize event causes loop

  • Thread starter Thread starter BillAtWork
  • Start date Start date
B

BillAtWork

Hi,
Found some code to do this but seems to be causing a stack overflow due to a
loop somewhere. Code is:

....
public event EventHandler Minimize;
....

//in form constructor
....
this.Minimize += new EventHandler(Form1_Minimize);
....

void Form1_Minimize(object sender, EventArgs e)
{
this.ShowInTaskbar = false; //System tray app - no taskbar icon
when minimized. It gets turned back on elsewhere.
}

protected override void WndProc(ref Message msg)
{
const int WM_SIZE = 0x0005;
const int SIZE_MINIMIZED = 1;

if ((msg.Msg == WM_SIZE) && ((int)msg.WParam == SIZE_MINIMIZED)
&& (this.Minimize != null))
{
this.Minimize(this, EventArgs.Empty);
}

base.WndProc(ref msg);
}
Any ideas?

Thanks!
 
Hi,
Found some code to do this but seems to be causing a stack overflow due to a
loop somewhere. Code is:

...
public event EventHandler Minimize;
...

//in form constructor
...
this.Minimize += new EventHandler(Form1_Minimize);
...

void Form1_Minimize(object sender, EventArgs e)
{
this.ShowInTaskbar = false; //System tray app - no taskbar icon
when minimized. It gets turned back on elsewhere.
}

protected override void WndProc(ref Message msg)
{
const int WM_SIZE = 0x0005;
const int SIZE_MINIMIZED = 1;

if ((msg.Msg == WM_SIZE) && ((int)msg.WParam == SIZE_MINIMIZED)
&& (this.Minimize != null))
{
this.Minimize(this, EventArgs.Empty);
}

base.WndProc(ref msg);
}
Any ideas?

Thanks!

My guess would be that setting This.ShowInTaskbar to False causes
another WM_SIZE message to be sent, which sets This.ShowInTaskbar to
False which causes another WM_SIZE message to be sent which ...

Perhaps you shouldn't call This.Minimize unless This.ShowInTaskbar is
True.
 
BillAtWork said:
Found some code to do this but seems to be causing a stack overflow due to
a
loop somewhere.

You could store the previous window state in a private variable and only
raise the event if the previous window state was not minimized.
 
Hi Bill,

Would you tell me what version of VS you're using?

I performed a test based on your sample code in VS.NET 2003, VS 2005 and VS
2008, but didn't produce the problem on my side.

I add a line of code "Console.Writeline("Minimized");" at the end of the
Minimize event handler of the form. When I run the test application and
minimize the form, the text "Minimized" are only printed to the Output
window twice, which means the Minimize event of the form is only fired for
two times. I got the same result in all the above three versions of VS.

In fact, when we change the ShowInTaskbar property of a form, the form is
recreated, i.e. the handle of the form is recreated.

I suggest that you set the ShowInTaskbar property of the form to false at
design time or in the form's Load event handler, rather than in the
Minimize event handler.

Hope this helps.
If the problem is still not solved, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,
It seems that the ShowInTaskBar was causing the loop. I've now changed this
whole section to trigger upon FormClose instead but I'm hitting another
somewhat related problem (to do with Minimize).

Quick description of what I'm aiming for:

1) Resizable Windows form is positioned over the system tray (i.e.
top=screenheight-windowheight, left=screenwidth-windowwidth), icon in System
Tray, button also appears in taskbar (while window is in Normal/Maximized
state).
2) User clicks Close button (effectively means "Minimise" in this scenario)
-> window minimises, button no longer in taskbar, icon in system tray.
3) Right click on icon -> context menu -> Choose "Open" -> Window returns to
Normal state, button appears in taskbar, icon still in System Tray.
4) User can now "Close" (i.e. minimize to system tray) and "Open" as often
as they wish.

The problem now is that, when I store the Form.Height and Form.Width (when
form is resized) these values CHANGE to reflect:

1) The dimension of the ICON in the System Tray! Or...
2) The dimensions of the taskbar button!

This was unexpected.

My resize event checks and only stores a change in height if in
WindowState.Normal but this does not work since the resize event fires at
weird times i.e, when user click Close and form minimises the values drop to
31, 29. When use clicks "Open", the taskbar button appear and dimensions are
31, 160 (the dimensions of the taskbar button). User clicks taskbar button
and dimensions drop to 31,29 and I see a tiny window!

Argh!
 
BTW I'm using the new "Tracepoints" in VS2008 to do as you did - print out
minimize events and window dimensions.
 
Hi Bill,

Thank you for your reply!

Firstly, I suggest that you hide the form instead of setting the
WindowState property of the form to Minimized when you "close" the
form(i.e. minimize to system tray). Thus, the Resize event of the form
won't be fired when you "close" the form. To hide a form, call the form's
Hide method. Note that you should call the form's Show method to show the
form again when you "open" the form.

Secondly, the Resize event of a form will be fired for many times when the
user resizes the form. I suggest that you use ResizeEnd event instead of
the Resize event. The ResizeEnd event will be triggered only once at the
end of a resizing operation.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you very much. Both these solutions, used together, have solved the
problem perfectly.
 
Back
Top