winforms 如何限制控件在另一个控件边界内的移动

brqmpdu1  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(137)

我正在创建一个应用程序,可以在其中移动PictureBox上的Labels
问题是我希望这些标签只在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) { }
}

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

7lrncoxx

7lrncoxx1#

PictureBox控件不是一个容器,你不能直接把另一个控件放在它里面,就像你对PanelGroupBox或其他实现IContainerControl的控件所做的那样。
您可以将Label设置为父对象(在本例中),将Label Parent设置为PictureBox句柄。然后Label.Bounds将反映父Bounds
但这不是必须的:你可以只计算标签相对于同时包含(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;
    }
}

字符串


的数据

ao218c7q

ao218c7q2#

你需要追踪两件事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;
            }
        }
    }

字符串

相关问题