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 之外
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01