Draw line between point and cursor location

China☆狼群 提交于 2019-12-11 20:39:21

问题


I have a similar question to this one here: Drawing a line by mouse in a panel

And i've actually used the first solution in the list, with great success, by creating a UserControl called SuperPanel.

However, I want the model for this to be driven by something other than MouseDown and MouseUp on the SuperPanel. I want it to be set to either ON or OFF by calling a public method, which will be called by the event handler of another control like a button... Is that possible?

Every which way i've tried so far doesn't work and i can't seem to understand why. Here's the code i have so far:

SuperPanel Code

public partial class SuperPanel : Panel
{
    private Point _origin = Point.Empty;
    private Point _terminus = Point.Empty;
    private Boolean _draw = false;
    private List<Tuple<Point, Point>> _lines = new List<Tuple<Point, Point>>();
    public bool DrawLine {
        get { return this._draw; }
        set { this._draw = value; }
    }

    public SuperPanel()
    {
        InitializeComponent();
        Dock = DockStyle.Fill;
        DoubleBuffered = true;
    }

    public void StartDrawing(Point origin)
    {
        _draw = true;
        _origin = origin;
        _terminus = Point.Empty;
        Invalidate();
    }

    public void StopDrawing()
    {
        if (_draw && !_origin.IsEmpty && !_terminus.IsEmpty)
            _lines.Add(new Tuple<Point, Point>(_origin, _terminus));

        _draw = false;
        _origin = Point.Empty;
        _terminus = Point.Empty;
        Invalidate();
    }

    //the events on the control itself, in my mind, are no longer needed?
    //protected override void OnMouseDown(MouseEventArgs e)
    //{
    //    base.OnMouseDown(e);
    //    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    //    {
    //        _draw = true;
    //        _origin = e.Location;
    //    }
    //    else
    //    {
    //        _draw = false;
    //        _origin = Point.Empty;
    //    }

    //    _terminus = Point.Empty;
    //    Invalidate();
    //}

    //protected override void OnMouseUp(MouseEventArgs e)
    //{
    //    base.OnMouseUp(e);
    //    if (_draw && !_origin.IsEmpty && !_terminus.IsEmpty)
    //        _lines.Add(new Tuple<Point, Point>(_origin, _terminus));
    //    _draw = false;
    //    _origin = Point.Empty;
    //    _terminus = Point.Empty;
    //    Invalidate();
    //}

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if (_draw)
            _terminus = e.Location;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        foreach (var line in _lines)
            e.Graphics.DrawLine(Pens.Blue, line.Item1, line.Item2);
        if (!_origin.IsEmpty && !_terminus.IsEmpty)
            e.Graphics.DrawLine(Pens.Red, _origin, _terminus);
    }
}

Main Winforms Event

    void MyCtrl1_OnVectorClicked(MyToken sender, Point MyLocation)
    {
        //If we are not already tracking a token vector...
        if (this.ActiveToken == null)
        {
            this.ActiveToken = sender;
            this.ActiveSourcePoint = MyLocation;
            this.panel1.DrawLine = true;
            this.panel1.StartDrawing(MyLocation);

        }
        else
        {
            //Stop tracking the vector and stop drawing the line...
        }
    }

Yet this simply doesn't work. Calling the StartDrawing method does absolutely nothing. Can you tell me why?

来源:https://stackoverflow.com/questions/26720642/draw-line-between-point-and-cursor-location

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!