Storing WindowState before a minimize
Hi,
When I minimize my application I hide the form and make a notifyicon visible. When I double click the notifyicon I want the application to return to its original size. I am not sure how to catch what the WindowState was before minimize was clicked.
I am using the code below to handle the double click but I am unsure how to set the WindowState. If I don't set the WindowState the form does not regain focus and just stays in the taskbar and the notifyicon also remains in the system tray.
Code:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
WindowState = ?????
}
Any help appreciated. I am using Visual Studio 2005.
Thanks,
badwolf500
Re: Storing WindowState before a minimize
Code:
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
If you want to know what was the last window state you retain it in a private FormWindowState enum.
Re: Storing WindowState before a minimize
Code:
//Make visible the notifyIcon which is false initially
private void Button1_Click_1(object sender, System.EventArgs e)
{
this.Hide();
NotifyIcon1.Visible = true;
}
//Make notifyIcon visible false
private void NotifyIcon1_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.Show();
NotifyIcon1.Visible = false;
}
Whereas form window retains its previous size.
Re: Storing WindowState before a minimize
Quote:
I am not sure how to catch what the WindowState was before minimize was clicked.
Declaration
Store the window state
Code:
i = (int)this.WindowState;
use the stored windowstate
Code:
this.WindowState = (FormWindowState)i;
Re: Storing WindowState before a minimize
window state is either maximized or normal. if it was maximized before it was minimized, then setting it to normal should restore it to its last position.
Re: Storing WindowState before a minimize
When application gets minimized add this code:
Code:
ShowInTaskBar = false;
NotifyIcon1.Visible = true;
In NotifyIcon double click event handler add this code:
Code:
this.WindowState = FormWindowState.Normal;
NotifyIcon1.Visible = false;
this.Activate();
ShowInTaskbar = true;
I think this should solve your problem
Re: Storing WindowState before a minimize
Thanks for all your replies but it does not seem to be working for me.
When I restore using "this.WindowState = FormWindowState.Normal;" the window always goes to interim size regardless of the size before it was minimized. Weird ...
If I don't use the notifyicon in the system tray and just leave it in the taskbar it works fine so it seems my notifyicon is causing this problem.
Thanks,
badwolf500
Re: Storing WindowState before a minimize
Can you please post the code for how are you catching MINIMIZE event and what are you doing in that event handler. Also code of NotifyIcon double click event handler.
Re: Storing WindowState before a minimize
Try to use not only window state but window size and when you will restore state, you may change the size :)
1 Attachment(s)
Re: Storing WindowState before a minimize
Hi,
I have attached a sample project that shows this problem. It has been compiled using VS2005. If anyone could spot the problem I would be grateful.
For anyone who doesn't have VS2005 this is my code
Thanks
Code:
amespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
this.Activate();
ShowInTaskbar = true;
}
}
}
1 Attachment(s)
Re: Storing WindowState before a minimize
First override WndProc function and check for NON CLIENT AREA BUTTON DOWN(i.e WM_NCLBUTTONDOWN) message and check whether mouse down position was indeed on MINBUTTON.
Add FormWindowState variable to keep track of previous WindowState. Also you need to declare two more constant values which are WIN32 defines for WM_NCLBUTTONDOWN and HTMINBUTTON messages.
Code:
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTMINBUTTON = 8;
Code:
protected override void WndProc(refMessage m)
{
if (m.Msg == WM_NCLBUTTONDOWN && (int)m.WParam == HTMINBUTTON)
{
prevWindowState = WindowState;
}
base.WndProc(ref m);
}
In notifyicon double click event assign previously stored WindowState value to current WindowState.
If we had WindowStateChanged event then we could have avoided all these things.:D
Quote:
window state is either maximized or normal. if it was maximized before it was minimized, then setting it to normal should restore it to its last position.
As MadHatter said this is true in .NET Framework 1.1. But in 2.0 they have distinguished between NORMAL and MAXIMIZED.
Re: Storing WindowState before a minimize
In the Form1 constructor you are setting the state to Maximized. In notifyIcon1_MouseDoubleClick you are setting the state to Normal. These are not the same thing, so you wouldn't get the same result. Normal is the last non-maximized, non-minimized size, or the designer size.
Re: Storing WindowState before a minimize
open notepad. maximize it, click the minimize button, then from the task bar, select restore. see what happens. it goes back to the last way it was. this is the same thing that happens by setting the window state w/ normal after its been minimized. but IMO naveen's solution is the one I'd do.
Re: Storing WindowState before a minimize
Quote:
Originally Posted by MadHatter
open notepad. maximize it, click the minimize button, then from the task bar, select restore. see what happens. it goes back to the last way it was. this is the same thing that happens by setting the window state w/ normal after its been minimized. but IMO naveen's solution is the one I'd do.
Naveen's method works perfectly - thanks again Naveen.
Just out of interest, MadHatter, notepad does what I want my app to do. Do you think using normal should do what I want? I thought normal was the last state before it was minimized but it just does not seem to work. zips description seems to make sense but it is strange their is no easier way to do this apparently simple task.
Thanks,
badwolf500
Re: Storing WindowState before a minimize
Actually, it makes perfectly good sense: Maximize has no need for size values, and Minimize has no need for size values. Only Normal would use size values, so only the size from the last Normal state is stored.