Recent

Author Topic: How paint a row in a stringGrid ?  (Read 6372 times)

crack81

  • New Member
  • *
  • Posts: 12
How paint a row in a stringGrid ?
« on: July 27, 2016, 02:18:12 am »
How paint a row  when I selected it ?

like it  https://www.youtube.com/watch?v=x5kbU-Vz8lk&feature=youtu.be

I try this

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. var
  4.   i,j:Integer;
  5. begin
  6.  
  7.   if (ACol >=1) and (ARow =StringGrid1.Row) then
  8.     with TStringGrid(Sender) do
  9.     begin
  10.       //paint the background Green
  11.       Canvas.Brush.Color := clBlue;
  12.       Canvas.FillRect(aRect);
  13.       Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
  14.     end;
  15.  
  16.   if (ACol = StringGrid1.Col) and (ARow = StringGrid1.Row) then
  17.     with TStringGrid(Sender) do
  18.     begin
  19.       //paint the background Green
  20.       Canvas.Brush.Color := clYellow;
  21.       Canvas.FillRect(aRect);
  22.       Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[ACol, ARow]);
  23.     end;
  24. end;


But I think that it have a bug  because when I selected a static cell not work correctly

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How paint a row in a stringGrid ?
« Reply #1 on: July 27, 2016, 09:39:12 am »
Use the OnPrepareCanvas event (not OnDrawCell) like this - the TStringGrid here is named "grid":

Code: Pascal  [Select][+][-]
  1. procedure TForm1.gridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. begin
  4.   if (gdFixed in aState) then
  5.     Exit;
  6.   if (aRow=grid.Row) then
  7.     if (aCol=grid.Col) then
  8.       grid.Canvas.Brush.Color:=clYellow
  9.     else grid.Canvas.Brush.Color:=clBlue;
  10. end;    

crack81

  • New Member
  • *
  • Posts: 12
Re: How paint a row in a stringGrid ?
« Reply #2 on: July 27, 2016, 09:55:34 pm »
Thanks for you answer but I have the same problem  watch this https://www.youtube.com/watch?v=xu3tsq3OnwQ&feature=youtu.be

I am using Lazarus 1.6
Regards....

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How paint a row in a stringGrid ?
« Reply #3 on: July 27, 2016, 11:06:43 pm »
I did not investigate what happens at the title click. But as a workaround, it helps to repaint the entire grid in OnSelectCell, or OnSelection:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  2.   var CanSelect: Boolean);
  3. begin
  4.   StringGrid1.Invalidate;
  5. end;

crack81

  • New Member
  • *
  • Posts: 12
Re: How paint a row in a stringGrid ?
« Reply #4 on: July 28, 2016, 12:06:38 am »
Thanks for your help already works
//Regards....

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: How paint a row in a stringGrid ?
« Reply #5 on: July 28, 2016, 06:56:19 am »
The problem is that previous cells are not being invalidated. This case is practically the same as the one described here: http://wiki.lazarus.freepascal.org/Grids_Reference_Page#Highlighting_the_selected_cell_column_and_row
In fact if the BeforeSelection handler mentioned in that article is added to the code by Posted by howardpc it should work. The solution given by wp also works but it invalidates too much, in my opinion.

 

TinyPortal © 2005-2018