Recent

Author Topic: Call OnDrawCell Directly?  (Read 4116 times)

Madmensking

  • Newbie
  • Posts: 4
Call OnDrawCell Directly?
« on: August 26, 2014, 02:25:35 pm »
Afternoon all,

I'm trying to work out if it is possible to directly call the onDrawCell procedure to force a String Grid to redraw itself so that cells in a column can be coloured according to if they match values in a string list, the code is below and hopefully will make more sense!

Code: [Select]
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState);
var
  StringComp: LongInt;
  rc: Integer;
begin
  rc:= ReturnList.Count;
  I:= 1;

     if rc < 1 then
     While i < StringGrid1.RowCount do
     begin
          if i > rc then exit;
          StringComp:=AnsiCompareStr(ReturnList.Strings[i],StringGrid1.Cells[3, i]);
          if StringComp = 0 then
             begin
               StringGrid1.Canvas.Brush.Color:=clGreen;
               StringGrid1.Canvas.FillRect(aRect);

               StringGrid1.Canvas.TextRect(aRect, aRect.Left, aRect.Top, StringGrid1.Cells[3, i]);
               i:= i + 1;

               if gdFocused in aState then
                  StringGrid1.Canvas.DrawFocusRect(aRect);
             end
             else
             begin
               StringGrid1.Canvas.Brush.Color:=clRed;
               StringGrid1.Canvas.FillRect(aRect);

               StringGrid1.Canvas.TextRect(aRect, aRect.Left, aRect.Top, StringGrid1.Cells[3, i]);
               i:= i + 1;

               if gdFocused in aState then
                  StringGrid1.Canvas.DrawFocusRect(aRect);
             end;
     end;
end;                                       

NOTE
The variable i is declared as a public variable and is used a generic counter for loops. 

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Call OnDrawCell Directly?
« Reply #1 on: August 26, 2014, 02:36:43 pm »
Don't know about OnDrawCell, but with TDBGrid, I use OnPrepareCanvas for similar...
http://lazarus-ccr.sourceforge.net/docs/lcl/grids/tcustomgrid.onpreparecanvas.html

Here's my code for TDBGrid.  I note the parameters for PrepareCanvas are different for TStringGrid (for TStringGrid you get Row and Col passed as parameters), but you should get the idea...  Basically, I'm only setting Canvas.Brush.Colour and Canvas.Font.Colour, the grid does the actual drawing itself.

Code: [Select]
Procedure TFrameGrid.grdSQLPrepareCanvas(Sender: TObject; DataCol: Integer;
  Column: TColumn; AState: TGridDrawState);
Var
  oField: TField;
  oColor: TColor;
  sColor: String;
Begin
  If Not (gdSelected In AState) Then
  Begin
    oField := FDataset.FindField('Colour_ID');
    If Not Assigned(oField) Then
      oField := FDataset.FindField('Color_ID');

    If Assigned(oField) Then
    Begin
      sColor := Lowercase(oField.Text);

      Case sColor Of
        'red': oColor := TColor($008080FF);
        'green': oColor := TColor($0080FF80);
        'blue': oColor := TColor($00FF9F40);
        'orange': oColor := TColor($0084ACFF);
        'yellow': oColor := TColor($0080FFFF);
        Else
          sColor := 'ignore';
      End;

      If sColor <> 'ignore' Then
      Begin
        If gdRowHighlight In AState Then
          oColor := GetHighLightColor(oColor, 32);

        grdSQL.Canvas.Brush.Color := oColor;
      End;

      grdSQL.Canvas.Font.Color := clBlack;
    End;
  End;
End;
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Madmensking

  • Newbie
  • Posts: 4
Re: Call OnDrawCell Directly?
« Reply #2 on: August 26, 2014, 02:48:55 pm »
Cheers Mike,

I will give that a go, I notice it doesn't use any TRect though so does the code you've given colour the entire row rather than an individual cell?

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Call OnDrawCell Directly?
« Reply #3 on: August 26, 2014, 02:51:58 pm »
I guess you cannot call StringGrid1DrawCell directly, or, better said, you can but is no guarantee that anything will be painted. Some wigdetsets, like Qt and Carbon, does not allow to paint out of Paint method. Maybe it will work on Windows and GTK2.

A more straightforward method is to assign the OnDrawCell event to the grid and call StringGrid1.Invalidate. Job will be done in a natural way.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

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

Madmensking

  • Newbie
  • Posts: 4
Re: Call OnDrawCell Directly?
« Reply #4 on: August 26, 2014, 02:54:39 pm »
Blaazen,

I tried using .invalidate but nothing happened?

Even when I put a break in at the beginning on the drawcell after the invalidate had been run, ondrawcell was not called..

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Call OnDrawCell Directly?
« Reply #5 on: August 26, 2014, 02:58:18 pm »
Cheers Mike,

I will give that a go, I notice it doesn't use any TRect though so does the code you've given colour the entire row rather than an individual cell?
It's the different parameters thing I was talking about.  For TStringGrid, your parameters will include Row and Column, so you'll be doing for an individual cell.
My code is actually suboptimal as I'm doing the same Field Lookup once for every column per record in the dataset.  And I've only just noticed, so thanks for your question  :)

As for the lack of rect, there's no drawing going on inside PrepareCanvas.  The grid does the drawing.  It just uses the brush, pen, font etc that you set up for it in OnPrepareCanvas.
« Last Edit: August 26, 2014, 03:01:16 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

 

TinyPortal © 2005-2018