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.
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.
Correct:
Searching in google, i have tried to write this procedure:
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');
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
> 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 :(
> 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.
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.
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
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.
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?
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!
If I understand you correctly, you need to force the remaining cells
that are in the 0 row, to update so that they can also display the
new "FONT" change ?
So, I take it that you would like all the cells on the border to use
the same FONT type, and show that immediately ?
You can do a complete repaint of the control at some point..
MyStringGrid.Repaint. You can do that after you make the change to
the font.
or you could also do this.
MystringGrid.Invalidate;
That will force an update for the control at some point when windows
gets around to it.
If you only what to update portion, then more advanced methods are
used which involves passing a RECT object to specify the area to update.
Hope that is what you wanted..
Jamie.
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