* * *

Author Topic: Turn off String Grid red border of selected cell?  (Read 518 times)

josh

  • Sr. Member
  • ****
  • Posts: 494
Turn off String Grid red border of selected cell?
« on: February 14, 2018, 11:42:07 pm »
I have not used stringgrid in a while, and am trying to remove the red line around a selected cell.

I have placed the following in ondraw event

I suspect I have forgot to turn off an option somewhere, as last time I used stringgrid ( 3+ years ago) i am sure I did not have this behaviour, and I no longer have the code.

Code: Pascal  [Select]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. begin
  4.   With (sender as tstringgrid) Do
  5.   Begin
  6.     if not (grids.gdFixed in aState) then
  7.     begin
  8.       if not (grids.gdSelected in aState) then
  9.       begin
  10.         Canvas.Brush.Color :=$00710000;
  11.       end
  12.       else
  13.       begin
  14.         Canvas.Brush.Color := $002c8d31;
  15.       end;
  16.     end;
  17.     font.color:=ClWhite;
  18.     defaultdrawcell(Acol, Arow, arect, astate);
  19.   end;
  20. end;      
  21.  
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

jamie

  • Sr. Member
  • ****
  • Posts: 430
Re: Turn off String Grid red border of selected cell?
« Reply #1 on: February 14, 2018, 11:59:17 pm »
maybe the Pen.Color is what you need?

canvas.Pen.Color := ????

Just a shot ..

josh

  • Sr. Member
  • ****
  • Posts: 494
Re: Turn off String Grid red border of selected cell?
« Reply #2 on: February 15, 2018, 12:32:57 am »
Hi

I have just tried pen.color and pen.width:=0 and still get the red border.

Code: [Select]
canvas.Pen.Width:=0;
canvas.pen.color:=clwhite;
Canvas.Rectangle(aRect.Left - 1,aRect.Top - 1,aRect.Right +1,aRect.Bottom + 1); 
« Last Edit: February 15, 2018, 12:34:53 am by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

molly

  • Hero Member
  • *****
  • Posts: 2288
Re: Turn off String Grid red border of selected cell?
« Reply #3 on: February 15, 2018, 12:44:07 am »
did you meant the focus rectangle josh ?

You should be able to check for that with gridstate gdFocused and draw according to your wishes (e.g. not draw the focus rectangle).

jamie

  • Sr. Member
  • ****
  • Posts: 430
Re: Turn off String Grid red border of selected cell?
« Reply #4 on: February 15, 2018, 12:53:27 am »
the fact that u r calling the default drawing pretty much negates what you are doing.

How about not calling the default drawing if it was a focus rectangle drawing cycle ?
« Last Edit: February 15, 2018, 12:55:24 am by jamie »

molly

  • Hero Member
  • *****
  • Posts: 2288
Re: Turn off String Grid red border of selected cell?
« Reply #5 on: February 15, 2018, 12:56:12 am »
In addition ... what jamie said  :)

josh

  • Sr. Member
  • ****
  • Posts: 494
Re: Turn off String Grid red border of selected cell?
« Reply #6 on: February 15, 2018, 12:58:22 am »
Hi Molly & Jamie,

My moded code is
Code: Pascal  [Select]
  1. With (sender as tstringgrid) Do
  2.   Begin
  3.     canvas.Pen.Width:=1;
  4.     canvas.pen.color:=color;
  5.     if not (grids.gdFixed in aState) then
  6.     begin
  7.       if not (grids.gdSelected in aState) then
  8.       begin
  9.         Canvas.Brush.Color :=$00710000;
  10.       end
  11.       else
  12.       begin
  13.         Canvas.Brush.Color := $002c8d31;
  14.       end;
  15.     end;
  16.     font.color:=ClWhite;
  17.     Canvas.Rectangle(aRect.Left - 1,aRect.Top - 1,aRect.Right +1,aRect.Bottom + 1);
  18.     defaultdrawcell(Acol, Arow, arect, astate);
  19.   end;
  20.  

I would have thought the code would draw a rectangle of pencolor=color of width one, but it does not change anything.
If I change the pen width to 6 then still nothing changes, if I comment out the defaultdraw routine then I get a wide border, but still I have a red line around the selected cell.


« Last Edit: February 15, 2018, 01:01:19 am by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

molly

  • Hero Member
  • *****
  • Posts: 2288
Re: Turn off String Grid red border of selected cell?
« Reply #7 on: February 15, 2018, 01:02:14 am »
if your aim is only to remove the focused rectangle then remove the flag form the drawstate and call defaultdrawcell. That should be able to do the trick without too much additional code.

In case you wish to alter more then you would need to add additional checks and draw the cell yourself (at least for those states for which you wish to change drawing).

Blaazen

  • Hero Member
  • *****
  • Posts: 2463
  • POKE 54296,15
    • Eye-Candy Controls
Re: Turn off String Grid red border of selected cell?
« Reply #8 on: February 15, 2018, 01:06:22 am »
Hi,
there is public (not published) property FocusColor so you can change the red dotted line to any color.
There's also options DrawFocusSelected which fills the cell with color clActiveCaption instead of the line around.
Also, there is property UseXORFeatures, which affects focus, see http://wiki.freepascal.org/Grids_Reference_Page#property_UseXORFeatures.3B
Lazarus 1.9.0 r56952 FPC 3.0.4 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.11.4
Lazarus 1.8.0 r56594 FPC 3.0.4 i386-win32-win32/win64 Wine 2.22

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Sr. Member
  • ****
  • Posts: 430
Re: Turn off String Grid red border of selected cell?
« Reply #9 on: February 15, 2018, 01:34:33 am »
Set the "FocusColor" property to clNone;
it seems to remove it

josh

  • Sr. Member
  • ****
  • Posts: 494
Re: Turn off String Grid red border of selected cell?
« Reply #10 on: February 15, 2018, 01:36:16 am »
Hi Molly

I added in
if (grids.gdFocused in aState) then astate:=astate-[gdFocused];
but this had no effect, debugged the value of astate when it was defaultdrawing and astate had changed, but did not effect result.

Hi Blaazen
Ading in FocusColor Solved it :) I will have to look at what other non published properies are available.

Code: Pascal  [Select]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. begin
  4.   With (sender as tstringgrid) Do
  5.   Begin
  6.     canvas.Pen.Width:=0; // was set to 2 when testing astate
  7.     canvas.pen.color:=color;
  8. //    if (grids.gdFocused in aState) then astate:=astate-[gdFocused];  No Effect
  9.     if not (grids.gdFixed in aState) then
  10.     begin
  11.       if not (grids.gdSelected in aState) then
  12.       begin
  13.         Canvas.Brush.Color :=$00710000;
  14.       end
  15.       else
  16.       begin
  17.         Canvas.Brush.Color := $002c8d31;
  18.       end;
  19.     end;
  20.  
  21.     focuscolor:=Canvas.Brush.Color;
  22.  
  23.     font.color:=ClWhite;
  24.     Canvas.Rectangle(aRect.Left - 1,aRect.Top - 1,aRect.Right +1,aRect.Bottom + 1);
  25.     defaultdrawcell(Acol, Arow, arect, astate);
  26.   end;
  27. end;        
  28.  

Thanks all for your help.. :) :)
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

molly

  • Hero Member
  • *****
  • Posts: 2288
Re: Turn off String Grid red border of selected cell?
« Reply #11 on: February 15, 2018, 01:50:06 am »
but this had no effect, debugged the value of astate when it was defaultdrawing and astate had changed, but did not effect result.
thanks for the feedback. Hmz... strange i vividly remember that should be able to do the trick. Perhaps i'm remembering wrongly, in which case i'm sorry about guiding your wrongly.

josh

  • Sr. Member
  • ****
  • Posts: 494
Re: Turn off String Grid red border of selected cell?
« Reply #12 on: February 15, 2018, 01:54:05 am »
Hi Molly,

No worries, I agree it should solve it, I have been trying to work out why not, as astate is being changed before the call to defaultdraw.

I am using latest svn, not sure if that is having an impact, hopfully not.
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

molly

  • Hero Member
  • *****
  • Posts: 2288
Re: Turn off String Grid red border of selected cell?
« Reply #13 on: February 15, 2018, 05:59:09 am »
it is my bad Josh.

See also wiki. The focus rectangle is drawn after OnDrawCell.

And seeing that order of things made my recollection a little better. At least in Delphi it was possible to call DrawFocusRectangle yourself when the state was set. Because of the 'or'-drawing of that rectangle, that rectangle looks to have disappeared when the VCL made the second call to that function. No idea if that also works for LCL (and for every widgetset).

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus