实现选中区域时鼠标可以移动区域的功能,用于调整区域位置

This commit is contained in:
zqm
2025-11-07 15:26:27 +08:00
parent cd76055fa7
commit 36ef3415fb

View File

@@ -42,6 +42,10 @@ namespace JoyD.Windows.CS
private Point _resizeStartPoint; private Point _resizeStartPoint;
private Rectangle _originalRectangle; private Rectangle _originalRectangle;
// 区域移动相关变量
private bool _isMoving = false;
private Point _startMovePoint;
public Setting() public Setting()
{ {
InitializeComponent(); InitializeComponent();
@@ -95,7 +99,7 @@ namespace JoyD.Windows.CS
// 刷新绘制 // 刷新绘制
picBoxTemp.Invalidate(); picBoxTemp.Invalidate();
} }
// 处理选中区域的调整大小 // 处理选中区域的调整大小或移动
else if (e.Button == MouseButtons.Left && !_isDrawingMode && _selectedRegionIndex != -1) else if (e.Button == MouseButtons.Left && !_isDrawingMode && _selectedRegionIndex != -1)
{ {
RegionInfo selectedRegion = _drawnRectangles.FirstOrDefault(r => r.Index == _selectedRegionIndex); RegionInfo selectedRegion = _drawnRectangles.FirstOrDefault(r => r.Index == _selectedRegionIndex);
@@ -113,6 +117,13 @@ namespace JoyD.Windows.CS
_resizeStartPoint = e.Location; _resizeStartPoint = e.Location;
_originalRectangle = controlRectangle; _originalRectangle = controlRectangle;
} }
else if (controlRectangle.Contains(e.Location))
{
// 开始移动区域
_isMoving = true;
_startMovePoint = e.Location;
picBoxTemp.Cursor = Cursors.SizeAll;
}
} }
} }
// 处理左击开始绘制矩形 // 处理左击开始绘制矩形
@@ -216,7 +227,7 @@ namespace JoyD.Windows.CS
/// <summary> /// <summary>
/// 鼠标移动事件 - 更新矩形大小、检测鼠标悬停区域或更新光标 /// 鼠标移动事件 - 更新矩形大小、移动区域、检测鼠标悬停区域或更新光标
/// </summary> /// </summary>
private void PicBoxTemp_MouseMove(object sender, MouseEventArgs e) private void PicBoxTemp_MouseMove(object sender, MouseEventArgs e)
{ {
@@ -251,6 +262,48 @@ namespace JoyD.Windows.CS
} }
} }
} }
// 处理移动区域
else if (_isMoving && !_isDrawingMode && _selectedRegionIndex != -1)
{
RegionInfo selectedRegion = _drawnRectangles.FirstOrDefault(r => r.Index == _selectedRegionIndex);
if (selectedRegion != null)
{
// 计算移动距离
int deltaX = e.Location.X - _startMovePoint.X;
int deltaY = e.Location.Y - _startMovePoint.Y;
// 转换为图像坐标的移动距离
float scaleX = (float)picBoxTemp.Image.Width / picBoxTemp.ClientSize.Width;
float scaleY = (float)picBoxTemp.Image.Height / picBoxTemp.ClientSize.Height;
int imageDeltaX = (int)(deltaX * scaleX);
int imageDeltaY = (int)(deltaY * scaleY);
// 计算新的矩形位置
Rectangle newRect = new Rectangle(
selectedRegion.ImageRectangle.X + imageDeltaX,
selectedRegion.ImageRectangle.Y + imageDeltaY,
selectedRegion.ImageRectangle.Width,
selectedRegion.ImageRectangle.Height
);
// 确保矩形不会移出图像边界
if (newRect.Left >= 0 && newRect.Top >= 0 &&
newRect.Right <= picBoxTemp.Image.Width &&
newRect.Bottom <= picBoxTemp.Image.Height)
{
selectedRegion.ImageRectangle = newRect;
// 更新起始点,为下一次移动做准备
_startMovePoint = e.Location;
// 重新创建叠加层以反映变化
CreateRectangleOverlayImage();
// 触发重绘
picBoxTemp.Invalidate();
}
}
}
// 处理绘制新矩形 // 处理绘制新矩形
else if (_isDrawing && _isDrawingMode) else if (_isDrawing && _isDrawingMode)
{ {
@@ -292,7 +345,7 @@ namespace JoyD.Windows.CS
} }
// 如果没有悬停在句柄上,检查是否悬停在区域上 // 如果没有悬停在句柄上,检查是否悬停在区域上
if (_currentHandle == ResizeHandle.None) if (_currentHandle == ResizeHandle.None && !_isMoving)
{ {
// 将鼠标坐标转换为图像坐标 // 将鼠标坐标转换为图像坐标
Point imagePoint = ControlPointToImagePoint(e.Location); Point imagePoint = ControlPointToImagePoint(e.Location);
@@ -420,7 +473,7 @@ namespace JoyD.Windows.CS
} }
/// <summary> /// <summary>
/// 鼠标释放事件 - 完成矩形绘制调整大小 /// 鼠标释放事件 - 完成矩形绘制调整大小或移动
/// 使用增量绘制,避免每次都重绘所有矩形 /// 使用增量绘制,避免每次都重绘所有矩形
/// 确保矩形坐标相对于图像而非控件 /// 确保矩形坐标相对于图像而非控件
/// </summary> /// </summary>
@@ -435,6 +488,14 @@ namespace JoyD.Windows.CS
return; return;
} }
// 结束移动区域
if (_isMoving && e.Button == MouseButtons.Left)
{
_isMoving = false;
picBoxTemp.Cursor = Cursors.Default;
return;
}
// 结束绘制新矩形 // 结束绘制新矩形
if (_isDrawing && _isDrawingMode && e.Button == MouseButtons.Left && picBoxTemp.Image != null) if (_isDrawing && _isDrawingMode && e.Button == MouseButtons.Left && picBoxTemp.Image != null)
{ {