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