这个例子实现了TStringGrid内容靠左、中、右显示。由于篇幅,没有完成靠上、下显示和换行显示的功能。如果还有什么问题获更好的方法,欢迎探讨。示例源码:procedure TextOutAlign(ACanvas:TCanvas;ARect:TRect; AText:string;Al:TAlignment=taLeftJustify;Margin:integer=2); var iTextWidth,iTextheight,iLeft,iTop:integer; begin if not Assigned(ACanvas) then Exit; iTextWidth:=ACanvas.TextWidth(AText); iTextheight:=ACanvas.TextHeight(AText); case Al of taLeftJustify: iLeft:=ARect.Left+Margin; taCenter: iLeft:=ARect.Left+(ARect.Right-ARect.Left-iTextWidth)div 2; taRightJustify: iLeft:=ARect.Right-iTextWidth-Margin; end; iTop:=ARect.Top+(ARect.Bottom-ARect.Top-iTextheight)div 2; if iLeft<ARect.Left then iLeft:=ARect.Left; if iTop<ARect.Top then iTop:=ARect.Top; ACanvas.FillRect(ARect); ACanvas.TextRect(ARect,iLeft,iTop,AText); end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var sValue:string; iTextWidth,iTextheight,iLeft,iTop:integer; Al:TAlignment; begin case ACol mod 3 of 1: Al:=taLeftJustify; 2: Al:=taCenter; 0: Al:=taRightJustify; end; TextOutAlign(StringGrid1.Canvas,Rect,StringGrid1.Cells[ACol,ARow],Al); end;
修正: procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var Al:TAlignment; begin case ACol mod 3 of 1: Al:=taLeftJustify; 2: Al:=taCenter; 0: Al:=taRightJustify; end; TextOutAlign(StringGrid1.Canvas,Rect,StringGrid1.Cells[ACol,ARow],Al); end;
procedure TfrmMain.sgr4DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var OldAlign, OldBkMode, OldTextColor:Integer; Text_Height, X, Y:Integer; begin Inherited; with sgr4.Canvas do begin FillRect(Rect); OldBkMode:=SetBkMode(Handle,OPAQUE); OldAlign:=SetTextAlign(Handle, TA_Center); Text_Height:=TextHeight('Test'); X:=(Rect.Left+Rect.Right) div 2+1; Y:=(Rect.Bottom+Rect.Top-Text_Height) div 2; if Y<0 then Y:=0; OldTextColor:=Font.Color; if (ARow=0)or(ACol=0) then begin Font.Color:=clWhite; TextOut(X+1, Y+1, sgr4.Cells[ACol, ARow]); end; Font.Color:=OldTextColor; TextOut(X, Y, sgr4.Cells[ACol, ARow]); SetTextAlign(Handle, OldAlign); SetBkMode(Handle, OldBkMode); end; end;百分百的能行的。
AText:string;Al:TAlignment=taLeftJustify;Margin:integer=2);
var
iTextWidth,iTextheight,iLeft,iTop:integer;
begin
if not Assigned(ACanvas) then Exit;
iTextWidth:=ACanvas.TextWidth(AText);
iTextheight:=ACanvas.TextHeight(AText);
case Al of
taLeftJustify: iLeft:=ARect.Left+Margin;
taCenter: iLeft:=ARect.Left+(ARect.Right-ARect.Left-iTextWidth)div 2;
taRightJustify: iLeft:=ARect.Right-iTextWidth-Margin;
end;
iTop:=ARect.Top+(ARect.Bottom-ARect.Top-iTextheight)div 2;
if iLeft<ARect.Left then
iLeft:=ARect.Left;
if iTop<ARect.Top then
iTop:=ARect.Top;
ACanvas.FillRect(ARect);
ACanvas.TextRect(ARect,iLeft,iTop,AText);
end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
sValue:string;
iTextWidth,iTextheight,iLeft,iTop:integer;
Al:TAlignment;
begin
case ACol mod 3 of
1: Al:=taLeftJustify;
2: Al:=taCenter;
0: Al:=taRightJustify;
end;
TextOutAlign(StringGrid1.Canvas,Rect,StringGrid1.Cells[ACol,ARow],Al);
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
Al:TAlignment;
begin
case ACol mod 3 of
1: Al:=taLeftJustify;
2: Al:=taCenter;
0: Al:=taRightJustify;
end;
TextOutAlign(StringGrid1.Canvas,Rect,StringGrid1.Cells[ACol,ARow],Al);
end;
Rect: TRect; State: TGridDrawState);
var
OldAlign,
OldBkMode,
OldTextColor:Integer;
Text_Height, X, Y:Integer;
begin
Inherited;
with sgr4.Canvas do
begin
FillRect(Rect);
OldBkMode:=SetBkMode(Handle,OPAQUE);
OldAlign:=SetTextAlign(Handle, TA_Center);
Text_Height:=TextHeight('Test');
X:=(Rect.Left+Rect.Right) div 2+1;
Y:=(Rect.Bottom+Rect.Top-Text_Height) div 2;
if Y<0 then Y:=0;
OldTextColor:=Font.Color;
if (ARow=0)or(ACol=0) then
begin
Font.Color:=clWhite;
TextOut(X+1, Y+1, sgr4.Cells[ACol, ARow]);
end;
Font.Color:=OldTextColor;
TextOut(X, Y, sgr4.Cells[ACol, ARow]);
SetTextAlign(Handle, OldAlign);
SetBkMode(Handle, OldBkMode);
end;
end;百分百的能行的。