Why bother testing to see what state the window is currently in?
Just set e.Cancel
to true
and set the state to minimized.
=== update ===
I created a new Windows Forms project (tested both 4.0 and 3.5 frameworks, using VS2010 and VS2012).
From there I added the notify icon to Form1 and set the doubleclick event (code below) and the icon to some random icon file I had. I also set the forms formclosing event (code below):
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_FormClosing( object sender, FormClosingEventArgs e ) {
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void notifyIcon1_MouseDoubleClick( object sender, MouseEventArgs e ) {
this.WindowState = FormWindowState.Normal;
}
}
Then I ran the project. When I clicked the close button, the form was hidden. When I double clicked the notify icon it came back. I'm on Windows 7, but that shouldn't matter.
Now, I did find that if I made Form1 a child form, then some weirdness occurred. But I'm not entirely sure that's what you are doing.