【问题标题】:Don't move the Labels outside a PictureBox不要将标签移到 PictureBox 之外
【发布时间】:2019-04-18 08:51:06
【问题描述】:

我正在创建一个应用程序,我可以在其中移动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) { }
}

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

【问题讨论】:

  • 只需将标签放在面板上,然后用图像填充面板的背景

标签: c# winforms drag-and-drop label picturebox


【解决方案1】:

PictureBox 控件不是容器,您不能直接将另一个控件放入其中,就像使用 PanelGroupBox 或其他实现的控件一样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;
    }
}

【讨论】:

    【解决方案2】:

    您需要跟踪两件事: 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;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 2013-02-04
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多