How to draw a highlighted line?

I want to create an application that the user can manipulate the string that he draws. Something like deleting a line or selecting it. How should I do it?

Thanks in advance


I managed to do this using a hard-coded rectangle. But I still have no idea how to do this, using. drawLine()Can I use drawPathhits to perform the test?

Here is the code:

private bool selectGraph = false;
private Rectangle myrec = new Rectangle(50, 50, 100, 100);
private Graphics g;

private void panel1_Paint(object sender, PaintEventArgs e)
    {
        SolidBrush sb = new SolidBrush(Color.Blue);
        Pen p = new Pen(Color.Blue, 5);

        e.Graphics.DrawRectangle(p, myrec);
        e.Graphics.FillRectangle(sb, myrec);
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        Point mPT = new Point(e.X, e.Y);

        if (e.Button == MouseButtons.Left)
        {
            if (myrec.Contains(mPT))
            {
                selectGraph = true;
                button1.Enabled = true;
            }
            else
            {
                selectGraph = false;
                button1.Enabled = false;
            }
        }
        Invalidate();
    }
+3
source share
2 answers

Well, you can start with something like a simple class Line:

public class Line
{
    public Point Start { get; set; }
    public Point End { get; set; }
}

Then you can have your form:

private Line Line = new Line();

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawLine(Pens.Red, this.Line.Start, this.Line.End);
}

protected override void OnMouseMove(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.Line.Start = e.Location;
        this.Refresh();
    }
    else if (e.Button == MouseButtons.Right)
    {
        this.Line.End = e.Location;
        this.Refresh();
    }
}

, , this.Line, , "MiddleButton" - . , .

, , . , .

+2

. .

, . onmousedown , , /, . , . , .

- , .

0

Source: https://habr.com/ru/post/1762195/


All Articles