Re: Delphi stringgrid focus color

Posted by webmaster Guido on August 17, 2003

In Reply to: annoying stringgrid focus posted by Ed Marmier, p12332 on August 12, 2003

I am using Delphi's Stringgrid to display data. The system seems to have the habit of setting the focus on the upper lefthand cell [0,0] and coloring it dark blue.This obfuscates what i want to show in that cell, and it also disturbs the appearance of the Stringgrid.

How can I turn this focussing off without setting the Defaultdrawing property of the Stringgrid to false?

The default behavior of a Delphi stringgrid is as follows:

- If the stringgrid is focused (if it is the active component), then a focus rectangle is drawn around the *selected* cell and its colors are the default colors for the stringgrid, usually black text on a white background. At startup of the program, this is the topleft non-fixed cell: cell [0,0] if there is no fixed row nor fixed column, with 1 fixed column and 1 fixed row this is cell[1,1], and so on...

- But if the stringgrid is not focused, the selected cell is shown without a focus rectangle, but with the system default highlight colors, usually white text on a dark blue background.

To avoid the "highlight" coloring (white-on-dark-blue) of a cell, we can redraw that cell in another color ourselves in the OnDrawCell event handler. By the way, you don't have to set the Defaultdrawing property to "false"; even with its default state of "true", you get the OnDrawCell event. An example:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
  with StringGrid1.Canvas do begin
    // Fill rectangle with background color of grid
    Brush.Color := StringGrid1.Color;
    // Next, draw the text in the rectangle
    Font.Color := StringGrid1.Font.Color;
    TextOut(Rect.Left + 2, Rect.Top + 2, StringGrid2.Cells[ACol, ARow]);