簡體   English   中英

不要將標簽移到PictureBox外部

[英]Don't move the Labels outside a PictureBox

我正在創建一個應用程序,可以在其中移動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) { }
}

在上面的代碼中,我為標簽創建了一些鼠標事件。

PictureBox控件不是容器,不能像在PanelGroupBox或其他實現IContainerControl控件中那樣直接將另一個控件放入其中。
您可以將Label設為父級(在這種情況下),將Label Parent設置為PictureBox手柄。 然后Label.Bounds將反映父Bounds
但是,這不是必需的:您可以只計算Label相對於同時包含( LabelPictureBox )的控件的位置:

您可以訂閱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)
    {
        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;
    }
}

標簽在邊界內移動

您需要跟蹤兩件事:1.是否按下鼠標bool IsMouseDown = false; 2.標簽的起始位置-Point 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;
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM