当另一个标签朝同一方向发现时,停止移动标签

时间:2016-02-13 09:30:06

标签: c# label drag

我在图片框上有很多标签。我想相对于光标移动标签,但是当移动方向上有另一个标签时,标签应该停止移动

这是我的代码

void lbl_MouseClick(object sender, MouseEventArgs e)
    {
        try
        {
            lblCutPaste = sender as Control;
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }

    void lbl_MouseDown(object sender, MouseEventArgs e)
    {
        try
        {
            activeControl = sender as Control;
            previousLocation = e.Location;
            //  preloc = activeControl.Location;
            Cursor = Cursors.Hand;



        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }

    void lbl_MouseMove(object sender, MouseEventArgs e)
    {
        try
        {
            bool isCollide = false;
            if (activeControl == null || activeControl != sender)
                return;
            var location = activeControl.Location;
            location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);

            if (location.X >= 0 && location.X <= activeControl.Parent.Width - activeControl.Width && location.Y >= 0 && location.Y <= activeControl.Parent.Height - activeControl.Height)
            {
                activeControl.Location = location;

            }
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }

    void lbl_MouseUp(object sender, MouseEventArgs e)
    {
       activeControl = null;
        Cursor = Cursors.Default;

    }

1 个答案:

答案 0 :(得分:0)

这是一个有效的例子;在学习之后你必须调整你的代码。将lambda拉入你的活动应该不难。

首先创建20 Labels并将其添加到PictureBox。它将每个Label与三个事件挂钩。它在拖动时为活动Label着色。

每个Label也会添加到班级List<T>,以便我们轻松完成所需的检查。检查是使用LINQ来计算所有重叠标签。

它使用Rectangle.IntersectsWithPoint.Subtract方法..

List<Label> myLabels = new List<Label>();
Label movingLabel = null;
Point mDown = Point.Empty;

private void button11_Click(object sender, EventArgs e)
{
    Random R = new Random(8);
    Size sz = pictureBox1.ClientSize;
    for (int i = 0; i < 20; i++)
    {
        Label lbl = new Label() {Text = "L " + i};
        lbl.Location = new Point(R.Next(sz.Width), R.Next(sz.Height));
        lbl.Parent = pictureBox1;
        myLabels.Add(lbl);

        lbl.BorderStyle = BorderStyle.FixedSingle;

        lbl.MouseDown += (ss, ee) =>
            {
                movingLabel = lbl;
                lbl.BackColor = Color.Bisque;
                mDown = ee.Location;  
            };

        lbl.MouseMove += (ss, ee) =>
            {
                if (ee.Button == MouseButtons.Left)
                {
                   Point nLoc = Point.Subtract(lbl.Location, 
                                      new Size(mDown.X - ee.X, mDown.Y - ee.Y));
                   Rectangle rlbNew = new Rectangle(nLoc, lbl.Size);
                   var overlapped = myLabels.Where(x => x != lbl && 
                              rlbNew.IntersectsWith(new Rectangle(x.Location, x.Size)));
                   if (overlapped.Count() == 0) lbl.Location = nLoc;
                }
            };

        lbl.MouseUp += (ss, ee) =>
            {
                movingLabel = null;
                lbl.BackColor = Color.Transparent;
            };
    }
}

请注意,我并不关心创建的Labels是否重叠,所以有些人会这样做,因为他们这样做是不能分开的,除非你继续拖动直到达到'合法的';然后它会跳过重叠的标签并跳到新的位置..

如果你想要你可以编码逻辑以允许在移动时重叠,请将背景颜色设置为红色以显示这不正常,并且如果它仍然重叠则让它跳回MouseUp。你将需要存储原始位置..但是拖到合法的位置可能就足够了,imo ..