0

I'm drawing a line on a Form and when it finished will be draw a rectangle on line. I need to resize the line after clicking on the rectangle. Any way to do that?

This before:

enter image description here

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawLine(Pens.Red, start.X, start.Y, end.X, end.Y);
    if (isMouse == false  )
    {
        rect1.X = start.X - 5;
        rect1.Y = start.Y - 5;
        rect1.Width = 10;
        rect1.Height = 10;
        rect2.X = end.X - 5;
        rect2.Y = end.Y - 5;
        rect2.Width = 10;
        rect2.Height = 10;
        e.Graphics.DrawRectangle(Pens.Blue, rect1.X, rect1.Y,rect1.Width, rect1.Height);
        e.Graphics.DrawRectangle(Pens.Blue, rect2.X, rect2.Y, rect2.Width, rect2.Height);
    }
    base.OnPaint(e);
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (start.X == 0 && start.Y == 0)
    {
        isMouse = true;
        start = e.Location;
    }
    else {
        resize = true;
    }
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (isMouse == true) {
        end = e.Location;
    }
    if (rect1.Contains(e.Location)&&resize ) {
        start = e.Location;
    }
    else if (rect2.Contains(e.Location)&&resize) {
        end = e.Location;
    }
    Refresh();
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    if (isMouse == true)
    {
        end = e.Location;
        isMouse = false;
    }
}

This will be after:

enter image description here

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • You need to store you Line object as a class object (a Shape) that contains all the information needed in relation to your sizing handles (the rectangular shapes). When the mouse pointer is moved on the drawing surface, you can then determine whether one of the handles contains the mouse position (`Rectangle.Contain(Point)`). You can then redraw that shape and, maybe, change it's Color and/or the Pen line (e.g., draw a dashed line instead of a solid one). When a handle is selected, you don't draw a new shape, move the location of one of the handles an redraw the line of the same Shape object. – Jimi Oct 12 '20 at 08:01
  • see [Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?](https://stackoverflow.com/a/20924609/2521214) – Spektre Oct 12 '20 at 08:32

0 Answers0