不要将标签移到 PictureBox 之外

Don't move the Labels outside a PictureBox

我正在创建一个应用程序,我可以在其中移动 PictureBox.
上的 Labels 问题是我希望这些标签仅移动 inside PictureBox.

这是我的代码:

protected void lbl_MouseMove(object sender, MouseEventArgs e)
{
    Label lbl = sender as Label;

    try
    {
        if (lbl != null && e.Button == MouseButtons.Left)
        {
            if (m_lblLocation != new Point(0, 0))
            {
                Point newLocation = lbl.Location;
                newLocation.X = newLocation.X + e.X - m_lblLocation.X;
                newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
                lbl.Location = newLocation;
                this.Refresh();
            }
        }
    }
    catch(Exception ex) { }
}

protected void lbl_MouseUp(object sender, MouseEventArgs e)
{
    Label lbl = sender as Label;

    try
    {
        if (lbl != null && e.Button == MouseButtons.Left)
        {
            m_lblLocation = Point.Empty;
        }
    }
    catch(Exception ex) { }
}

protected void lbl_MouseDown(object sender, MouseEventArgs e)
{
    Label lbl = sender as Label;

    try
    {
        if (lbl != null && e.Button == MouseButtons.Left)
        {
            m_lblLocation = e.Location;
        }
    }
    catch(Exception ex) { }
}

在上面的代码中,我为标签创建了一些鼠标事件。

您需要跟踪两件事: 1. 是否按下鼠标 - bool IsMouseDown = false; 2.标签的起始位置- Point StartPoint;

// mouse is not down
private void label1_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseDown = false;
}


 //mouse is down and set the starting postion
 private void label1_MouseDown(object sender, MouseEventArgs e)
 {   
     //if left mouse button was pressed
     if (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
         IsMouseDown = true;
         label1.BringToFront();
         StartPoint = e.Location;
      }
   }


    //check the label is withing the borders of the picture box
    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (IsMouseDown)
        {
            int left = e.X + label1.Left - StartPoint.X;
            int right = e.X + label1.Right - StartPoint.X;
            int top = e.Y + label1.Top - StartPoint.Y;
            int bottom = e.Y + label1.Bottom - StartPoint.Y;
            if (left > pictureBox1.Left && top > pictureBox1.Top && pictureBox1.Bottom >= bottom && pictureBox1.Right >= right)
            {
                label1.Left = left;
                label1.Top = top;
            }
        }
    }

PictureBox 控件不是容器,您不能像 Panel 那样直接将另一个控件 放入 中, GroupBox 或其他实现 IContainerControl.
的控件 您可以将 Label 作为父级(在本例中),将 Label 父级设置为 PictureBox 句柄。 Label.Bounds 将反映父 Bounds.
然而,这不是必需的:您可以只计算 Label 相对于包含 (Label(s) 和 PictureBox):

的控件的位置

您可以限制订阅 MovableLabel_MouseDown/MouseUp/MouseMove 事件的其他 Label 控件的移动。

一个例子:

bool thisLabelCanMove;
Point labelMousePosition = Point.Empty;

private void MovableLabel_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left) {
        labelMousePosition = e.Location;
        thisLabelCanMove = true;
    }
}

private void MovableLabel_MouseUp(object sender, MouseEventArgs e)
{
    thisLabelCanMove = false;
}

private void MovableLabel_MouseMove(object sender, MouseEventArgs e)
{
    if (thisLabelCanMove) {
        var label = sender as Label;

        Point newLocation = new Point(label.Left + (e.Location.X - labelMousePosition.X),
                                      label.Top + (e.Location.Y - labelMousePosition.Y));
        newLocation.X = (newLocation.X < pictureBox1.Left) ? pictureBox1.Left : newLocation.X;
        newLocation.Y = (newLocation.Y < pictureBox1.Top) ? pictureBox1.Top : newLocation.Y;
        newLocation.X = (newLocation.X + label.Width > pictureBox1.Right) ? label.Left : newLocation.X;
        newLocation.Y = (newLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : newLocation.Y;
        label.Location = newLocation;
    }
}