CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Oct 2006
    Posts
    43

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    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.
    Bogdan

    If someone helped you then please Rate his post and mark the thread as Resolved
    Please improve your messages appearance by using tags [ code] Place your code here [ /code]

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    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.

  4. #4
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Storing WindowState before a minimize

    I am not sure how to catch what the WindowState was before minimize was clicked.
    Declaration
    Code:
    	 int i;
    Store the window state
    Code:
             i = (int)this.WindowState;
    use the stored windowstate
    Code:
             this.WindowState = (FormWindowState)i;

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  6. #6
    Join Date
    Jun 2005
    Posts
    160

    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

  7. #7
    Join Date
    Oct 2006
    Posts
    43

    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

  8. #8
    Join Date
    Jun 2005
    Posts
    160

    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.

  9. #9
    Join Date
    Mar 2004
    Location
    Ukraine
    Posts
    170

    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
    God could improve essentially a
    human nature, but he
    was too anxious with compatibility
    with the monkey.
    (Eugeny Goldberg)

  10. #10
    Join Date
    Oct 2006
    Posts
    43

    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;
            }
        }
    }
    Attached Files Attached Files

  11. #11
    Join Date
    Jun 2005
    Posts
    160

    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.

    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.
    Attached Files Attached Files

  12. #12
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    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.

  13. #13
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  14. #14
    Join Date
    Oct 2006
    Posts
    43

    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

  15. #15
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured