Stark
2010-06-14 09:45:30 UTC
I needed to center the text in some cells and align it to left on others. In
internet I found the routine below that does something different, but useful
anyway to me. I tried it and found a funny thing that confuses me: in the
procedure definition that is automatically produced would be ".. Rect:
TRect; " , but is changed to ".. ARect: TRect; ".
In the code, both Rect and ARect is used, and this is the only way the
routine works. In fact I tried initially to use "Rect" instead, but can't
compile correctly.
What's the secret behind this ?
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
ARect: TRect; State: TGridDrawState);
var
TextHt : integer; // height of word-broken text
WidthRect : TRect; // dummy for height calculation
begin
with StringGrid1, Stringgrid1.Canvas do
begin
if (gdFixed in State) then
Brush.Color := clInactiveBorder
else
if (gdSelected in State) then
begin
Brush.Color := clHighLight;
Brush.Color := clWindow;
Font.Color := clWindow;
end
else
Brush.Color := clWindow;
Canvas.FillRect(ARect);
if (gdFocused in State) then
DrawFocusRect(ARect);
InflateRect(ARect, -2, -2); // reduce cell rect for text margins in cell
{calculate height of word-broken text}
WidthRect := Rect(0, 0, ColWidths[ACol] - 4, 0);
TextHt := DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]),
-1, WidthRect, DT_CENTER or DT_WORDBREAK or
DT_CALCRECT);
if TextHt > RowHeights[ARow] - 4 then
RowHeights[ARow] := TextHt + 4;
if TextHt > TextHeight('X') + 2 then // + 2 for text leading
{multi-lines}
DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]),
-1, ARect, DT_CENTER or DT_WORDBREAK)
else
{only one line - so centre vertically}
DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]),
-1, ARect, DT_CENTER or DT_SINGLELINE or DT_VCENTER);
end;
end;
internet I found the routine below that does something different, but useful
anyway to me. I tried it and found a funny thing that confuses me: in the
procedure definition that is automatically produced would be ".. Rect:
TRect; " , but is changed to ".. ARect: TRect; ".
In the code, both Rect and ARect is used, and this is the only way the
routine works. In fact I tried initially to use "Rect" instead, but can't
compile correctly.
What's the secret behind this ?
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
ARect: TRect; State: TGridDrawState);
var
TextHt : integer; // height of word-broken text
WidthRect : TRect; // dummy for height calculation
begin
with StringGrid1, Stringgrid1.Canvas do
begin
if (gdFixed in State) then
Brush.Color := clInactiveBorder
else
if (gdSelected in State) then
begin
Brush.Color := clHighLight;
Brush.Color := clWindow;
Font.Color := clWindow;
end
else
Brush.Color := clWindow;
Canvas.FillRect(ARect);
if (gdFocused in State) then
DrawFocusRect(ARect);
InflateRect(ARect, -2, -2); // reduce cell rect for text margins in cell
{calculate height of word-broken text}
WidthRect := Rect(0, 0, ColWidths[ACol] - 4, 0);
TextHt := DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]),
-1, WidthRect, DT_CENTER or DT_WORDBREAK or
DT_CALCRECT);
if TextHt > RowHeights[ARow] - 4 then
RowHeights[ARow] := TextHt + 4;
if TextHt > TextHeight('X') + 2 then // + 2 for text leading
{multi-lines}
DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]),
-1, ARect, DT_CENTER or DT_WORDBREAK)
else
{only one line - so centre vertically}
DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]),
-1, ARect, DT_CENTER or DT_SINGLELINE or DT_VCENTER);
end;
end;