Board index » delphi » Re: Centering Text In a TStringGrid Cell?

Re: Centering Text In a TStringGrid Cell?


2005-11-18 01:33:24 AM
delphi62
mojoala writes:
Quote
How would I go about doing this?
Use a tDrawGrid and draw the text yourself ...
--
-Mike (TeamB)
 
 

Re: Centering Text In a TStringGrid Cell?

How would I go about doing this?
TIA
 

Re: Centering Text In a TStringGrid Cell?

"mojoala" <XXXX@XXXXX.COM>writes
Quote
How would I go about doing this?

TIA
Not a StringGrid, but the idea's the same:
procedure TUserListForm.DBGridDrawDataCell( Sender: TObject; const ARect:
TRect;
AField: TField; AState: TGridDrawState );
var
X : Integer;
S : String;
begin
With DBGrid, UndSource.DataSet do
begin
S := UpperCase( AField.AsString );
If AField.Alignment = taRightJustify then
X := ( ARect.Right - ARect.Left ) - Canvas.TextWidth( S ) - 2
else
If AField.Alignment = taCenter then
X := ( ( ARect.Right - ARect.Left ) div 2 ) - ( Canvas.TextWidth(
S ) div 2 )
else
X := 2;
ExtTextOut( Canvas.Handle, ARect.Left + X, ARect.Top + 2, ETO_OPAQUE
or ETO_CLIPPED,
@ARect, PChar( S ), Length( S ), nil );
end;
end;
 

Re: Centering Text In a TStringGrid Cell?

thank you to the both of you!
 

Re: Centering Text In a TStringGrid Cell?

Except I am not using a data-aware grid, I am using the TStringList.
 

Re: Centering Text In a TStringGrid Cell?

"mojoala" <XXXX@XXXXX.COM>writes
Quote
Except I am not using a data-aware grid, I am using the TStringList.
Then, assuming:
procedure TSomeForm.StringGridDrawCell( Sender: TObject; ACol, ARow:
Longint; ARect: TRect; AState: TGridDrawState );
Then one could do the following:
// Use TextRect() because of clipping within the respective Cell.
// Right-justify
SetTextAlign( StringGrid.Canvas.Handle, TA_RIGHT + TA_TOP );
StringGrid.Canvas.TextRect( ARect, ARect.Right - 2, ARect.Top + 2,
StringGrid.Cells[ ACol, ARow ] );
// Left-justify
SetTextAlign( StringGrid.Canvas.Handle, TA_LEFT + TA_TOP );
StringGrid.Canvas.TextRect( ARect, ARect.Left + 2, ARect.Top + 2,
StringGrid.Cells[ ACol, ARow ] );
// Center
S := StringGrid.Cells[ ACol, ARow ];
X := ( ( ARect.Right - ARect.Left ) div 2 ) - ( Canvas.TextWidth( S ) div
2 );
SetTextAlign( StringGrid.Canvas.Handle, TA_CENTER + TA_TOP );
StringGrid.Canvas.TextRect( ARect, ARect.Left + X, ARect.Top + 2, S );
 

Re: Centering Text In a TStringGrid Cell?

Thank you I will try that.
"Bill Edwards" <XXXX@XXXXX.COM>writes
Quote
"mojoala" <XXXX@XXXXX.COM>writes
news:437cdec1$XXXX@XXXXX.COM...
>Except I am not using a data-aware grid, I am using the TStringList.

Then, assuming:
procedure TSomeForm.StringGridDrawCell( Sender: TObject; ACol, ARow:
Longint; ARect: TRect; AState: TGridDrawState );
Then one could do the following:
// Use TextRect() because of clipping within the respective Cell.
// Right-justify
SetTextAlign( StringGrid.Canvas.Handle, TA_RIGHT + TA_TOP );
StringGrid.Canvas.TextRect( ARect, ARect.Right - 2, ARect.Top + 2,
StringGrid.Cells[ ACol, ARow ] );
// Left-justify
SetTextAlign( StringGrid.Canvas.Handle, TA_LEFT + TA_TOP );
StringGrid.Canvas.TextRect( ARect, ARect.Left + 2, ARect.Top + 2,
StringGrid.Cells[ ACol, ARow ] );
// Center
S := StringGrid.Cells[ ACol, ARow ];
X := ( ( ARect.Right - ARect.Left ) div 2 ) - ( Canvas.TextWidth( S ) div
2 );
SetTextAlign( StringGrid.Canvas.Handle, TA_CENTER + TA_TOP );
StringGrid.Canvas.TextRect( ARect, ARect.Left + X, ARect.Top + 2, S );