【问题标题】:How to create a line to be draggable object?如何创建一条线作为可拖动对象?
【发布时间】:2021-07-04 12:07:05
【问题描述】:

我通过保持起点和终点创建了一条线。 目前,如果我想移动起点或终点,我必须点击某个点才能移动一个点。

但有时我不想改变起点和终点的距离。 所以我想点击两点之间的一条线来移动一条线并移动线的位置。

如何使一条线成为对象?用户点击该行如何处理?

【问题讨论】:

    标签: .net graphics line


    【解决方案1】:

    这是一个画线的类:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.ComponentModel;
    
    namespace BaseClasses
    {
        public class MyLine : Control
        {
            private int _lineSize = 1;
            [DefaultValue(typeof(int), "1")]
            public int LineSize
            {
                get
                {
                    return _lineSize;
                }
    
                set
                {
                    if (value != _lineSize)
                    {
                        _lineSize = value;
                        MinimumSize = new Size(_lineSize, _lineSize);
                        if (Parent != null)
                            Invalidate();
                    }
                }
            }
    
            private LineOrientation _orientation = LineOrientation.Diag1;
            [DefaultValue(typeof(LineOrientation), "Diag1")]
            public LineOrientation Orientation
            {
                get
                {
                    return _orientation;
                }
                set
                {
                    if (value != _orientation)
                    {
                        _orientation = value;
                        if (Parent != null)
                            Invalidate();
                    }
                }
            }
    
            private Color _lineColor = Color.Black;
            [DefaultValue(typeof(Color), "Black")]
            public Color LineColor
            {
                get
                {
                    return _lineColor;
                }
    
                set
                {
                    if (value != _lineColor)
                    {
                        _lineColor = value;
                        if (Parent != null)
                            Invalidate();
                    }
                }
            }
    
            public MyLine()
            {
                TabStop = false;
            }
    
            protected override void OnParentChanged(EventArgs e)
            {
                base.OnParentChanged(e);
                if (Parent != null)
                    this.BackColor = this.Parent.BackColor;
            }
    
            protected override void OnParentBackColorChanged(EventArgs e)
            {
                base.OnParentBackColorChanged(e);
                this.BackColor = this.Parent.BackColor;
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                using (Pen pen = new Pen(_lineColor, _lineSize))
                {
                    if (_orientation == LineOrientation.Diag1)
                        e.Graphics.DrawLine(pen, _lineSize - 1, _lineSize - 1, Width - _lineSize, Height - _lineSize);
                    else if (_orientation == LineOrientation.Diag2)
                        e.Graphics.DrawLine(pen, _lineSize - 1, Height - _lineSize, Width - _lineSize, _lineSize - 1);
                    else if (_orientation == LineOrientation.Horiz)
                        e.Graphics.DrawLine(pen, _lineSize - 1, (Height - _lineSize) / 2, Width - _lineSize, (Height - _lineSize) / 2);
                    else if (_orientation == LineOrientation.Vert)
                        e.Graphics.DrawLine(pen, (Width - _lineSize) / 2, Height - _lineSize, (Width - _lineSize) / 2, _lineSize - 1);
                }
            }
    
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                Invalidate();
            }
    
        }
    
        public enum LineOrientation { Diag1, Diag2, Vert, Horiz }
    }
    

    应在基类级别(Control)处理单击事件,并确定单击是在线上还是其他地方发生。

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 1970-01-01
      • 2014-07-25
      • 2011-07-30
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多