通过鼠标调整线条大小

时间:2020-10-12 06:14:11

标签: c# winforms graphics

我在窗体上画一条线,完成后将在网上画一个矩形。单击矩形后,我需要调整线条的大小。有什么办法吗?

此之前:

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;
    }
}

这将在之后:

enter image description here

0 个答案:

没有答案
相关问题