Recent

Author Topic: Thin line at left of cells in selected row in tstringgrid which is different clr  (Read 1424 times)

acpascquestions

  • Newbie
  • Posts: 2
Hi, I have a tstringgrid on a form which uses this code to highlight a selected row:

procedure TTestForm.testGridDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin

  testGrid.Canvas.Pen.Color := clNone;


  if ARow <> SelectedRow then
    testGrid.Canvas.Brush.Color := clWindow
  else testGrid.Canvas.Brush.Color := clActiveCaption;


  if ColorTSTringList[ARow] = 'Red' then
    testGrid.Canvas.Font.Color := clRed;

  testGrid.Canvas.Rectangle(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom);

  testGrid.Canvas.TextRect(Rect, Rect.Left,Rect.Top,testGrid.Cells[ACol,ARow]);


end;

For some reason, there is a slightly darker blue rectangle, perhaps 3 pixels wide and the height of the cell in each row which is highlighted (see attached image). Any ideas on this one?

winni

  • Hero Member
  • *****
  • Posts: 3197
Hi!

The background color of a textrect was discussed here:

https://forum.lazarus.freepascal.org/index.php?topic=44978.0

Winni

howardpc

  • Hero Member
  • *****
  • Posts: 4144
You don't say where your SelectedRow variable comes from, but it is better to use the parameters provided by the event.Something like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.testGridDrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. var
  4.   grd: TStringGrid absolute Sender;
  5. begin
  6.   if not (Sender is TStringGrid) then
  7.     Exit;
  8.   case (gdSelected in aState) of
  9.     True:  grd.Canvas.Brush.Color := clActiveCaption;
  10.     False: grd.Canvas.Brush.Color := clWindow;
  11.   end;
  12.   grd.Canvas.FillRect(aRect);
  13.  
  14.   grd.Canvas.Pen.Color := clNone;
  15.   if ColorTSTringList[ARow] = 'Red' then
  16.     grd.Canvas.Font.Color := clRed;
  17.   grd.Canvas.TextRect(aRect, aRect.Left, aRect.Top, grd.Cells[aCol, aRow]);
  18. end;

jamie

  • Hero Member
  • *****
  • Posts: 7421
There must be something wrong because RECTANGLE is suppose to fill the interior completely using the current brush style and color.

 You shouldn't need to call a separate function to do that..

 I didn't notice the widget set that is being used here ? Looks to me like a widget error using rectangle.

 However, I also notice no previsions are being made to shrink or offset the RECT for the text so that means it's possible the TextOut could be doing it for what ever reason..
The only true wisdom is knowing you know nothing

acpascquestions

  • Newbie
  • Posts: 2
So fiddly, but I got there. I found a post on Stackoverflow which suggested:

Rect.Left := Rect.Left-4;

and putting this at the start of the ondraw event gets rid of the random line at the left of each cell in the selected row:

https://stackoverflow.com/questions/33120729/how-to-fill-cell-of-a-string-grid-using-custom-color

Thanks for your prompt replies.

jamie

  • Hero Member
  • *****
  • Posts: 7421
Would you mine telling us the Widget set you are using? I suspect there is a problem...

The only offsetting you should be doing is that of the Text Rect, that should be enclose a little so not to over write the borders.

 For example

  for the text operation..

  Inc(Rect.Left, 4);

  But if even if that worked it shouldn't be drawing the line like that.

 so what is your widget set ? Win32, GTK et c?
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018