问题描述
我正在创建一个应用程序,我可以在其中移动 PictureBox
上的 Labels
.
问题是我希望这些标签只移动 inside PictureBox
.
这是我的代码:
protected void lbl_MouseMove(object sender, MouseEventArgs e){标签 lbl = 作为标签的发件人;尝试{if (lbl != null && e.Button == MouseButtons.Left){if (m_lblLocation != new Point(0, 0)){点 newLocation = lbl.Location;新位置.X = 新位置.X + e.X - m_lblLocation.X;新位置.Y = 新位置.Y + e.Y - m_lblLocation.Y;lbl.Location = 新位置;this.Refresh();}}}捕捉(异常前){}}protected void lbl_MouseUp(对象发送者,MouseEventArgs e){标签 lbl = 作为标签的发件人;尝试{if (lbl != null && e.Button == MouseButtons.Left){m_lblLocation = Point.Empty;}}捕捉(异常前){}}protected void lbl_MouseDown(对象发送者,MouseEventArgs e){标签 lbl = 作为标签的发件人;尝试{if (lbl != null && e.Button == MouseButtons.Left){m_lblLocation = e.Location;}}捕捉(异常前){}}
在上面的代码中,我为标签创建了一些鼠标事件.
PictureBox
控件不是容器,不能直接在里面放另一个控件,就像使用 Panel
、GroupBox
或其他实现
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.
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;
}
}
这篇关于不要将标签移到 PictureBox 之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!