delphi使用RichView控件 表格撤消

TRichView表格撤消

撤消和重做

如果表被插入到 RichViewEdit 中,则所有表操作(添加/删除行、合并/取消合并、表属性的分配)都可以撤消/重做。

如果出现以下情况,操作将不会被撤消:

  • 表已创建,但尚未插入;
  • 表格被插入到 RichView;
  • 表格被插入到 RichViewEdit 中,UndoLimit=0

两步操作,分两步撤消

  1. 插入表格
  2. 改变表格颜色
table := TRVTableItemInfo.CreateEx(4,3, RichViewEdit1.RVData);
if RichViewEdit1.InsertItem('', table) then
  table.Color := clRed;

一步操作,一步撤销

插入红色表格

table := TRVTableItemInfo.CreateEx(4,3, RichViewEdit1.RVData);
table.Color := clRed; // 表尚未插入
RichViewEdit1.InsertItem('', table);

关于在编辑器中使用查看器样式的操作来修改文档

这些方法(例如AddNL)不能与编辑器样式的方法(例如InsertText)混合使用,因为它们会与撤消机制发生冲突。 如果在 编辑器样式 之后使用 查看器样式 操作,则撤消重做操作的结果是不可预测的。

此规则不仅与主文档的内容有关,还与单元格有关。

在某些情况下,查看器样式的方法可能在编辑器中有用:

  • Clear方法之后,在任何编辑器样式方法之前(用于在编辑之前生成文档)。Clear 方法清除撤消和重做缓冲区。
  • 在插入表格之前在表格单元格中。
  • 如果编辑器的UndoLimit = 0(撤消机制关闭)。 但是在调用任何编辑器样式的方法之前必须调用 Format
  • ClearUndo方法之后。 但是在调用任何编辑器样式的方法之前必须调用 Format

撤消对属性的分配

为单元格属性(以及行的 VAlign 属性)赋值不会被撤销。

table.Cells[r,c].Color := clRed; // 不会被撤销

table.SetCellColor(clRed, r,c);  // 会被撤销。

这是因为效率原因,这些方法不会更新编辑器视图。 将它们与 BeginItemModify/EndItemModify 一起使用。

示例

var item: TCustomRVItemInfo;
    table: TRVTableItemInfo;
    Data: Integer;
    rve: TCustomRichViewEdit;
    ItemNo: Integer;
begin
  if not RichViewEdit1.CanChange or
     not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, rve, item) then
    exit;
  table := TRVTableItemInfo(item);
  ItemNo := rve.GetItemNo(table);
  rve.BeginItemModify(ItemNo, Data);
  table.SetRowVAlign(rvcBottom, 1);
  table.SetCellColor(clRed,1,1);
  rve.EndItemModify(ItemNo, Data);
  rve.Change;
end;

可以撤消对所有表属性的分配(但仍然需要 BeginItemModify-operations-EndItemModify-Change 顺序)。 唯一的例外是 VisibleBorders 属性,如果你想实现一个可撤销的操作,请使用 SetTableVisibleBorders

对单元格和行属性的直接分配无法撤消,因此请使用以下方法来实现可撤消的操作。

分配表属性的方法

  • TRVTableItemInfo.SetTableVisibleBorders

分配行属性的方法

  • TRVTableItemInfo.SetRowVAlign

分配单元格属性的方法

  • TRVTableItemInfo.SetCellColor, TRVTableItemInfo.SetCellBorderColor, TRVTableItemInfo.SetCellBorderLightColor
  • TRVTableItemInfo.SetCellVAlign
  • TRVTableItemInfo.SetCellBestWidth, TRVTableItemInfo.SetCellBestHeight
  • TRVTableItemInfo.SetCellBackgroundImage, TRVTableItemInfo.SetCellBackgroundImageFileName, TRVTableItemInfo.SetCellBackgroundStyle
  • TRVTableItemInfo.SetCellHint
  • TRVTableItemInfo.SetCellVisibleBorders
原文地址:https://www.cnblogs.com/txgh/p/15641756.html