Question

I am using a borderless form and implemented the form with HTCAPTION so that it can be dragged around. Another benefit of HTCAPTION is that when doubled cliked, it can minimize and maximize. The only problem is that I want to catch an event so that when the window is maximized, I can change the icon of a button from maximize button to normal mode button. Any great ideas on how to implement this?

protected override void WndProc(ref Message m) { base.WndProc(ref m);

        switch(m.Msg)
        {
            case Global.WM_NCHITTEST:
                Point mouseCursor = PointToClient(Cursor.Position);

                if (mouseCursor.X < borderSize && mouseCursor.Y < borderSize)
                    m.Result = (IntPtr)Global.HTTOPLEFT;
                else if (mouseCursor.X < borderSize && mouseCursor.Y > Height - borderSize)
                    m.Result = (IntPtr)Global.HTBOTTOMLEFT;
                else if (mouseCursor.X < borderSize)
                    m.Result = (IntPtr)Global.HTLEFT;
                else if (mouseCursor.X > Width - borderSize && mouseCursor.Y < borderSize)
                    m.Result = (IntPtr)Global.HTTOPRIGHT;
                else if (mouseCursor.X > Width - borderSize && mouseCursor.Y > Height - borderSize)
                    m.Result = (IntPtr)Global.HTBOTTOMRIGHT;
                else if (mouseCursor.X > Width - borderSize)
                    m.Result = (IntPtr)Global.HTRIGHT;
                else if (mouseCursor.Y < borderSize)
                    m.Result = (IntPtr)Global.HTTOP;
                else if (mouseCursor.Y > Height - borderSize)
                    m.Result = (IntPtr)Global.HTBOTTOM;
                else
                    m.Result = (IntPtr)Global.HTCAPTION;
                break;
        }
    }

This is the code I want to add in the catched event

        if (WindowState == FormWindowState.Maximized)
        {
            WindowState = FormWindowState.Normal;
            pb_max.Image = GomeeSoft.Properties.Resources.buttonmax;
        }
        else
        {
            Screen screen = Screen.FromRectangle(new Rectangle(Left, Top, Width, Height));
            this.MaximumSize = screen.WorkingArea.Size;
            WindowState = FormWindowState.Maximized;
            pb_max.Image = GomeeSoft.Properties.Resources.buttonreturn;
        }
Was it helpful?

Solution

Double-clicking generates WM_SYSCOMMAND messages. Which you can detect with WndProc as well:

    private const int WM_NCHITTEST = 0x84;
    private const int WM_SYSCOMMAND = 0x112;
    private const int SC_MINIMIZE = 0xf020;
    private const int SC_MAXIMIZE = 0xf030;
    private const int SC_RESTORE = 0xf120;

    protected override void WndProc(ref Message m) {
        if (m.Msg == WM_SYSCOMMAND) {
            switch (m.WParam.ToInt32() & 0xfff0) {
                case SC_MINIMIZE: Console.WriteLine("Minimize"); break;
                case SC_MAXIMIZE: Console.WriteLine("Maximize"); break;
                case SC_RESTORE:  Console.WriteLine("Restore");  break;
            }
        }
        base.WndProc(ref m);
        if (m.Msg == WM_NCHITTEST && m.Result == (IntPtr)1) m.Result = (IntPtr)2;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top