不要将标签移到PictureBox外部 [英] Don't move the Labels outside a PictureBox

查看:97
本文介绍了不要将标签移到PictureBox外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,可以在其中移动PictureBox上的Labels.
问题是我只希望这些标签将标签移动到的内部 .

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 inside the PictureBox.

这是我的代码:

Here is my code:

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) { }
}

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

In above code I have created some mouse events for the Labels.

推荐答案

PictureBox控件不是容器,您不能像在其中那样直接将另一个控件放入 PanelGroupBox或其他实现

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):

您可以限制订阅MovableLabel_MouseDown/MouseUp/MouseMove事件的其他Label控件的移动.

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

一个例子:

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

这篇关于不要将标签移到PictureBox外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆