Board index » delphi » how to get the each cell's color of the StringGrid??

how to get the each cell's color of the StringGrid??


2003-11-14 12:59:44 PM
delphi85
Hi,
i wrote some code like follow:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (ACol=1) and (Arow=1) then
begin
StringGrid1.Canvas.Brush.Color := clred;
StringGrid1.Canvas.FillRect (Rect);
end
else
if (ACol=1) and (Arow=2) then
begin
StringGrid1.Canvas.Brush.Color := clblue;
StringGrid1.Canvas.FillRect (Rect);
end
else
if (ACol=1) and (Arow=3) then
begin
StringGrid1.Canvas.Brush.Color := clyellow;
StringGrid1.Canvas.FillRect (Rect);
end
end;
and then, i can I get back the specifically cell's color i click?
ari
 
 

Re:how to get the each cell's color of the StringGrid??

"ari" <XXXX@XXXXX.COM>writes:
Quote
Hi,
i wrote some code like follow:
[clip]
Quote
and then, i can I get back the specifically cell's color i click?
When you see the click, you know the cell's column and row. So do it
the same way you decided what colors to use in painting.
You might want to use a more structured approach that uses a single
function which is passed a row and column and returns the color to be
used for the cell. That function can be shared by the OnDrawCell and
OnClick event handlers to determine the color.
Good luck.
Kurt
 

Re:how to get the each cell's color of the StringGrid??

"Kurt Barthelmess (TeamB)" <XXXX@XXXXX.COM>bl
Quote
You might want to use a more structured approach that uses a single
function which is passed a row and column and returns the color to be
used for the cell. That function can be shared by the OnDrawCell and
OnClick event handlers to determine the color.

Good luck.

Kurt

Thanks for Kurt's answer!
And In my actual program, the cell's color is dynamic setted by user's mouse
click. And I can use a list to record the every cells's color, but, is it
any easy way to reach my goal?
regards,
Ari
 

Re:how to get the each cell's color of the StringGrid??

Since your are drawing the color directly in the DrawCell event the cell
color does not exists anywhere except on the screen.
So the only way to get the color is to look at the screens DC when there is
a mouse click.
This is possible to do, but really no worth doing.
I suggest you follow Kurt's advice to make a function to return the cell
color and use this bot in OnDrawCell and OnClick event.
Come to think of it, there is another way to do this:
The stringgrid has a Objects property where you could store each cells color
value initially and then use this value in both OnDrawCell and OnClick
event. But it is really just a variation of Kurt's suggestion.
--
Finn Tolderlund
"ari" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
StringGrid1.Canvas.Brush.Color := clred;
StringGrid1.Canvas.FillRect (Rect);
and then, i can I get back the specifically cell's color i click?
 

Re:how to get the each cell's color of the StringGrid??

"ari" <XXXX@XXXXX.COM>writes:
Quote
And In my actual program, the cell's color is dynamic setted by user's mouse
click. And I can use a list to record the every cells's color, but, is it
any easy way to reach my goal?
That doesn't seem possible. You must know, in your drawing code, what
color to draw, based on column and row. Otherwise if you are asked to
redraw later, you won't draw it correctly. Suppose you drew a cell in
red. If another window (even from another application) pops up over
that cell and then is closed, you will be asked to draw that cell
again. You can't look at the "old color" or a pixel on the screen,
because that has long since been replaced. So the color has to be
"saved" somewhere.
A TStringGrid does have an Objects property which is indexed by column
and row. While the entries are officially declared as being of type
TObject, you are free to store any 32 bit value there. You could use
that to store the TColor for each cell at the time you first determine
the color (or whenever it changes) by writing:
MyStringGrid.Objects[SomeCol, SomeRow] :=
TObject(clRed);
Then in your drawing code, write:
procedure TForm1.StringGrid1DrawCell(Sender: TObject;
ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with Sender as TStringGrid do
begin
Canvas.Brush.Color := TColor(Objects[ACol, ARow]);
FillRect(Rect);
end;
...
end;
Good luck.
Kurt