Recent

Author Topic: [solved] Extend TStringgrid  (Read 1526 times)

Danccce

  • New member
  • *
  • Posts: 7
[solved] Extend TStringgrid
« on: May 25, 2018, 12:23:23 pm »
Hi,

I'm trying to extend the TStringGrid class.
So I've made a subclass, but the DrawCell Methode does not work at all.

Code: Pascal  [Select][+][-]
  1. type
  2.   MExtendedStringGrid = class(TStringGrid)
  3.   private
  4.  
  5.   protected
  6.     procedure DrawCell(aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState); override;
  7.   public
  8.  
  9.   end;
  10.  
  11. implementation
  12.  
  13. procedure MExtendedStringGrid.DrawCell(aCol, aRow: Integer; aRect: TRect;
  14.   aState: TGridDrawState);
  15. begin
  16.   Canvas.Font.Color := clRed;
  17.   Canvas.TextOut(aRect.Left+2, aRect.Top+2, Cells[ACol, ARow]);
  18. end;
  19.  

 Do you have a suggestion for me what I've done wrong?
« Last Edit: May 25, 2018, 12:47:13 pm by Danccce »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Extend TStringgrid
« Reply #1 on: May 25, 2018, 12:24:54 pm »
Code: Pascal  [Select][+][-]
  1. procedure MExtendedStringGrid.DrawCell(aCol, aRow: Integer; aRect: TRect;
  2.   aState: TGridDrawState);
  3. begin
  4.   Canvas.Font.Color := clRed;
  5.   Canvas.TextOut(aRect.Left+2, aRect.Top+2, Cells[ACol, ARow]);
  6.   inherited;
  7. end;
  8.  
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Extend TStringgrid
« Reply #2 on: May 25, 2018, 12:25:08 pm »
Use OnPrepareCanvas (of a regular TStringGrid) to set color/font etc.

Bart

Danccce

  • New member
  • *
  • Posts: 7
Re: Extend TStringgrid
« Reply #3 on: May 25, 2018, 12:29:58 pm »
Thank you for the quick responce.

@Bart: I will exend the StringGrid class for unsing it in different ways, so using the regular TStringGrid is not an option

@mangakissa:
Code: Pascal  [Select][+][-]
  1. procedure MExtendedStringGrid.DrawCell(aCol, aRow: Integer; aRect: TRect;
  2.   aState: TGridDrawState);
  3. begin
  4.   Canvas.Font.Color := clRed;
  5.   Canvas.TextOut(aRect.Left+2, aRect.Top+2, Cells[ACol, ARow]);
  6.   inherited;
  7. end;
doesn't work as well. I have the suggestion the methode is never called...??? 

Thank you in advanved

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Extend TStringgrid
« Reply #4 on: May 25, 2018, 01:09:38 pm »
I will exend the StringGrid class for unsing it in different ways, so using the regular TStringGrid is not an option
Your ExtendedStringGrid inherits from TStringGrid, therefore you will be "using" the properties and methods of the regular TStringGrid, if you want or not.

BTW: Why don't you follow the standard naming convention to begin the classnames with a leading T? Beginning with an M will make your code very hard to read. And the name "ExtendedStringGrid" has a high probability that it exists already somewhere in the internet, and you will run into problems if you will install a package containing another ExtendedStringGrid. A usual workaround is to add your initials somewhere in the component name. (In my case, a name with less coincidence chance would be "TWPExtendedStringGrid").

Code: Pascal  [Select][+][-]
  1. procedure MExtendedStringGrid.DrawCell(aCol, aRow: Integer; aRect: TRect;
  2.   aState: TGridDrawState);
  3. [...]
  4.  

All grids inherit from TCustomGrid and this introduces a variety of painting methods. Depending on what you want to change in the painting behavior, your job will be very easy or become a nightmare - if you override a method called too early.

If you only want to change a font the easiest way is to follow Bart's suggestion and override the PrepareCanvas method. It is called immediately before the cell content is painted and gives you a last opportunity to change the canvas used for painting. Whatever you do with the canvas will be used for painting. Dont't forget to mark the declaration of the method as "override".

Calling "DrawCell" on the other hand is the outermost cell drawing method called by DrawRow. This means you must either call inherited or draw everything by yourself. Without knowing what you want to achieve I cannot give a specific recommendation.

If your DrawCell is not executed you did not declare it as "override".

 

TinyPortal © 2005-2018