在面板控件内绘制多条可拖动线

Draw more than one draggable line inside the panel control

namespace DraggableControls
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Control activeControl;
        private Point previousLocation;

        void txt1_MouseDown(object sender, MouseEventArgs e)
        {
            activeControl = sender as Control;
            previousLocation = e.Location;
            Cursor = Cursors.Hand;            
        }
        void txt1_MouseUp(object sender, MouseEventArgs e)
        {
            activeControl = null;
            Cursor = Cursors.Default;
        }
        void txt1_MouseMove(object sender, MouseEventArgs e)
        {
            if (activeControl == null || activeControl != sender)
                return;
            Point location = activeControl.Location;
            location.Offset(e.Location.X - previousLocation.X, 
                            e.Location.Y - previousLocation.Y);
            activeControl.Location = location;
            DrawLine(txt1.Location, txt2.Location, panel1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt1.MouseDown += new MouseEventHandler(txt1_MouseDown);
            txt1.MouseMove += new MouseEventHandler(txt1_MouseMove);
            txt1.MouseUp += new MouseEventHandler(txt1_MouseUp);


            txt3.MouseDown += new MouseEventHandler(txt3_MouseDown);
            txt3.MouseMove += new MouseEventHandler(txt3_MouseMove);
            txt3.MouseUp += new MouseEventHandler(txt3_MouseUp);
        }



        void txt3_MouseDown(object sender, MouseEventArgs e)
        {
            activeControl = sender as Control;
            previousLocation = e.Location;
            Cursor = Cursors.Hand;
        }
        void txt3_MouseUp(object sender, MouseEventArgs e)
        {
            activeControl = null;
            Cursor = Cursors.Default;
        }
        void txt3_MouseMove(object sender, MouseEventArgs e)
        {
            if (activeControl == null || activeControl != sender)
                return;
            Point location = activeControl.Location;
            location.Offset(e.Location.X - previousLocation.X, 
                            e.Location.Y - previousLocation.Y);
            activeControl.Location = location;
            DrawLine(txt3.Location, txt4.Location, panel1);
        }

        public void DrawLine(Point start, Point end, Panel ctrl)
        {
            ctrl.Refresh();
            Pen P = new Pen(Color.Red, 3);
            P.StartCap = System.Drawing.Drawing2D.LineCap.NoAnchor;
            P.CustomEndCap = 
              new System.Drawing.Drawing2D.AdjustableArrowCap(4, 8, false);
            ctrl.CreateGraphics().DrawLine(P, start, end);
            ctrl.PerformLayout();
            ctrl.CreateGraphics().Dispose();
        }
    }
}

我想绘制一个可拖动的文本框和 link 两个文本框。在此代码中,我可以拖动一个 TextBox,但两行之间的 link 无法正常工作。

  • DrawLine 代码已关闭,错误 and/or 缺少 Dispose
  • 你的代码有点多余

这是一个建议;首先让我们用适当的 using 子句清理 DrawLine 代码。:

    public void DrawLine(Point start, Point end, Control ctrl)
    {
        ctrl.Refresh();
        using ( Graphics g = activeControl.CreateGraphics())
        using ( Pen P = new Pen(Color.Red, 3) )
        {
          P.StartCap = System.Drawing.Drawing2D.LineCap.NoAnchor;
          P.CustomEndCap = 
            new System.Drawing.Drawing2D.AdjustableArrowCap(4, 8, false);
          g.DrawLine(P, start, end);
        }
    }

旁白:请注意,这是您要使用 control.CreateGraphics() 的极少数情况之一;生成的图形是非持久性的,即当系统重绘表单时它们将消失,但这正是我们在这里想要的..

接下来让鼠标事件通用化:

void txt_MouseDown(object sender, MouseEventArgs e)
{
    activeControl = sender as Control;
    previousLocation = e.Location;
    Cursor = Cursors.Hand;
}

void txt_MouseUp(object sender, MouseEventArgs e)
{
    activeControl = null;
    Cursor = Cursors.Default;
    panel1.Invalidate();
}

void txt_MouseMove(object sender, MouseEventArgs e)
{
    if (activeControl == null || activeControl != sender)
        return;
    Control tgtCtl = activeControl.Tag as Control;  // check the..
    if (tgtCtl == null) return;                     // target!

    Point location = activeControl.Location;
    location.Offset(e.Location.X - previousLocation.X, 
                    e.Location.Y - previousLocation.Y);
    activeControl.Location = location;

    DrawLine(location, tgtCtl.Location, activeControl.Parent);
}

最后,我们通过将 both TextBoxes 挂钩到相同的通用事件并告诉每个 TextBox 将通过设置其 Tag:

private void Form1_Load(object sender, EventArgs e)
{
    txt1.Tag = txt2;
    txt3.Tag = txt4;

    txt1.MouseDown += new MouseEventHandler(txt_MouseDown);
    txt1.MouseMove += new MouseEventHandler(txt_MouseMove);
    txt1.MouseUp += new MouseEventHandler(txt_MouseUp);

    txt3.MouseDown += new MouseEventHandler(txt_MouseDown);
    txt3.MouseMove += new MouseEventHandler(txt_MouseMove);
    txt3.MouseUp += new MouseEventHandler(txt_MouseUp);

    .. 
  }

另请注意我是如何通过借鉴 activeControl.Parent 使 Panel 通用的,它可以是任何东西..