Don#39;t move the Labels outside a PictureBox(不要将标签移到 PictureBox 之外)
问题描述
我正在创建一个应用程序,我可以在其中移动 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 之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不要将标签移到 PictureBox 之外
基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
