c# - 在鼠标移动中设置边界
问题描述
如何防止鼠标在图片框(500 x 500 像素)内拖动图像时从图片框(500 x 500 像素)中脱出?
这是鼠标事件:
private void pictureBox_Canvass_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
drag = true;
//start = new Point(e.Location.X + (int)imageRect.Location.X, e.Location.Y + (int)imageRect.Location.Y);
start = new Point((int)Shape.center.X - ((int)imageRect.Location.X - e.X), (int)Shape.center.Y - ((int)imageRect.Location.Y - e.Y));
}
}
private void pictureBox_Canvass_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && drag == true)
{
Point loc = new Point((int)((e.X - start.X) - (imageRect.Width / 2)), (int)((e.Y - start.Y) - (imageRect.Height / 2)));
start.Offset(loc.X, loc.Y);
imageRect.Location = start;
Debug.WriteLine(start);
pictureBox_Canvass.Invalidate();
}
}
private void pictureBox_Canvass_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
解决方案
推荐阅读
- ssl - 如何禁用 Forticlient Fortinet VPN 跟踪日志?
- amazon-web-services - 用于应用程序负载均衡器的 AWS 私有静态 ipv4
- latex - 如何对齐 Latex 方程中的符号?
- php - Laravel:无法访问从控制器@更新发送的视图中的变量
- postgresql - 运行数据库脚本时出现 Postgres 错误
- ssh - 当 X11 服务器死机时,ssh 上的 IPython 会话退出
- atom-editor - Atom Tex 编辑器无法安装任何软件包
- java - 如何在intellij中自动升级spring版本以及maven依赖项?
- timeout - JUnit5:DisableOnDebug 替换?
- c# - ASP Boilerplate 中的 LDAP 实现上的 InvalidCastException