Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Don';不要将标签移到图片盒外_C#_Winforms_Drag And Drop_Label_Picturebox - Fatal编程技术网

C# Don';不要将标签移到图片盒外

C# Don';不要将标签移到图片盒外,c#,winforms,drag-and-drop,label,picturebox,C#,Winforms,Drag And Drop,Label,Picturebox,我正在创建一个应用程序,可以在其中移动图片盒上的标签 问题是,我只希望这些标签在图片盒中移动 这是我的代码: protected void lbl_MouseMove(object sender, MouseEventArgs e) { Label lbl = sender as Label; try { if (lbl != null && e.Button == MouseButtons.Left) {

我正在创建一个应用程序,可以在其中移动
图片盒上的
标签

问题是,我只希望这些标签在
图片盒
中移动

这是我的代码:

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.标签的起始位置-
点起始点

// 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
控件不是一个容器,您不能直接将另一个控件放入其中,就像使用
面板
、组框
或其他实现的控件一样。
您可以将
标签设置为父标签(在本例中),将
标签设置为
PictureBox
句柄。然后,
Label.Bounds
将反映父级
Bounds

但是,这不是必需的:您只需计算标签相对于控件的位置,该控件包含(
Label
(s)和
PictureBox
):

您可以限制订阅
MovableLabel\u 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)
    {
        Label label = sender as Label;

        Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
                                           label.Top + (e.Location.Y - LabelMousePosition.Y));
        LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
        LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
        LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
        LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
        label.Location = LabelNewLocation;
    }
}
bool这个标签可以移动;
Point LabelMousePosition=点.空;
私有void MovableLabel_MouseDown(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
LabelMousePosition=e.位置;
ThisLabelCanMove=true;
}
}
私有void MovableLabel_MouseUp(对象发送方,MouseEventArgs e)
{
ThisLabelCanMove=false;
}
私有void MovableLabel_MouseMove(对象发送方,MouseEventArgs e)
{
如果(此标签可以移动)
{
标签=发送者作为标签;
Point LabelNewLocation=新点(label.Left+(e.Location.X-LabelMousePosition.X),
label.Top+(e.Location.Y-LabelMousePosition.Y));
LabelNewLocation.X=(LabelNewLocation.XpictureBox1.Right)?label.Left:LabelNewLocation.X;
LabelNewLocation.Y=(LabelNewLocation.Y+label.Height>pictureBox1.Bottom)?label.Top:LabelNewLocation.Y;
label.Location=LabelNewLocation;
}
}

只需将标签放在面板上,并用图像填充面板的背景即可