实现鼠标指针与画笔大小一致的功能
This commit is contained in:
@@ -751,6 +751,7 @@ namespace JoyD.Windows.CS
|
|||||||
if (_isTempDiffDrawingMode)
|
if (_isTempDiffDrawingMode)
|
||||||
{
|
{
|
||||||
// 进入温差图绘制状态
|
// 进入温差图绘制状态
|
||||||
|
// 暂时使用十字光标,实际光标会在MouseMove事件中根据画笔大小设置
|
||||||
picBoxTemp.Cursor = Cursors.Cross;
|
picBoxTemp.Cursor = Cursors.Cross;
|
||||||
btnDrawTempDiff.ToolTipText = "温差图绘制模式已启用,点击图片区域进行绘制(点击关闭)";
|
btnDrawTempDiff.ToolTipText = "温差图绘制模式已启用,点击图片区域进行绘制(点击关闭)";
|
||||||
|
|
||||||
@@ -826,8 +827,13 @@ namespace JoyD.Windows.CS
|
|||||||
_isTempDiffDrawingMode = false;
|
_isTempDiffDrawingMode = false;
|
||||||
btnDrawTempDiff.Checked = false;
|
btnDrawTempDiff.Checked = false;
|
||||||
|
|
||||||
// 重置鼠标光标
|
// 重置鼠标光标并释放自定义光标资源
|
||||||
|
if (picBoxTemp.Cursor != Cursors.Default)
|
||||||
|
{
|
||||||
|
Cursor oldCursor = picBoxTemp.Cursor;
|
||||||
picBoxTemp.Cursor = Cursors.Default;
|
picBoxTemp.Cursor = Cursors.Default;
|
||||||
|
oldCursor.Dispose(); // 释放自定义光标资源
|
||||||
|
}
|
||||||
|
|
||||||
// 更新按钮提示文本
|
// 更新按钮提示文本
|
||||||
btnDrawTempDiff.ToolTipText = "绘制温差图";
|
btnDrawTempDiff.ToolTipText = "绘制温差图";
|
||||||
@@ -997,11 +1003,105 @@ namespace JoyD.Windows.CS
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建与画笔大小一致的自定义光标
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="size">画笔大小</param>
|
||||||
|
/// <param name="color">画笔颜色</param>
|
||||||
|
/// <returns>自定义光标</returns>
|
||||||
|
private Cursor CreateCustomBrushCursor(int size, Color color)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 创建一个足够大的位图以容纳画笔大小
|
||||||
|
int cursorSize = Math.Max(size, 16); // 最小16x16
|
||||||
|
Bitmap cursorBitmap = new Bitmap(cursorSize, cursorSize);
|
||||||
|
|
||||||
|
using (Graphics g = Graphics.FromImage(cursorBitmap))
|
||||||
|
{
|
||||||
|
// 清除背景为透明色
|
||||||
|
g.Clear(Color.Magenta); // Magenta作为透明色
|
||||||
|
|
||||||
|
// 计算正方形位置,使其居中
|
||||||
|
int x = (cursorSize - size) / 2;
|
||||||
|
int y = (cursorSize - size) / 2;
|
||||||
|
|
||||||
|
// 绘制正方形表示画笔大小
|
||||||
|
using (SolidBrush brush = new SolidBrush(color))
|
||||||
|
{
|
||||||
|
g.FillRectangle(brush, x, y, size, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加边框以提高可见性
|
||||||
|
using (Pen pen = new Pen(Color.Black))
|
||||||
|
{
|
||||||
|
g.DrawRectangle(pen, x, y, size - 1, size - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置透明色
|
||||||
|
cursorBitmap.MakeTransparent(Color.Magenta);
|
||||||
|
|
||||||
|
// 创建光标,热点位于正方形中心
|
||||||
|
return new Cursor(cursorBitmap.GetHicon());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("创建自定义光标失败: " + ex.Message);
|
||||||
|
// 失败时返回默认光标
|
||||||
|
return Cursors.Cross;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 鼠标移动事件 - 更新矩形大小、移动区域、检测鼠标悬停区域或更新光标
|
/// 鼠标移动事件 - 更新矩形大小、移动区域、检测鼠标悬停区域或更新光标
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void PicBoxTemp_MouseMove(object sender, MouseEventArgs e)
|
private void PicBoxTemp_MouseMove(object sender, MouseEventArgs e)
|
||||||
{
|
{
|
||||||
|
// 温差图绘制模式下,使用自定义画笔光标
|
||||||
|
if (_isTempDiffDrawingMode)
|
||||||
|
{
|
||||||
|
// 获取当前选中的温差图例颜色
|
||||||
|
Color selectedColor = Color.Black; // 默认颜色
|
||||||
|
int selectedRowIndex = -1;
|
||||||
|
|
||||||
|
// 尝试获取选中行的颜色
|
||||||
|
if (dataGridViewTempDiff.SelectedRows.Count > 0)
|
||||||
|
{
|
||||||
|
selectedRowIndex = dataGridViewTempDiff.SelectedRows[0].Index;
|
||||||
|
}
|
||||||
|
else if (dataGridViewTempDiff.SelectedCells.Count > 0)
|
||||||
|
{
|
||||||
|
selectedRowIndex = dataGridViewTempDiff.SelectedCells[0].RowIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果选中行索引有效,获取对应的颜色
|
||||||
|
if (selectedRowIndex >= 0 && selectedRowIndex < tempDiffData.Count)
|
||||||
|
{
|
||||||
|
selectedColor = (Color)tempDiffData[selectedRowIndex]["color"];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建并设置自定义光标
|
||||||
|
Cursor customCursor = CreateCustomBrushCursor(_currentBrushSize, selectedColor);
|
||||||
|
|
||||||
|
// 确保不重复设置相同的光标(避免资源泄漏)
|
||||||
|
if (picBoxTemp.Cursor != customCursor)
|
||||||
|
{
|
||||||
|
// 释放旧光标资源
|
||||||
|
picBoxTemp.Cursor.Dispose();
|
||||||
|
picBoxTemp.Cursor = customCursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果按下鼠标左键,执行绘制操作(这里可能需要根据实际绘制逻辑补充)
|
||||||
|
if (e.Button == MouseButtons.Left)
|
||||||
|
{
|
||||||
|
// 这里可以添加实际的绘制逻辑
|
||||||
|
// 目前仅设置光标,具体绘制代码可能在其他事件中
|
||||||
|
}
|
||||||
|
|
||||||
|
return; // 温差图绘制模式下,不执行其他鼠标移动逻辑
|
||||||
|
}
|
||||||
|
|
||||||
// 处理调整大小
|
// 处理调整大小
|
||||||
if (_isResizing && !_isDrawingMode && _selectedRegionIndex != -1)
|
if (_isResizing && !_isDrawingMode && _selectedRegionIndex != -1)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user