Discussion:
Align text to center of a cell in a tstringlist.
(too old to reply)
Cooper
2010-03-30 19:17:26 UTC
Permalink
Hello,
what i can align text to center in a tstringgrid? Me not found property for
to do it :( I use d2010.
Thanks, again.
Cooper.
Jamie
2010-03-31 00:14:12 UTC
Permalink
Post by Cooper
Hello,
what i can align text to center in a tstringgrid? Me not found property
for to do it :( I use d2010.
Thanks, again.
Cooper.
There is no property for that as far as I now..

You can how ever, use the "OwnerDraw" style of a TstringGrid and then
perform your own drawing. With that, you can use the "DrawText" using the
DT_CENTER flag. This is a windows function and found in the windows help.

P.S.
You need to write a OnMeasureItem and OnOwnerDraw event..

something for you to work on. :)
Cooper
2010-03-31 09:26:32 UTC
Permalink
Post by Jamie
There is no property for that as far as I now..
You can how ever, use the "OwnerDraw" style of a TstringGrid and then
perform your own drawing. With that, you can use the "DrawText" using the
DT_CENTER flag. This is a windows function and found in the windows help.
P.S.
You need to write a OnMeasureItem and OnOwnerDraw event..
something for you to work on. :)
Forgive me :( but from a side i have understood about you wrote but from
other side, me not found much about as to do, too searching in property of
tstringgrid.
Can i ask you about some link or resource where i can learn to do it?
Thanks very much.
Cooper.
Jamie
2010-03-31 23:44:58 UTC
Permalink
Post by Cooper
Post by Jamie
There is no property for that as far as I now..
You can how ever, use the "OwnerDraw" style of a TstringGrid and then
perform your own drawing. With that, you can use the "DrawText" using the
DT_CENTER flag. This is a windows function and found in the windows help.
P.S.
You need to write a OnMeasureItem and OnOwnerDraw event..
something for you to work on. :)
Forgive me :( but from a side i have understood about you wrote but from
other side, me not found much about as to do, too searching in property
of tstringgrid.
Can i ask you about some link or resource where i can learn to do it?
Thanks very much.
Cooper.
Implement the OnDrawCell Event and try this code below..

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
Rect: TRect; State: TGridDrawState);
Var
B:TBrush;
S:string;
begin
if gdFixed in State then exit;// don't do the borders.
B := TBrush.create;
B.Assign(Canvas.Brush);
WIth TStringGrid(Sender) Do
Begin
S := cells[col,row]; // we don't have any strings in the cells.
S :='test'; // make a dummy string.
If gdSelected in State Then
Begin
Canvas.Brush.Color := $007F4580;
Canvas.Brush.Style := bsSolid;
Canvas.fillRect(Rect);
End;
Canvas.Brush.style := bsClear;
Canvas.Font.Color := $800000+(256*Trunc(256))+Trunc(255*random);
DrawText(Canvas.Handle,Pchar(s),Length(S),
Rect, DT_CENTER);
End; // End of "WITH"

end;
Cooper
2010-04-02 14:55:00 UTC
Permalink
Post by Jamie
Implement the OnDrawCell Event and try this code below..
procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
Rect: TRect; State: TGridDrawState);
Var
B:TBrush;
S:string;
begin
if gdFixed in State then exit;// don't do the borders.
B := TBrush.create;
B.Assign(Canvas.Brush);
WIth TStringGrid(Sender) Do
Begin
S := cells[col,row]; // we don't have any strings in the cells.
S :='test'; // make a dummy string.
If gdSelected in State Then
Begin
Canvas.Brush.Color := $007F4580;
Canvas.Brush.Style := bsSolid;
Canvas.fillRect(Rect);
End;
Canvas.Brush.style := bsClear;
Canvas.Font.Color := $800000+(256*Trunc(256))+Trunc(255*random);
DrawText(Canvas.Handle,Pchar(s),Length(S),
Rect, DT_CENTER);
End; // End of "WITH"
end;
Thanks very much, working on it i have solved problem for my particular
problem.
Cooper
2010-04-04 19:31:53 UTC
Permalink
"Jamie" <***@charter.net> ha scritto nel
messaggio news:nfQsn.304158$***@newsfe25.iad...
cut
I have customized your procedure doing so:

Procedure TMainForm1.StringGrid2DrawCell (Sender: TObject; Col, Row:
Integer; Rect: TRect; State: TGridDrawState);
Var Index: Byte; Str: String;
Begin
For Index := 0 to 4 do
Begin
If gdFixed in State then Exit;
Rect := StringGrid2.CellRect (Index, 0);
Rect.Top := Rect.Top + 3;
Case Index Of
0: Str := 'Text One';
1: Str := 'Text Two';
2: Str := 'Text Three';
3: Str := 'Text Four';
4: Str := 'Text Five';
End;
WIth TStringGrid (Sender) Do
Begin
Canvas.Brush.Style := bsClear;
DrawText(Canvas.Handle,Pchar (Str), Length(Str), Rect,
DT_CENTER);
End;
End;
End;

When i run application in stringgrid all is displayed correctly but problem
is that when change cells with mouse, or i navigate in grid then cell (0,0)
lost text; and all normal with all other cell (0,x).
Can i can fix this problem?
Other small thing: can i to do in mode that cell not display as selected?
What property i need use?
Jamie
2010-04-05 07:56:45 UTC
Permalink
Post by Cooper
cut
Integer; Rect: TRect; State: TGridDrawState);
Var Index: Byte; Str: String;
Begin
For Index := 0 to 4 do
Begin
If gdFixed in State then Exit;
Rect := StringGrid2.CellRect (Index, 0);
Rect.Top := Rect.Top + 3;
Case Index Of
0: Str := 'Text One';
1: Str := 'Text Two';
2: Str := 'Text Three';
3: Str := 'Text Four';
4: Str := 'Text Five';
End;
WIth TStringGrid (Sender) Do
Begin
Canvas.Brush.Style := bsClear;
DrawText(Canvas.Handle,Pchar (Str), Length(Str), Rect,
DT_CENTER);
End;
End;
End;
When i run application in stringgrid all is displayed correctly but
problem is that when change cells with mouse, or i navigate in grid then
cell (0,0) lost text; and all normal with all other cell (0,x).
Can i can fix this problem?
Other small thing: can i to do in mode that cell not display as
selected? What property i need use?
Because I skipped the fixed cells..
"If gdFixed in State then exit"

The state variable is there to report the type of cell.

those are normally the ones on the left and top borders.

which fall into the 0x? or ?x0 ranges..

When ever you receive a "gdFixed" check the COL and ROW to see if
either of those are set to "0" ? If so, you know you're receiving a
border "fixed" cell update..

I guess one could always use the "DrawCell" if one does not want to
process some cells.. You most likely would do that some where while in
the "OnDrawCell" event.. Just my guess...

If you look at the TcustomGrid class, you'll find lots of goodies!
Cooper
2010-04-04 21:09:56 UTC
Permalink
Post by Jamie
Post by Cooper
When i run application in stringgrid all is displayed correctly but
problem is that when change cells with mouse, or i navigate in grid then
cell (0,0) lost text; and all normal with all other cell (0,x).
Can i can fix this problem?
Other small thing: can i to do in mode that cell not display as selected?
What property i need use?
Because I skipped the fixed cells..
"If gdFixed in State then exit"
Ok solved about first problem, thanks :) But remain problem that cell (0,0)
when i navigate in other cell change style from bold to normal and other
cell of row 0 remaing same, can i solve it?
About as remove focus from grid cells, not understood as to do :( Forgive
me, can to take patience, can tell me better?
Jamie
2010-04-05 09:37:07 UTC
Permalink
This post might be inappropriate. Click to display it.
a***@aol.com
2010-04-05 10:57:02 UTC
Permalink
Cooper

Delphi Help on TStringGrid gives a full description of drawing on the
stringgrid.

Here is some background on drawing on a StringGrid. (read SG below as
StringGrid)

If TSG.DefaultDrawing is true then the recrangle is brushed, then
TSG.OnDrawCell is called, and then the selection rectangle is drawn
for the selected cell. Otherwise you draw all the contents of a cell
yourself.

You can find the cell on which the mouse has rested by using
TSG.MouseCoords(). note that the coordinates passed are _screen_
coordinates. So to get these from the Rect in TSG.OnDrawCell use ...

var
MidPoint, MouseCellPoint : TPoint;
MouseCol, MouseRow : integer;

MidPoint := TPoint((Rect.Right-Rect.Left) div 2, (Rect.Bottom-
Rect.Top) div 2);
MidPoint := MySG.ClientToScreen(MidPoint);
MouseCellPoint := MySG.MouseCoords(MidPoint)
MouseCol := MouseCellPoint.X;
MouseRow := MouseCellPoint.Y;

If you want different drawing for different columns or rows, you can
construct a case statement to draw as you wish.

OnDrawCell(... State...) is a set - ie a collection of integer values
(often of enumerated values as in State, you can find if it has
certain values by using the in operator for single values, or the
= (contains) operator or the <= (Is contained in). You normally want
to know only if the cell is either gdFixed or gdSelected.

To draw background colours set MySG.Canvas.Brush.Color (usually
conditional on gdSelected), Then use MySG.Canvas.FillRect.

Then use Windows DrawText passing the MySG.Handle, the string typecast
to a PChar (or a PWideChar if appropriate), Set the string length to
-1. then the length is taken as where the zero PChar terminator is.
Use the OnDrawCell(...Rect...) for the DrawText(...Rect...). Set the
format to DT_LEFT, DT_CENTER, or DT_RIGHT. If you want the text
vertically centred add "or DT_SINGLELINE or DT_VCENTER" to the format.

Alternatively to using windows DrawText, work out the text size from
MySG.Canvas.TextWidth & MySG.Canvas.TextHeight, find the X & Y
coordinates from the text sizes and the Rect parameter of OnDrawCell
(note that text is positioned by the left & top of the text). Then
call MySG.Canvas.TextOut().

With TSG.Canvas.TextOut, X is 2 (or whatever margin) for left,
(Rect.Right + Rect.Left + TextWidth) div 2 for centred, and RectRight
- 2 - TextWidth for right align. Similarly for Y using Rect.Top,
Rect.Bottom & TextHeight.

Read Delphi Help, Read Delphi Help, Read Delphi Win Help <g>

Have fun <g>.

Alan Lloyd

Cooper
2010-03-31 10:59:48 UTC
Permalink
Post by Jamie
There is no property for that as far as I now..
You can how ever, use the "OwnerDraw" style of a TstringGrid and then
perform your own drawing. With that, you can use the "DrawText" using the
DT_CENTER flag. This is a windows function and found in the windows help.
P.S.
You need to write a OnMeasureItem and OnOwnerDraw event..
something for you to work on. :)
Searching in google, i have tried to write this function:

Procedure TForm1.WriteAlignTextCell (Sender: TObject; ACol, ARow: Integer;
Text: String; Align: Cardinal);
Var SetAlign: Word; Rect: TRect;
Begin
SetAlign := SetTextAlign (StringGrid1.Canvas.Handle, Align);
Rect := StringGrid1.CellRect (ACol, ARow);
StringGrid1.Canvas.TextRect (Rect, Rect.Left + (Rect.Right -
Rect.Left) Div 2, Rect.Top + 2, Text);
SetTextAlign (StringGrid1.Canvas.Handle, SetAlign);
StringGrid1.Cells [ACol, ARow] := Text;
End;

Calling it with:

WriteAlignTextCell (Sender, 1, 1, 'MyString', TA_CENTER);

But i continue to display 'MyString' every to left, and not to center.
Where i have mistaked?
Thanks you very much.
Cooper.
Cooper
2010-03-31 11:06:11 UTC
Permalink
Correct:
Searching in google, i have tried to write this procedure:
Cooper
2010-03-31 11:16:12 UTC
Permalink
"Cooper" <***@blueware.it> ha scritto nel messaggio news:EWFsn.41438$***@twister2.libero.it...
Sorry, In last post wrote wrong, try to correct here (about last post, mine
idea was to do procedure for align to left, center, right a text in a cell,
but was incomplete); try only with center text, just only for understand
where i mistake.

Then i told, searching in google, i have tried to write this procedure:

Procedure TForm1.WriteCenterAlignTextCell (Sender: TObject; ACol, ARow:
Integer; Text: String);
Var SetAlign: Word; Rect: TRect;
Begin
SetAlign := SetTextAlign (StringGrid1.Canvas.Handle, TA_CENTER);
Rect := StringGrid1.CellRect (ACol, ARow);
StringGrid1.Canvas.TextRect (Rect, Rect.Left + (Rect.Right -
Rect.Left) Div 2, Rect.Top + 2, Text);
SetTextAlign (StringGrid1.Canvas.Handle, SetAlign);
StringGrid1.Cells [ACol, ARow] := Text;
End;

Calling it with:

WriteCenterAlignTextCell (Sender, 1, 1, 'MyString');

But i continue to display 'MyString' every to left, and not to center.
Where i have mistaked?
Thanks you very much.
Cooper.
ap
2010-03-31 14:46:44 UTC
Permalink
Post by Cooper
Integer; Text: String);
My humble opinion is that you should not spend you time and energy
trying to write yet another new Delphi TStringGrid descendants to this
world..

There are several free alternatives available. They are able to do the
Aligning and other things right out of the box. My old time favourite
was TStringAlignGrid, though I have not needed to use it for several
years.
http://www.torry.net/quicksearchd.php?String=tstringaligngrid&Title=Yes

The German speaking fellows say that TStringAlignGrid should work also
with D2010
http://groups.google.com/group/de.comp.lang.delphi.misc/browse_thread/thread/a42490a822636ccf

Try to locate the newest version of this, or some other Align capable
descendant Grid component.
Then it starts to be about time to get prepared for Eastern...

ap
Cooper
2010-03-31 14:46:52 UTC
Permalink
Post by ap
There are several free alternatives available. They are able to do the
Aligning and other things right out of the box. My old time favourite
was TStringAlignGrid, though I have not needed to use it for several
years.
http://www.torry.net/quicksearchd.php?String=tstringaligngrid&Title=Yes
Saw it, but i see too that is until Delphi 6. I use delphy 2010, work too?
Post by ap
The German speaking fellows say that TStringAlignGrid should work also
with D2010
http://groups.google.com/group/de.comp.lang.delphi.misc/browse_thread/thread/a42490a822636ccf
Ok, but translating with google, from a side tell me that work correctly but
tell too that give some problem :( so not understand all :(
Post by ap
Try to locate the newest version of this, or some other Align capable
descendant Grid component.
Then it starts to be about time to get prepared for Eastern...
Where i can find other? can i ask you some other link for d2010?
Thanks you very much.
Cooper
2010-03-31 15:51:56 UTC
Permalink
Post by ap
My humble opinion is that you should not spend you time and energy
trying to write yet another new Delphi TStringGrid descendants to this
world..
There are several free alternatives available. They are able to do the
Aligning and other things right out of the box. My old time favourite
was TStringAlignGrid, though I have not needed to use it for several
years.
http://www.torry.net/quicksearchd.php?String=tstringaligngrid&Title=Yes
The German speaking fellows say that TStringAlignGrid should work also
with D2010
http://groups.google.com/group/de.comp.lang.delphi.misc/browse_thread/thread/a42490a822636ccf
Try to locate the newest version of this, or some other Align capable
descendant Grid component.
Then it starts to be about time to get prepared for Eastern...
ap
I have tried to install it, but i have impression that not work; in sense
not give me some error message, but compiling and installing it, i display
this component in samples category, when i drop it, i display it in form but
after looking in property, and in form, nothing that help me; in sense that
in form i don't see grid; and in property not see nothing that allow me to
connect this to tstringgrid.
So i ask you what i can to do.
Thanks you again.
Cooper.
ap
2010-03-31 18:23:39 UTC
Permalink
Post by Cooper
in form i don't see grid; and in property not see nothing that allow me to
connect this to tstringgrid.
So i ask you what i can to do.
I'm not able to help you very thoroughly with any of the D2010 problems.
We are steadily stuck to pure Ansi versions of Delphi only.

At least this KGrid v.1.4 says it knows Delphi 2010 version also
http://www.torry.net/authorsmore.php?id=5878

It may do what you want to do or then it does not. Good luck.

ap
Loading...