2012-04-06 83 views
2

我正在使用delphi 2010爲帶有一個stringgrid的項目。我希望網格的一些列是正確的。 I understand how I can do this將defaultdrawing設置爲false。右對齊delphi stringgrid列但保持主題繪圖樣式

但是,如果可能的話,我希望爲網格保留運行時主題陰影。有沒有一種方法可以在啓用了defaultdrawing的情況下對列進行右對齊,或者至少複製onDrawCell事件中的代碼以模擬運行時主題着色?

回答

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; 

而結果

enter image description here