ReoGrid 在工作表上提供五个鼠标事件,用于处理单元格上的鼠标交互。

命名空间

using unvell.ReoGrid;
using unvell.ReoGrid.Events;
using unvell.ReoGrid.Interaction;

事件

事件描述
CellMouseDown鼠标按钮在单元格上按下时触发
CellMouseUp鼠标按钮在单元格上释放时触发
CellMouseMove鼠标在单元格上移动时触发
CellMouseEnter鼠标指针进入单元格时触发
CellMouseLeave鼠标指针离开单元格时触发

所有事件使用 CellMouseEventArgs

CellMouseEventArgs 属性

属性类型描述
CellCell单元格实例。如果单元格没有设置数据或样式,可能为 null
CellPositionCellPosition单元格的从零开始的位置(始终可用)
ButtonsMouseButtons按下了哪个鼠标按钮
RelativePositionPoint鼠标相对于单元格的位置
AbsolutePositionPoint鼠标相对于控件的位置
Clicksint点击次数(用于双击检测)
Deltaint鼠标滚轮增量(仅在滚轮事件中使用)
Capturebool获取或设置是否捕获鼠标
CursorStyleCursorStyle获取或设置光标样式
IsCancelledbool设置为 true 以取消 ReoGrid 的默认操作
WorksheetWorksheet工作表实例

注意: 如果单元格没有数据或样式,Cell 属性可能为 null。使用 sheet.CreateAndGetCell(e.CellPosition) 以确保获取有效的单元格实例。

CursorStyle 枚举

描述
PlatformDefault默认光标
Hand手型光标
Selection范围选择光标
FullRowSelect全行选择器
FullColumnSelect全列选择器
EntireSheet全表选择器
Move移动对象
Copy复制对象
ChangeColumnWidth列宽调整
ChangeRowHeight行高调整
ResizeHorizontal水平调整大小
ResizeVertical垂直调整大小
Busy忙碌/等待
Cross十字光标

示例

单元格鼠标按下

sheet.CellMouseDown += (s, e) =>
{
    // 安全做法:始终使用 CellPosition,而不是 Cell(可能为 null)
    var cell = sheet.CreateAndGetCell(e.CellPosition);
    Console.WriteLine($"Clicked cell {e.CellPosition}: {cell.Data}");
};

单元格鼠标释放

sheet.CellMouseUp += (s, e) =>
{
    Console.WriteLine($"Mouse released at {e.CellPosition}, button: {e.Buttons}");
};

单元格鼠标进入/离开

sheet.CellMouseEnter += (s, e) =>
{
    // 悬停时高亮
    if (e.Cell != null)
    {
        e.Cell.Style.BackColor = SolidColor.LightYellow;
    }
};

sheet.CellMouseLeave += (s, e) =>
{
    if (e.Cell != null)
    {
        sheet.RemoveRangeStyles(e.CellPosition, PlainStyleFlag.BackColor);
    }
};

取消默认行为

sheet.CellMouseDown += (s, e) =>
{
    if (e.CellPosition.Col == 0)
    {
        // 阻止第一列的选择
        e.IsCancelled = true;
    }
};

更改光标样式

sheet.CellMouseMove += (s, e) =>
{
    if (e.Cell?.Data != null)
    {
        e.CursorStyle = CursorStyle.Hand;
    }
};

检测双击

sheet.CellMouseDown += (s, e) =>
{
    if (e.Clicks == 2)
    {
        Console.WriteLine($"Double-clicked on {e.CellPosition}");
    }
};

相关主题

这篇文章对您有帮助吗?