Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
[COLOR=green]
{ Procedure to draw text centered in a given TCanvas cell. If the text contains a CRLF, then two lines are drawn otherwise only one line is drawn.}[/color]
procedure CenterTwoLineText(AGrid:TStringGrid; Rect:TRect; ACol,ARow:integer;
TextColor:TColor=clWindowText; Background:TColor=clWindow );
var
zSaveForegroundColor,zSaveBackgroundColor:TColor;
sText:string;
nPos:integer;
sLine1:string;
sLine2:string;
nHorCenter:integer;
nVertCenter:integer;
nTextWidth:integer;
nTextHeight:integer;
X:integer;
Y:integer;
Rect2:TRect;
begin
zSaveForegroundColor := AGrid.Canvas.Font.Color;
zSaveBackgroundColor := AGrid.Canvas.Brush.Color;
try
if Background <> clWindow then AGrid.Canvas.Brush.Color := Background;
if TextColor <> clWindowText then AGrid.Canvas.Font.Color := TextColor;
nHorCenter := Rect.Left + (Rect.Right - Rect.Left) div 2;
nVertCenter := Rect.Top + (Rect.Bottom - Rect.Top) div 2;
sText := AGrid.Cells[ACol,ARow];
nPos := Pos(CRLF,sText);
if nPos > 0 then
begin[COLOR=green]
// Line 1[/color]
sLine1 := Copy(sText,1,nPos-1);
nTextWidth := AGrid.Canvas.TextWidth(sLine1);
nTextHeight := AGrid.Canvas.TextHeight(sLine1);
X := nHorCenter - (nTextWidth div 2);
Y := nVertCenter - nTextHeight;
AGrid.Canvas.TextRect(Rect,X,Y,sLine1);[COLOR=green]
// Line 2[/color]
sLine2 := Copy(sText,nPos+2,999);
Rect2.Left := Rect.Left;
Rect2.BottomRight := Rect.BottomRight;
Rect2.Top := nVertCenter + 1;
nTextWidth := AGrid.Canvas.TextWidth(sLine2);
X := nHorCenter - (nTextWidth div 2);
Y := nVertCenter;
AGrid.Canvas.TextRect(Rect2,X,Y,sLine2);
end
else
begin[COLOR=green]
// Single line:[/color]
nTextWidth := AGrid.Canvas.TextWidth(sText);
nTextHeight := AGrid.Canvas.TextHeight(sText);
X := nHorCenter - (nTextWidth div 2);
Y := nVertCenter - (nTextHeight div 2) - 1;
AGrid.Canvas.TextRect(Rect,X,Y,sText);
end;
finally
AGrid.Canvas.Brush.Color := zSaveBackgroundColor;
AGrid.Canvas.Font.Color := zSaveForegroundColor;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.RowHeights[0] := 36;
StringGrid1.Cells[1,0] := 'Price' + #13#10 + 'USD';
StringGrid1.Cells[2,0] := 'Net';
end;
procedure TForm1.StringGrid1DrawCell(
Sender: TObject;
ACol, ARow: Integer;
Rect: TRect;
State: TGridDrawState);
begin
if (ARow = 0) then
CenterTwoLineText(StringGrid1, Rect, ACol, ARow );
end;
Tried to assign Cells[x, 0] := 'Price' + #13+#10 + 'USD'...
if not Programming = 'Severe Migraine' then
ShowMessage('Eureka!');