ReoGrid 在工作表上提供五个鼠标事件,用于处理单元格上的鼠标交互。
命名空间
using unvell.ReoGrid;
using unvell.ReoGrid.Events;
using unvell.ReoGrid.Interaction;
事件
| 事件 | 描述 |
|---|---|
CellMouseDown | 鼠标按钮在单元格上按下时触发 |
CellMouseUp | 鼠标按钮在单元格上释放时触发 |
CellMouseMove | 鼠标在单元格上移动时触发 |
CellMouseEnter | 鼠标指针进入单元格时触发 |
CellMouseLeave | 鼠标指针离开单元格时触发 |
所有事件使用 CellMouseEventArgs。
CellMouseEventArgs 属性
| 属性 | 类型 | 描述 |
|---|---|---|
Cell | Cell | 单元格实例。如果单元格没有设置数据或样式,可能为 null |
CellPosition | CellPosition | 单元格的从零开始的位置(始终可用) |
Buttons | MouseButtons | 按下了哪个鼠标按钮 |
RelativePosition | Point | 鼠标相对于单元格的位置 |
AbsolutePosition | Point | 鼠标相对于控件的位置 |
Clicks | int | 点击次数(用于双击检测) |
Delta | int | 鼠标滚轮增量(仅在滚轮事件中使用) |
Capture | bool | 获取或设置是否捕获鼠标 |
CursorStyle | CursorStyle | 获取或设置光标样式 |
IsCancelled | bool | 设置为 true 以取消 ReoGrid 的默认操作 |
Worksheet | Worksheet | 工作表实例 |
注意: 如果单元格没有数据或样式,
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}");
}
};