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

时间:2016-08-30 07:39:43

标签: c# winforms draw drag

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

我想绘制一个可拖动的TextBox并链接两个TextBoxes。在这段代码中,我可以拖动一个TextBox,但是两行之间的链接没有正常工作。

1 个答案:

答案 0 :(得分:1)

  • DrawLine代码已关闭,错误和/或缺少Dispose
  • 您的代码非常冗余

这是一个建议;首先,让我们使用正确的DrawLine子句清除using代码。

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

最后,我们准备将两者 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);

    .. 
  }

另请注意我是如何通过绘制Panel制作activeControl.Parent泛型的,这可能是任何内容..