Recent

Author Topic: [SOLVED]How to avoid drawing the focused rect on a stringgrid cell?  (Read 1505 times)

kinlion

  • New Member
  • *
  • Posts: 45
  • I Love Lazarus
In TStringGrid's OnDrawCell event, I can draw anything except erase the focused rect, because it is drawn after that event.
I tried to set Canvas.Pen.Style to psClear in OnPrepareCanvas event, but no use.
My StringGrid Options includes goRowSelect.
 :(
« Last Edit: December 08, 2018, 07:51:21 am by kinlion »
Lazarus 1.8.4 / FPC 3.0.4 / SVN 57972
On Win10 X64, have to compile for Win32

wp

  • Hero Member
  • *****
  • Posts: 6233
Re: How to avoid drawing the focused rect on a stringgrid cell?
« Reply #1 on: November 27, 2018, 10:28:38 am »
You cannot do it with the standard events. But you can derive a new TStringGrid from the current class and override the DrawFocusRect method to paint nothing. Since this does not affect any properties you can even keep the class name:

Code: Pascal  [Select]
  1. type
  2.   TStringGrid = type(Grids.TStringGrid)  
  3.   protected
  4.     procedure DrawFocusRect(aCol,aRow: Integer; ARect: TRect); override;
  5.   end;
  6. ...
  7. procedure TStringGrid.DrawFocusRect(aCol, aRow: Integer; ARect: TRect);
  8. begin
  9.   // Do nothing
  10. end;

If you only have one string grid in your project you can add this code to the unit in which it is used. Or, if you have several grids, put it into its own unit and add this unit to the *end* of the uses clauses of all units with strings grids. Having the new unit at the end of the uses clause (or to be more precise: after the grids unit) makes it replace the standard TStringGrid.

[EDIT]
howardpc in the following post is right: it should be "class(Grids.TStringGrid)" instead of "type(Grids.TStringGrid)".
« Last Edit: November 27, 2018, 10:47:23 am by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

howardpc

  • Hero Member
  • *****
  • Posts: 3150
Re: How to avoid drawing the focused rect on a stringgrid cell?
« Reply #2 on: November 27, 2018, 10:38:18 am »
Slip of the pen.
I'm sure wp meant
Code: Pascal  [Select]
  1. type   TStringGrid = class(Grids.TStringGrid)
  2.   ...

kinlion

  • New Member
  • *
  • Posts: 45
  • I Love Lazarus
Re: How to avoid drawing the focused rect on a stringgrid cell?
« Reply #3 on: November 27, 2018, 10:43:38 am »
Thanks to all.

I haved derived a new class as wp said :)
Lazarus 1.8.4 / FPC 3.0.4 / SVN 57972
On Win10 X64, have to compile for Win32