【问题标题】:Drawing a straight line in visual studio C# by the user along with the line moving along with the mouse?用户在 Visual Studio C# 中绘制一条直线以及随鼠标移动的直线?
【发布时间】:2016-10-26 22:53:10
【问题描述】:

用户应该能够在面板上绘制一条直线,类似于在 paint 中绘制一条直线。

用户点击面板,当他移动鼠标时,线条也应该随着鼠标移动(即类似于在油漆中绘制一条直线),当用户释放鼠标时,线条应该是从原点点击到这个发布点。

即不是徒手线。

这个有动画吗?

【问题讨论】:

    标签: c# visual-studio user-interface


    【解决方案1】:

    这个怎么样? :

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

    要进行测试,您只需将新的 LinePanel() 添加到表单的 Controls 集合中,然后设置位置/大小或锚点/停靠参数来调整大小。

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多