Try
this - a bit more long winded byt it works, and you have full control over
everything }
unit
Unit26;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Form1: TForm1;
implementation
{$R
*.DFM}
procedure TForm1.Button1Click(Sender:
TObject);
Var
Xpos, YPos : SmallInt;
begin
{ Setup ROWS and COLS to be 6 wide/high }
StringGrid1.ROwCount := 6;
StringGrid1.ColCount := 6;
Var
Xpos, YPos : SmallInt;
begin
{ Setup ROWS and COLS to be 6 wide/high }
StringGrid1.ROwCount := 6;
StringGrid1.ColCount := 6;
{ Fill the cells with 1 Number such as 1:1 2:1
etc... }
for xPos := 0 to 6 do
for YPos := 0 to 6 do
StringGrid1.Cells[XPos, YPOs] := IntTostr(XPOs+1)+':'+IntTostr(YPOs+1);
end;
for xPos := 0 to 6 do
for YPos := 0 to 6 do
StringGrid1.Cells[XPos, YPOs] := IntTostr(XPOs+1)+':'+IntTostr(YPOs+1);
end;
procedure TForm1.FormCreate(Sender:
TObject);
begin
{ Disable the default drawing of the string grid }
StringGrid1.DefaultDrawing := FALSE;
end;
begin
{ Disable the default drawing of the string grid }
StringGrid1.DefaultDrawing := FALSE;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject;
ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1.Canvas do
begin
{ if the cell is selected, or focused then we use the highlight color for the cell }
if (gdSelected in State) or (gdFocused in State) then
brush.Color := clHighlight
{ if the cell is FIXED then we just use the color of the face of a button }
else if (gdFixed in State) then
brush.Color := clBtnFace
{ else it is just the window color - or any other color you want }
else
Brush.Color := clWindow;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1.Canvas do
begin
{ if the cell is selected, or focused then we use the highlight color for the cell }
if (gdSelected in State) or (gdFocused in State) then
brush.Color := clHighlight
{ if the cell is FIXED then we just use the color of the face of a button }
else if (gdFixed in State) then
brush.Color := clBtnFace
{ else it is just the window color - or any other color you want }
else
Brush.Color := clWindow;
{ Fill the cell with the color
from above }
FillRect(Rect);
FillRect(Rect);
{ If the cell is fixed, then
make it look 3d }
if (gdFixed in State) then
begin
DrawEdge(Stringgrid1.Canvas.Handle, Rect, BDR_RAISEDINNER, BF_ADJUST or BF_TopRight);
DrawEdge(Stringgrid1.Canvas.Handle, Rect, BDR_RAISEDINNER, BF_ADJUST or BF_BOTTOMLEFT);
end;
if (gdFixed in State) then
begin
DrawEdge(Stringgrid1.Canvas.Handle, Rect, BDR_RAISEDINNER, BF_ADJUST or BF_TopRight);
DrawEdge(Stringgrid1.Canvas.Handle, Rect, BDR_RAISEDINNER, BF_ADJUST or BF_BOTTOMLEFT);
end;
{ If the cell is in the last
row, and the row is > 0 then set the color of the font to blue
}
if (ACol = StringGrid1.ColCount-1) and (ARow > 0) then
Font.Color := clBlue
else { Set the color of the font to black for the rest of the text }
Font.Color := clBlack;
{ But !!! if the cell is focused or Selected then we should draw it in clHighlightText so
we can see it
}
if (gdSelected in State) or (gdFocused in State) then
Font.Color := clHighlightText;
if (ACol = StringGrid1.ColCount-1) and (ARow > 0) then
Font.Color := clBlue
else { Set the color of the font to black for the rest of the text }
Font.Color := clBlack;
{ But !!! if the cell is focused or Selected then we should draw it in clHighlightText so
we can see it
}
if (gdSelected in State) or (gdFocused in State) then
Font.Color := clHighlightText;
{ Draw the text in the cell -
you could draw anything in there }
TextRect(Rect, Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]);
end;
end;
TextRect(Rect, Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]);
end;
end;
end.
Christopher Crowe (Software Developer)
Microsoft MVP,
MCP
Adrock Software
Byte Computer & Software LTD
P.O Box
13-155
Christchurch
New Zealand
Phone/Fax (NZ)
03-3651-112
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Steve Galyer
Sent: Tuesday, 18 May 1999 14:13
To: Multiple recipients of list delphi
Subject: [DUG]: TStringGrid OnDrawCell EventHi,This should be simple and straight forward, but I can't see the wood for the trees.I want to display the text in the last column in my string grid using a blue font - the following code snippet worksIf ACol = LastColumn then
TStringGrid(Sender).Font.Color:= clBlue
else
TStringGrid(Sender).Font.Color:= clBlack;BUTthe grid appears to continue drawing as it continues to flicker - how do I stop this?Thanks in advanceSteve Galyer