2012-04-06 3 views
2

Я использую delphi 2010 для проекта с stringgrid. Я хочу, чтобы некоторые столбцы сетки были правильно обоснованы. I understand how I can do this с установкой defaultdrawing на false.right justify delphi stringgrid column, но сохранить themed drawingstyle

Я хотел бы, однако, сохранить затенение темы времени выполнения для сетки, если это возможно. Есть ли способ правого выравнивания столбца с включенным defaultdrawing или, по крайней мере, дублировать код в событии onDrawCell, чтобы имитировать затенение темы времени выполнения?

ответ

7

вы можете использовать класс Interposer и переопределить метод 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; 

И результат

enter image description here