locked
How to detect user press minimum size on Winform? RRS feed

  • Question

  • Hi, 

    I created a form that does not contain a close button, so I'm looking for the event of this detection so I can add the maximum button back to the screen, How do i do this?
    Thanks a lot!

    Sunday, May 3, 2020 3:50 PM

Answers

  • Add this to the form and replace the Console.WriteLine with your logic.

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_MINIMIZE = 0xF020;
    private const int SC_MAXIMIZE = 0xF030;
    
    [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)
                {
    
                }
                else if (command == SC_MAXIMIZE)
                {
    
                }
                // If you don't want to do the default action then break
                break;
        }
        base.WndProc(ref m);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Sunday, May 3, 2020 5:12 PM
  • Hi,

    We can use "SizeChanged" event handler.

    // SizeChanged event handler
    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            Console.WriteLine("form has been minimized");
        }
        else if (this.WindowState == FormWindowState.Maximized)
        {
            Console.WriteLine("form has been maximized");
        }
    }

    Regards,



    Ashidacchi -- http://hokusosha.com

    • Marked as answer by Khanh dang Monday, May 4, 2020 6:13 AM
    Monday, May 4, 2020 12:43 AM

All replies

  • Add this to the form and replace the Console.WriteLine with your logic.

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_MINIMIZE = 0xF020;
    private const int SC_MAXIMIZE = 0xF030;
    
    [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)
                {
    
                }
                else if (command == SC_MAXIMIZE)
                {
    
                }
                // If you don't want to do the default action then break
                break;
        }
        base.WndProc(ref m);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Sunday, May 3, 2020 5:12 PM
  • Hi,

    We can use "SizeChanged" event handler.

    // SizeChanged event handler
    private void Form1_SizeChanged(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            Console.WriteLine("form has been minimized");
        }
        else if (this.WindowState == FormWindowState.Maximized)
        {
            Console.WriteLine("form has been maximized");
        }
    }

    Regards,



    Ashidacchi -- http://hokusosha.com

    • Marked as answer by Khanh dang Monday, May 4, 2020 6:13 AM
    Monday, May 4, 2020 12:43 AM
  • Thanks to the Experts!, these answers helped me a lot.
    Monday, May 4, 2020 6:14 AM