how to detect resize

Jordan Halgunseth 1 Reputation point
2022-04-21T16:48:50.693+00:00

How do I detect when screen is sized(minimize is used).

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-04-21T19:24:23.35+00:00

    Here you go

    using System;
    using System.Security.Permissions;
    using System.Windows.Forms;
    
    namespace Demo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Resize += OnResize;
            }
    
            private void OnResize(object sender, EventArgs e)
            {
                if (WindowState == FormWindowState.Minimized)
                {
                    Console.WriteLine(nameof(OnResize));
                }
            }
    
            private const int WM_SYSCOMMAND = 0x0112;
            private const int SC_MINIMIZE = 0xF020;
    
            [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)
                        {
                            Console.WriteLine(nameof(WndProc));
                        }
                        // If you don't want to do the default action then break
                        break;
                }
    
                base.WndProc(ref m);
            }
        }
    }
    
    0 comments No comments