Don't move the Labels outside a PictureBox

后端 未结 2 1844
走了就别回头了
走了就别回头了 2020-12-22 04:15

I am creating an application in which I can move the Labels that are on a PictureBox.
The problem is that I want these to only Labels move

相关标签:
2条回答
  • 2020-12-22 04:50

    The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.
    You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.
    However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):

    You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.

    An example:

    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;
        }
    }
    

    0 讨论(0)
  • 2020-12-22 05:13

    you need to track two things: 1. is the mouse press or not - bool IsMouseDown = false; 2. the start location of the label- 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;
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题