2016-02-13 19 views
0

我在一个picturebox上有很多标签。我想移动的标签相对于光标,但标签上应停止移动时,有在移动方向当另一个标签在同一方向上找到时,请停止移动标签

这里的另一个标签是我的代码

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

我没有看到任何代码 – Maertin

+0

在方向或重叠? – TaW

+0

我给出的代码 – Mritunjay

回答

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 ..

+0

谢谢TaW为您的合作谢谢 – Mritunjay

+0

谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢谢 – Mritunjay

+0

如果您对答案感到满意,请考虑考虑[接受](http://stackoverflow.com/帮助/接受答案)它..! - 我发现你从来没有这样做过:在答案的选票下面,点击左上角的(不可见)复选标记,然后单击它!它变成绿色,并获得我们双方的声誉.. – TaW

相关问题