procedure TForm4.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
const
WSpace = 2;
HSpace = 2;
var
tmpstr: string;
w,h: integer;
iType:Integer;
Area:TRect;
begin
if ImpStringGrid1.Cells[ACol,ARow]='NG' then begin
with ImpStringGrid1.Canvas do
begin
FillRect(Rect);
Font.Color:=clred;
TextOut(Rect.left,Rect.top,ImpStringGrid1.cells[Acol,Arow]);
end;
end;
iType:=1;
if iType=0 then begin
tmpstr := (Sender as TStringGrid).Cells[ACol,ARow];
with (Sender as TStringGrid).Canvas do begin
w := TextWidth(tmpstr);
h := TextHeight(tmpstr);
if (gdFixed in State) then
TextRect(Rect,trunc((Rect.Right+Rect.Left-w)/2)+WSpace, //居中显示
trunc((Rect.Bottom+Rect.Top-H)/2)+HSpace,tmpstr)
else //右对齐显示
TextRect(Rect,Rect.Right-WSpace-W,Rect.Bottom-HSpace-H,tmpstr);
end;
end else begin
//画文本,可以设置显示的对齐方式
with (Sender as TStringGrid).Canvas do begin
FillRect(Rect);
Area:= Rect;
InflateRect(Area, -2, -2);
//if ARow=0 then
DrawText(Handle, PChar((Sender as TStringGrid).Cells[ACol, ARow]),Length((Sender as TStringGrid).Cells[ACol, ARow]), Area,DT_CENTER + DT_SINGLELINE + DT_VCENTER);//居中
//DrawText(Handle, PChar((Sender as TStringGrid).Cells[ACol, ARow]),Length((Sender as TStringGrid).Cells[ACol, ARow]), Area, DT_CENTER or DT_VCENTER);//居中
//else if (ACol=0) and (ARow<>0) then
//DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_RIGHT)//左对齐
//else
//DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_LEFT);//右对齐
end;
end;
end;