Hi Tom,
I know this is a DBGrid but it's all the same...so to speak.
This is the quickest example i could find of alternating colors on a DBGrid and
will work the same on a stringgrid. This is very similar to what i think your
wanting to do. Your controling the background color based on your defined
criteria. I didn't see that code in your example.
DBGrid1.DefaultDrawing:=True
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect; Field: TField; State: TGridDrawState);
begin
with TDBGrid(Sender) do begin
if SelectedRows.IndexOf(DataSource.Dataset.Bookmark) >= 0 then
Canvas.Brush.Color := clPurple
else if gdSelected in State then
Canvas.Brush.Color := clHighlight
else if (DataSource.Dataset.RecNo and 1) <> 0 then
Canvas.Brush.Color := $00DDEEFF
else
Canvas.Brush.Color := $00DDFFFF;
DefaultDrawDataCell(Rect, Field, State);
end;
end;
The above takes care of all drawing. I'm overriding the drawing and using my
code rather than the default drawing.
Hope that helps...if not let me know and i can write up an example more to you
need.
Cheers,
Charlie
----- Original Message -----
From: Nesler, Thomas J
To: [email protected]
Sent: Tuesday, April 24, 2007 8:40 AM
Subject: [delphi-en] Setting background color of String grid row
Hello!
I am trying to highlight certain rows of a string grid based on
different criteria. Whenever I work with the Win32 interface, I always
get confused as to what is going on and what I should do. I realize I
need to use the OnDrawCell event to take over the painting of the cell,
but I can't seem to get the procedure to leave in the default attributes
when I don't want to override the standard colors.
Another point of confusion: If I Say Default drawing = false, does that
mean I can turn on Default drawing after I am finished doing my custom
work?
All the examples I have seen so far, are very poorly documented (IMO)
so they don't explain what is going on at all. Does anyone have a
sample of code to do this?
Here is my routine:
procedure TForm1.XStrGrDailyDrawCell(Sender: TObject; ACol, ARow:
Integer;
Rect: TRect; State: TGridDrawState);
Var
OldColor : TColor;
OldBrush : TBrush;
begin
with canvas do
begin
OldColor := Font.Color; //Save default Font color
OldBrush := Brush; //Save default Brush
brush.Style := bssolid;
If ((ACol < XStrGrDaily.Fixedcols) OR (ARow < XStrGrDaily.FixedRows))
then //Ignore fixed rows
If ApptArray[ARow] = False //Array that turns on color when true or
turns off color when false
then
Begin
Brush.Color := clwindow;
canvas.Font.Style := [];
end
else
Begin
Brush.Color := clBlue;
canvas.Font.Style := [fsbold];
end;
FillRect(Rect);
SetBkMode(Canvas.Handle,Transparent);
TextOut(Rect.left+2,Rect.Top+2,XStrGrDaily.Cells[acol,arow]); //paint
Cell
Font.color := OldColor;
brush := OldBrush;
end;
end;
Thanks!
Tom Nesler
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]