Resize to "Normal" from Maximized fails with ClientSize reset to {0,0)!?

M

Mailing Lists

The problem: WinForms app with main form and one NotifyIcon (called
sysTrayIcon in my code). Main form starts out minimized with ShowInTaskBar
= False. Double clicking on the system tray icon displays the form. All
looks/works well except....

If you:
[1] start the app
[2] double-click on the system tray icon to display the ("normal" sized)
main form
[3] maximize the main form
[4] minimize the main form
[5] double-click on the system tray icon to re-display the (still
maximized) main form
[6] click the "normal size" button (between minimize and close in the
title bar)

the main form now displays nothing more than the title bar, as if there were
no client area at all. What the heck is that?!

Looking at the messages going through the message pump it looks like the
main form is being sized correctly the first time, but is then being resized
down to 0,0 twice. Why is that happening???

Here's the custom bits to add to the default WinForms app:

private void Form1_Load( object sender, System.EventArgs e )
{
this.WindowState = FormWindowState.Minimized ;
this.ShowInTaskbar = false ;
}

private void sysTrayIcon_DoubleClick( object sender, System.EventArgs e )
{
this.ShowInTaskbar = true ;
this.WindowState = FormWindowState.Normal ;
}

private void Form1_Deactivate( object sender, System.EventArgs e )
{
if( this.WindowState == FormWindowState.Minimized )
{
this.ShowInTaskbar = false ;
}
}

This has me puzzled. I'm not much of a UI guy (as if you couldn't tell).
The cause *is* related to my code (shown above) because the default WinForms
app doesn't do this (already checked).

Many thanks in advance for any help you can offer.

-Jason
 
M

Mailing Lists

Apparently, this is a known issue (!?). The work-around is to override the
WndProc method and handle persisting the client area manually. The code
follows, (derived from this thread:
http://groups.google.com/groups?hl=...-8&selm=utFEgQAJCHA.2240%40tkmsftngp13&rnum=4)
with critical insight from
(http://groups.google.com/groups?q=winform+restore&start=10&hl=en&lr=&ie=UTF
-8&oe=UTF-8&selm=BU%23mdf4ECHA.1380%40cpmsftngxa07&rnum=20). The folks in
these posts did all the heavy lifting; the following is theirs, just
refactored a bit.

protected const int WM_NCLBUTTONDBLCLK = 0xA3;
protected const int WM_SYSCOMMAND = 0x0112;
protected const int SC_MINIMIZE = 0xF020;
protected const int SC_MAXIMIZE = 0xF030;
protected const int SC_RESTORE = 0xF120;

private Size restoreClientSize ;

protected override void WndProc( ref Message m )
{
//handle min/max/restore buttons
if( m.Msg == WM_SYSCOMMAND )
{
int wParam = (int)m.WParam ; //cast/unbox once up front
if( this.WindowState == FormWindowState.Normal )
{
if(( wParam == SC_MINIMIZE ) || ( (wParam == SC_MAXIMIZE )))
{
//Console.WriteLine( @"Save position!" );
restoreClientSize = this.ClientSize ;
}
}
else
{
if( wParam == SC_RESTORE )
{
//Console.WriteLine( @"Restore position!" );
this.ClientSize = restoreClientSize ;
}
}
}
//handle double-click in caption area (maximize/restore window)
if( m.Msg == WM_NCLBUTTONDBLCLK )
if( this.WindowState == FormWindowState.Normal )
{
//Console.WriteLine( @"Save position!" );
restoreClientSize = this.ClientSize ;
}
else
{
//Console.WriteLine( @"Restore position!" );
this.ClientSize = restoreClientSize ;
}
//call the base class' method
base.WndProc (ref m);
}


Mailing Lists said:
The problem: WinForms app with main form and one NotifyIcon (called
sysTrayIcon in my code). Main form starts out minimized with ShowInTaskBar
= False. Double clicking on the system tray icon displays the form. All
looks/works well except....

If you:
[1] start the app
[2] double-click on the system tray icon to display the ("normal" sized)
main form
[3] maximize the main form
[4] minimize the main form
[5] double-click on the system tray icon to re-display the (still
maximized) main form
[6] click the "normal size" button (between minimize and close in the
title bar)

the main form now displays nothing more than the title bar, as if there were
no client area at all. What the heck is that?!

Looking at the messages going through the message pump it looks like the
main form is being sized correctly the first time, but is then being resized
down to 0,0 twice. Why is that happening???

Here's the custom bits to add to the default WinForms app:

private void Form1_Load( object sender, System.EventArgs e )
{
this.WindowState = FormWindowState.Minimized ;
this.ShowInTaskbar = false ;
}

private void sysTrayIcon_DoubleClick( object sender, System.EventArgs e )
{
this.ShowInTaskbar = true ;
this.WindowState = FormWindowState.Normal ;
}

private void Form1_Deactivate( object sender, System.EventArgs e )
{
if( this.WindowState == FormWindowState.Minimized )
{
this.ShowInTaskbar = false ;
}
}

This has me puzzled. I'm not much of a UI guy (as if you couldn't tell).
The cause *is* related to my code (shown above) because the default WinForms
app doesn't do this (already checked).

Many thanks in advance for any help you can offer.

-Jason
 

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