-1

I am using this code to check if the form is minimized:

[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case WM_SYSCOMMAND:
            int command = m.WParam.ToInt32() & 0xfff0;
            if (command == SC_MINIMIZE)
                MessageBox.Show("Minimized");
                Variaveis.telaMinimizada = true;
            else
                Variaveis.telaMinimizada = false;
                MessageBox.Show("Maximized");
            break;
    }
    base.WndProc(ref m);
}

This code works like a charm. When I click on minimize button, appear the message "minimized", and when I reopen the application, appear the message "maximized"

But there is a problem. Not always people minimize the form by clicking on minimize button. I mean, if I click on screen OUT the form, the form will also minimize, and when this happens, the code I have do not detect that the form got minimized.

How can I check if the form is minimized (or is invisible on screen) when the form gets minimized after clicking OUT the form?

Ideas? Thanks!

Edit: I already tried doing what is recommended on this post, but do not work:

How to detect when a windows form is being minimized?

Liam
  • 27,717
  • 28
  • 128
  • 190
Gabriel Weiss
  • 21
  • 1
  • 7
  • This is a duplicate question: https://stackoverflow.com/questions/1052913/how-to-detect-when-a-windows-form-is-being-minimized – Ves Sep 28 '18 at 14:37
  • @Ves, not duplicated, i tried doing what the post u coment recomended, and nto work – Gabriel Weiss Sep 28 '18 at 14:39
  • "if i click on screen OUT the form, the form will also minimize" - do you mean you have programmed it to do this? Or do you mean that it stops being the active window? [This might help](https://stackoverflow.com/questions/7162834/determine-if-current-application-is-activated-has-focus) – ProgrammingLlama Sep 28 '18 at 14:40
  • @John, I mean when u click OUT the form, the form get minimized and also stop being active windows, so the code i post do not work – Gabriel Weiss Sep 28 '18 at 14:42
  • @Eugene, i already edited my post telling that this question u posted do not solved my problem – Gabriel Weiss Sep 28 '18 at 14:43
  • So you've programmed it to minimize when you click away (since this is not standard Windows behaviour)? Surely you can just record somewhere if you've done that or if it's been unminimized? – ProgrammingLlama Sep 28 '18 at 14:44
  • @John, I do not programmed it to minimize when click away. The only programming i made is the code i posted... Do you know how can i program to minimize when click OUT the form? – Gabriel Weiss Sep 28 '18 at 14:46
  • Maybe [WM_SHOWWINDOW](https://learn.microsoft.com/en-us/windows/desktop/winmsg/wm-showwindow) – 001 Sep 28 '18 at 14:46
  • @JohnnyMopp, Im really new to c#, can you help me to make the code we find on the site u posted? – Gabriel Weiss Sep 28 '18 at 14:50
  • 1
    What do you count as "minimized"? A minimized window no longer exists as a Window that you can see, even if you minimize all other open Windows. The only reference you can find to it is in the taskbar (not factoring in things like alt+tab, etc.). There is no default behaviour to minimize an WinForms form to the taskbar. I really don't understand how your application is getting minimized by simply clicking away. – ProgrammingLlama Sep 28 '18 at 14:59
  • NM. I just tried it and it doesn't seem to work. – 001 Sep 28 '18 at 15:03
  • 2
    @GabrielWeiss I think there is some confusion here. When you click another window, your window is not minimised. It just stops being the active window and if the window you clicked occupies the same region of the screen as your window, you window will be behind it.. but not minimised – Dave Sep 28 '18 at 15:05
  • @Dave, Thanks for the explanations. Do you know how can i make a code that check if the windows is not active windows anymore? – Gabriel Weiss Sep 28 '18 at 15:12
  • 1
    @John, i think the correct word here is not minimized... when i click on screen OUT the form, the form minimize (in other word, the form is not active windows anymore)... Open google chrome page, put it in half-screen, and click out the google chrome screen. It will "minimize" Chrome. – Gabriel Weiss Sep 28 '18 at 15:14
  • 1
    @Dave that's the confusion I've been trying to clear up, and why I linked to the question about checking if a window is active. If OP also wants to check if the window isn't minimized but is fully occluded by other Windows, then that might be complicated. It seems to me that basic window state changes can be handled by the Resize, Activate and Deactivate events without even hooking into the window message loop. – ProgrammingLlama Sep 28 '18 at 15:15
  • Gabriel look at the events I suggested in my comment above. I'd add more but I must sleep now, I'm sorry. – ProgrammingLlama Sep 28 '18 at 15:16
  • 1
    Maybe you want: [Form.Deactivate Event](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.deactivate?view=netframework-4.7.2) – 001 Sep 28 '18 at 15:17

1 Answers1

4

this might work for you

//we create a variable to store our window's last state
FormWindowState lastState;
public Form2()
{
    InitializeComponent();

    //then we create an event for form size changed
    //i did use lambda for creating event but you can use ordinary way.
    this.SizeChanged += (s, e) =>
    {

        //when window size changed we check if current state
        //is not the same with the previous
        if (WindowState != lastState)
        {

            //i did use switch to show all 
            //but you can use if to get only minimized status
            switch (WindowState)
            {
                case FormWindowState.Normal:
                    MessageBox.Show("normal");
                    break;
                case FormWindowState.Minimized:
                    MessageBox.Show("min");
                    break;
                case FormWindowState.Maximized:
                    MessageBox.Show("max");
                    break;
                default:
                    break;
            }
            //and at the and of the event we store last window state in our
            //variable so we get single message when state changed.
            lastState = WindowState;
        }
    };
}

Edit: and to check if form is not on top anymore you can override OnLostFocus like so

protected override void OnLostFocus(EventArgs e)
{
MessageBox.Show("form not on top anymore");
base.OnLostFocus(e);
this.Focus();
}
trksyln
  • 136
  • 10