1

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.

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • 1
    When are you calling `FillCell`? In response to which event? `OnDrawCell` provides draw state. – David Heffernan Aug 12 '16 at 10:23
  • OnDraw. I know what you are going to suggest: to pass State as parameter. But I want to know if it is possible to find the State without passing it as parameter. – Gabriel Aug 12 '16 at 10:25
  • 1
    Why on earth would you want to do that? You already have the correct solution. Pass the parameter. – David Heffernan Aug 12 '16 at 10:25
  • @DavidHeffernan-That's my current solution. I was just curios. I looked in VCL.Grids.pas, trying to figure out how to do it and I couldn't. – Gabriel Aug 12 '16 at 10:28
  • Why not store the current selected row in the OnSelectCell event? Then use the stored rownr when needed. – Johan Aug 12 '16 at 10:36
  • @Johan-What I don't understand is how TStringGrid is obtaining the State information (in OnDraw event). There must be a function that returns this information. If the TStringGrid has access to this info, I should be able to obtain it also (unless the function is Private). – Gabriel Aug 12 '16 at 10:41
  • @SolarWind If you have the VCL source code: Set a breakpoint in your OnDrawCell handler and look in the call stack where and how the State parameter gets assigned. – Uli Gerhardt Aug 12 '16 at 11:32

1 Answers1

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490