5

I'm trying to hide the border (focus rectangle) which Delphi draws around the currently selected cell in a StringGrid. I'm doing owner drawing to customize the appearance of the string grid. I've managed to get rid of everything but the selection.

I was using

 GR.Left := -1;
 GR.Top  := -1;
 GR.Right := -1;
 GR.Bottom := -1;
 StringGrid.Selection := GR;

But that gives errors if you set this really fast (I have this running in onMouseMove). What I mean by that is it works fine, but if I call this particular chunk of code fast enough I get an exception in the rendering of the StringGrid (thus I can't just chuck an try except around it).

Any ideas on how I can solve this reliably?

Daisetsu
  • 4,846
  • 11
  • 50
  • 70
  • Hello Daietsu, have you tried TZColorStringGrid? It is a descendant of TStringGrid and it has abilities to personalize each cell. Maybe you can have a look: http://avemey.com/ – Cristian Vasuica May 23 '13 at 08:00

4 Answers4

3

You can use a interposer class for TStringgrid and override the Paint procedure to remove a drawn focus rect.

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TStringgrid=Class(Grids.TStringGrid)
  private
    FHideFocusRect: Boolean;
  protected
     Procedure Paint;override;
  public
     Property HideFocusRect:Boolean Read FHideFocusRect Write FHideFocusRect;
  End;
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TStringgrid.Paint;
var
 L_Rect:Trect;
begin
  inherited;
   if HideFocusRect then
      begin
       L_Rect := CellRect(Col,Row);
       if DrawingStyle = gdsThemed then InflateRect(L_Rect,-1,-1);
       DrawFocusrect(Canvas.Handle,L_Rect)
      end;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
   StringGrid1.HideFocusRect := not StringGrid1.HideFocusRect;
end;

end.
bummi
  • 27,123
  • 14
  • 62
  • 101
1

In the OnDrawCell event add

with Sender as TStringgrid do
begin
    if (gdSelected in State) then
    begin
        Canvas.Brush.Color := Color;
        Canvas.Font.Color := Font.Color;
        Canvas.TextRect(Rect, Rect.Left +2,Rect.Top +2, Cells[Col,Row]);
    end;
end;

In the OnSelectCell event add

CanSelect := False

sav
  • 2,064
  • 5
  • 25
  • 45
1

Inside Stringrid properties change TabOrder to -1 and HitTest to False

0

Set DefaultDrawing property to false and onDrawCell event draw the text something like this (this also alternates row color and center text in cell):

procedure GridDrawCell(Sender: TObject; ACol,ARow: Integer; Rect: TRect; State: TGridDrawState);
var S: string;
        c:TColor;
        SavedAlign: word;
begin
  S := Grid.Cells[ACol, ARow];
  SavedAlign := SetTextAlign(Grid.Canvas.Handle,TA_CENTER); 
  if (ARow mod 2 = 0)
  then c:=clWhite
  else c:=$00E8E8E8;
  // Fill rectangle with colour
  Grid.Canvas.Brush.Color := c;
  Grid.Canvas.FillRect(Rect);
  // Next, draw the text in the rectangle
  if (ACol=1) or (ACol=3) or (ACol=5) then
  begin
    Grid.Canvas.Font.Color := $005F5F5F; 
  end
  else
  begin
    Grid.Canvas.Font.Color := $005F5F5F;
  end;
    Grid.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
    SetTextAlign(Grid.Canvas.Handle, SavedAlign);
end;
Xalo
  • 164
  • 1
  • 2
  • 13