以前看到一个,给楼主参考一下,下面的是第一行中间对其,你修改一下就可以了 procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var CellStr: String; Mode,CellLen: Integer; begin //①先清除CELL StringGrid1.Canvas.FillRect(Rect); CellStr := StringGrid1.Cells[ACol,ARow]; //②根据文字長度自動調節列寛 CellLen := StringGrid1.Canvas.TextWidth(CellStr)+3; if (CellLen>StringGrid1.ColWidths[ACol]) then StringGrid1.ColWidths[ACol] := CellLen; //③底色設定 if ARow>0 then begin if ARow mod 2 = 0 then StringGrid1.Canvas.Brush.Color := $00DBDBDB else StringGrid1.Canvas.Brush.Color := clWhite; StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,''); end; //④文字排版 if ARow=0 then begin StringGrid1.Canvas.Font.Color := clRed; //可設定字体的顔色、大小 Mode := DT_CENTER //調整左右 end else begin StringGrid1.Canvas.Font.Color := clWindowText; Mode := DT_LEFT; Rect.Left := Rect.Left+2; //微調(使字不圧左側的線) end; Rect.Top := Rect.Top+3; //調整上下 //StringGrid1.Canvas.TextRect(rect,rect.Left,rect.Top,cellstr); 这种方法和下面的方法效果相同 DrawText(StringGrid1.Canvas.Handle,PChar(CellStr),Length(CellStr),Rect,Mode);end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin with StringGrid1 do begin if ACol = 2 then begin Canvas.FillRect(Rect); DrawText(Canvas.Handle,PChar(Cells[ACol,ARow]), -1, Rect, DT_RIGHT); end; end; end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
CellStr: String;
Mode,CellLen: Integer;
begin
//①先清除CELL
StringGrid1.Canvas.FillRect(Rect); CellStr := StringGrid1.Cells[ACol,ARow];
//②根据文字長度自動調節列寛
CellLen := StringGrid1.Canvas.TextWidth(CellStr)+3;
if (CellLen>StringGrid1.ColWidths[ACol]) then
StringGrid1.ColWidths[ACol] := CellLen; //③底色設定
if ARow>0 then begin
if ARow mod 2 = 0 then
StringGrid1.Canvas.Brush.Color := $00DBDBDB
else
StringGrid1.Canvas.Brush.Color := clWhite;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,'');
end; //④文字排版
if ARow=0 then begin
StringGrid1.Canvas.Font.Color := clRed; //可設定字体的顔色、大小
Mode := DT_CENTER //調整左右
end
else begin
StringGrid1.Canvas.Font.Color := clWindowText;
Mode := DT_LEFT;
Rect.Left := Rect.Left+2; //微調(使字不圧左側的線)
end;
Rect.Top := Rect.Top+3; //調整上下
//StringGrid1.Canvas.TextRect(rect,rect.Left,rect.Top,cellstr); 这种方法和下面的方法效果相同
DrawText(StringGrid1.Canvas.Handle,PChar(CellStr),Length(CellStr),Rect,Mode);end;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
if ACol = 2 then
begin
Canvas.FillRect(Rect);
DrawText(Canvas.Handle,PChar(Cells[ACol,ARow]), -1, Rect, DT_RIGHT);
end;
end;
end;