Click to See Complete Forum and Search --> : Storing WindowState before a minimize
badwolf500
October 15th, 2006, 10:04 AM
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.
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
WindowState = ?????
}
Any help appreciated. I am using Visual Studio 2005.
Thanks,
badwolf500
creatorul
October 15th, 2006, 10:46 AM
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.
aniskhan
October 15th, 2006, 08:34 PM
//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.
aniskhan
October 15th, 2006, 08:51 PM
I am not sure how to catch what the WindowState was before minimize was clicked.
Declaration
int i;
Store the window state
i = (int)this.WindowState;
use the stored windowstate
this.WindowState = (FormWindowState)i;
MadHatter
October 15th, 2006, 09:48 PM
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.
naveenl
October 17th, 2006, 03:22 AM
When application gets minimized add this code:
ShowInTaskBar = false;
NotifyIcon1.Visible = true;
In NotifyIcon double click event handler add this code:
this.WindowState = FormWindowState.Normal;
NotifyIcon1.Visible = false;
this.Activate();
ShowInTaskbar = true;
I think this should solve your problem
badwolf500
October 17th, 2006, 05:21 PM
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
naveenl
October 18th, 2006, 12:27 AM
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.
Skoons
October 18th, 2006, 04:14 AM
Try to use not only window state but window size and when you will restore state, you may change the size :)
badwolf500
October 18th, 2006, 05:48 AM
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
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;
}
}
}
naveenl
October 18th, 2006, 09:15 AM
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.
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTMINBUTTON = 8;
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
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.
zips
October 18th, 2006, 09:51 AM
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.
MadHatter
October 18th, 2006, 10:33 AM
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.
badwolf500
October 18th, 2006, 04:32 PM
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
zips
October 18th, 2006, 04:48 PM
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.
badwolf500
October 19th, 2006, 05:16 AM
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.
I wan't disagreeing with you. It just seemed strange to me that using the normal state would not automatically pick up if the last state was a 'normal' one or max size.
Again, thanks for the help.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.