简体   繁体   English

用户在Visual Studio C#中绘制一条直线以及随着鼠标移动的直线?

[英]Drawing a straight line in visual studio C# by the user along with the line moving along with the mouse?

the user should be able to draw a straight line on a panel similar to drawing a straight line in paint . 用户应该能够在面板上画直线,类似于在油漆中画直线。

the user clicks on the panel and when he moves the mouse the line should also move along with the mouse (ie similar to drawing a staright line in paint) and when the user releases the mouse the line should have been drawn from the original point of click to this release point . 用户单击面板时,当他移动鼠标时,该线也应与鼠标一起移动(即类似于在油漆中绘制星形线),并且当用户释放鼠标时,该线应从鼠标的原始点绘制单击此发布点。

ie not a free hand line. 即不是自由线。

is there any animation for this ? 为此有动画吗?

How about this? 这个怎么样? :

public class LinePanel : Panel
{
    public LinePanel()
    {
        this.MouseDown += (src, e) => { LineStartPos = LineEndPos = e.Location; Capture = true; Invalidate(); };
        this.MouseMove += (src, e) => { if (Capture) { LineEndPos = e.Location; Invalidate(); } };
        this.MouseUp += (src, e) => { if (Capture) { LineEndPos = e.Location; } Capture = false; Invalidate(); };
    }

    private Point LineStartPos, LineEndPos;

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

        if (LineStartPos != LineEndPos)
            e.Graphics.DrawLine(new Pen(Color.Black, 2), LineStartPos, LineEndPos);
    }
}

To test you can just add a new LinePanel() to the Controls collection of your form, and set location/size or anchor / dock paramaters to size it. 要进行测试,您只需向表单的Controls集合添加一个新的LinePanel(),然后设置位置/大小或锚定/停靠参数即可为其设置大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM