在 Indicator 列显示行号

设置 view 的 OptionsView|Indicator 属性为 True,并设置合适的 IndicatorWidth,在 View 的 OnCustomDrawIndicatorCell 事件中使用以下代码

procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
  ATextRect: TRect;
  ABoxRect: TRect;
  // AStyle: TcxStyle;
  aCV: TcxCanvas;
begin
  if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
    Exit;
  aCV := ACanvas;
  ATextRect := AViewInfo.ContentBounds;
  AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
  InflateRect(ATextRect, -2, -1);

  // 背景区域向上移动 1 像素,高度加 1,专门适应 Office2010Blue 皮肤
  ABoxRect := AViewInfo.ContentBounds;
  OffsetRect(ABoxRect, 0, -1);
  InflateRect(ABoxRect, 0, 1);

  //这个if段是为了加粗显示当前行号,可不用
  if AIndicatorViewInfo.GridRecord.Selected then
    begin
      aCV.Font.Style := Canvas.Font.Style + [fsBold];
      aCV.Font.Color := clRed;
    end
  else
    begin
      aCV.Font.Style := Canvas.Font.Style - [fsBold];
      acv.Font.Color := Canvas.Font.Color;
    end;
  //Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
  Sender.LookAndFeelPainter.DrawHeader(ACanvas, ABoxRect,
    ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,
    False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
    // AStyle.Font, AStyle.TextColor, AStyle.Color);
    acv.Font, acv.font.Color, acv.Brush.color);
  ADone := True;
end;
posted @ 2024-10-14 07:17  汉学  阅读(12)  评论(0)    收藏  举报