右对齐delphi stringgrid列,但保持主题绘图风格

Joe*_*Joe 2 delphi delphi-2010 tstringgrid

我正在使用delphi 2010来创建一个带有stringgrid的项目.我希望网格的某些列是正确的.我理解如何在defaultdrawing设置为false的情况下执行此操作.

但是,如果可能的话,我想保留网格的运行时主题着色.有没有办法在启用了defaultdrawing的情况下右对齐列,或者至少复制onDrawCell事件中的代码来模仿运行时主题着色?

RRU*_*RUZ 7

您可以使用插入器类并覆盖DrawCell方法,请检查此示例

type
  TStringGrid = class(Grids.TStringGrid)
   protected
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
  end;    

  TForm79 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
  end;

var
  Form79: TForm79;

implementation

{$R *.dfm}

{ TStringGrid }

procedure TStringGrid.DrawCell(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState);
var
  s : string;
  LDelta : integer;
begin
  if (ACol=1) and (ARow>0) then
  begin
    s     := Cells[ACol, ARow];
    LDelta := ColWidths[ACol] - Canvas.TextWidth(s);
    Canvas.TextRect(ARect, ARect.Left+LDelta, ARect.Top+2, s);
  end
  else
  Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol, ARow]);
end;

procedure TForm79.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[0,0]:='title 1';
  StringGrid1.Cells[1,0]:='title 2';
  StringGrid1.Cells[2,0]:='title 3';

  StringGrid1.Cells[0,1]:='normal text';
  StringGrid1.Cells[1,1]:='right text';
  StringGrid1.Cells[2,1]:='normal text';
end;
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述