Search code examples
delphigraphicsvcldelphi-xe7tstringgrid

TStringGrid: Is it possible to find out the 'State' of a cell?


I want to implement a FillCell procedure for TStringGrid. I want to fill a certain cell with a color but only when the cell (row) is not selected.

procedure TMyStrGrid.FillCell(Rect: TRect; aColor: TColor);
begin
 //if NOT (gdSelected in State) then    <---- how do I obtain the 'State' here?
   begin
    Canvas.Brush.Color:= aColor;
    Canvas.FillRect(Rect);
   end;
end;

This is just an exercise :) I am trying to figure out VCL.Grids.pas which is quite complex.


Solution

  • According to the comments, you are calling this function from an OnDrawCell handler. That OnDrawCell handler is passed a TGridDrawState argument which specifies whether or not the cell is selected. The event handler is of this form:

    TDrawCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
      Rect: TRect; State: TGridDrawState) of object;
    

    You are asking whether it is possible to ignore the TGridDrawState and somehow recover the information later. In principle it is possible:

    • You have available the row and column. That identifies the cell and you can check whether or not the cell is in the current selection.
    • If you want to ignore the row and column also, then you could inspect the TRect that is supplied. Map that back to the row and column and again check that against the current selection.

    Frankly what you are trying to do is silly in my view. You have been supplied with the draw state for a good reason. It has the information you need. Use it.