'c# how to add resize button squares?

Hi i want to make a square to resize button like from normal button to this: https://prnt.sc/ESy2d63JMgRv if you know how to do resizing squares to resize top, left, right and bottom of button by dragging mouse please let me know



Solution 1:[1]

I could not fully understand whether you want to make such a change while the application is running or in the designer, neither the link you shared above nor the question is fully explanatory, but I tried to write something about how this can be done while the application is running.

I'm writing this to give you some idea of ??how to do it, obviously the code below doesn't solve your specific problem.

        private int squareSize = 5;
        private bool isDragging = false;
        private bool onlyX = false;
        private bool onlyY = false;
        private bool bothXY = false;

You need to draw squares on the corners and edges of the button.

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;

            Rectangle rect1 = new Rectangle(new Point(button1.Left - squareSize, button1.Top - squareSize), new Size(squareSize, squareSize));
            Rectangle rect2 = new Rectangle(new Point(button1.Right - (button1.Right - button1.Left) / 2, button1.Top - squareSize), new Size(squareSize, squareSize));
            Rectangle rect3 = new Rectangle(new Point(button1.Right, button1.Top), new Size(squareSize, squareSize));
            Rectangle rect4 = new Rectangle(new Point(button1.Left - squareSize, button1.Bottom - (button1.Bottom - button1.Top) / 2), new Size(squareSize, squareSize));
            Rectangle rect5 = new Rectangle(new Point(button1.Right, button1.Bottom - (button1.Bottom - button1.Top) / 2), new Size(squareSize, squareSize));
            Rectangle rect6 = new Rectangle(new Point(button1.Left - squareSize, button1.Bottom - squareSize), new Size(squareSize, squareSize));
            Rectangle rect7 = new Rectangle(new Point(button1.Left + (button1.Right - button1.Left) / 2, button1.Bottom), new Size(squareSize, squareSize));
            Rectangle rect8 = new Rectangle(new Point(button1.Right, button1.Bottom - squareSize), new Size(squareSize, squareSize));

            // copy the left, right, top and bottom coordinates of the rectangles above.
            // draw the rectangles.
        }

You need to write the mouse down event to determine which corner or edge it was grabbed and dragged (For example, the first and eighth squares act on the x=-y line, while the third and sixth squares act on the x=y line, with that in mind I suggest you write different boolean values so you can differentiate when resizing the button.), something like this:

 private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            // Dragging first rectangle, changes on both axis
            if (e.Location.X < rect1Right && e.Location.X > rect1Left && e.Location.Y < rect1Bottom && e.Location.Y > rect1Top && e.Button == MouseButtons.Left)
            {
                bothXY = true;
                isDragging = true;
            }
            // Dragging second rectangle, changes on only y-axis
            if (e.Location.X < rect2Right && e.Location.X > rect2Left && e.Location.Y < rect2Bottom && e.Location.Y > rect2Top && e.Button == MouseButtons.Left)
            {
                onlyY = true;
                isDragging = true;
            }
        }

You should write a mouse move event function to determine the direction and amount of movement on each axis. (I'm not sure if you should do the resizing inside that function, give it a try.)

private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging && bothXY)
            {
                resizeX = e.X - button1.Right;
                resizeY = e.Y - button1.Bottom;
            }
            if(isDragging && onlyY)
            {
                resizeY = e.Y - button1.Bottom;
            }

            button1.Width += resizeX;
            button1.Height += resizeY;
            this.Invalidate();
        }

And finally you should write a mouse up event where you reset all boolean values to initials to stop resizing the button.

private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
            onlyY = false;
            onlyX = false;
            bothXY = false;
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1