Board index » delphi » Specifying color to a specific cell in TStringGrid

Specifying color to a specific cell in TStringGrid

Given a color cell (x,y), how do I change the background color for that cell
only?  Also, what if I wanted to use a custom color which is not available
on the standard color palette?

Brad

 

Re:Specifying color to a specific cell in TStringGrid


Quote
brad huff wrote:
> Given a color cell (x,y), how do I change the background color for that cell
> only?  Also, what if I wanted to use a custom color which is not available
> on the standard color palette?

You need to write your own ondraw event.

--
                                       Andreas
Who doesn't live can never die

Re:Specifying color to a specific cell in TStringGrid


Quote
> Given a color cell (x,y), how do I change the background color for that
cell
> only?  Also, what if I wanted to use a custom color which is not available
> on the standard color palette?

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
const myColor = $00134217; // set the value of your color here...

var
  grid: TStringGrid;
begin
  if ACol = 3 then
    if ARow = 3 then
    begin
      StringGrid1.Canvas.Brush.Color := myColor;
      StringGrid1.Canvas.FillRect(Rect);
      StringGrid1.Canvas.TextOut(Rect.Left+2, Rect.Top+2,
StringGrid1.Cells[ACol,ARow]);
      StringGrid1.Canvas.Brush.Color := clWindow;
    end;
end;

Re:Specifying color to a specific cell in TStringGrid


Quote
In article <3baf612c_1@dnews>, Brad huff wrote:
> Given a color cell (x,y), how do I change the background color for that cell
> only?  

Use the OnDrawCell event. The event fires for every cell that needs to be
drawn and when it fires the cell is already fully drawn. So you can check
which cell you are asked to draw by checking the acol and arow parameters of
the event. If it is not "your" cell, you simply do nothing, otherwise you set
the grids canvas.brush to the color you want, then use the Canvas.Fillrect and
TextRect methods to fill the cell background and paint the text.

Quote
> Also, what if I wanted to use a custom color which is not available
> on the standard color palette?

You can use any color you like by specifying the color as a hex constant, e.g.
$C0FFFF (a pale yellow), or by using the RGB API function. What will be
displayed depends on the video modes color resolution and, if the video mode
uses a palette (256 color mode) on the palette contents.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Other Threads