0

I have a following situation.

In my scenario, I have a borderless form, where a panel is docked on top , left and right. For this scenario I first wanted to drag the form using the top bar which I was able to after listening to the top panel mouse move event .

private void panel4_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
    }




public DetectionSegmentation()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);

        }

 
    private const int
        HTLEFT = 10,
        HTRIGHT = 11,
        HTTOP = 12,
        HTTOPLEFT = 13,
        HTTOPRIGHT = 14,
        HTBOTTOM = 15,
        HTBOTTOMLEFT = 16,
        HTBOTTOMRIGHT = 17;

    const int _ = 10; // you can rename this variable if you like

    Rectangle Top { get { return new Rectangle(0, 0, this.ClientSize.Width, _); } }
    Rectangle Left { get { return new Rectangle(0, 0, _, this.ClientSize.Height); } }
    Rectangle Bottom { get { return new Rectangle(0, this.ClientSize.Height - _, this.ClientSize.Width, _); } }
    Rectangle Right { get { return new Rectangle(this.ClientSize.Width - _, 0, _, this.ClientSize.Height); } }

    Rectangle TopLeft { get { return new Rectangle(0, 0, _, _); } }
    Rectangle TopRight { get { return new Rectangle(this.ClientSize.Width - _, 0, _, _); } }
    Rectangle BottomLeft { get { return new Rectangle(0, this.ClientSize.Height - _, _, _); } }
    Rectangle BottomRight { get { return new Rectangle(this.ClientSize.Width - _, this.ClientSize.Height - _, _, _); } }


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

        if (message.Msg == 0x84) // WM_NCHITTEST
        {
            var cursor = this.PointToClient(Cursor.Position);

            if (TopLeft.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT;
            else if (TopRight.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT;
            else if (BottomLeft.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT;
            else if (BottomRight.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT;

            else if (Top.Contains(cursor)) message.Result = (IntPtr)HTTOP;
            else if (Left.Contains(cursor)) message.Result = (IntPtr)HTLEFT;
            else if (Right.Contains(cursor)) message.Result = (IntPtr)HTRIGHT;
            else if (Bottom.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM;
        }
    }

Now I want to resize the form and for this I have added above lines of code, it was working well without the panels in the form but now after the panels have been added to the form, the resize code does not work. Moreover, I want to resize the form to a specific width and height of the form.

Form with top , left and right panels

Naseer
  • 4,041
  • 9
  • 36
  • 72
  • 1
    It those panels you added that now get the WM_NCHITTEST message. Best to not use them. [Second best](https://stackoverflow.com/a/31357074/17034). – Hans Passant Dec 09 '22 at 19:49
  • If it is best not to use them and I still need to resize the form then is there any other way that I can use, Do you have some suggestions? . Actually I need to have some panels and place controls inside them so is there any suggestion for my scenario Hans? – Naseer Dec 09 '22 at 20:25
  • 1
    One other basic thing you can do is set the form's Padding property to `8, 8, 8, 8`. That leaves enough room around the edges of the form that the user can hit with the mouse. – Hans Passant Dec 09 '22 at 22:01
  • @HansPassant thats so elegant , so somple and so easy way. Thanks a lot. – Naseer Dec 09 '22 at 22:03

0 Answers0