Recent

Author Topic: [SOLVED] TStringGrid image on fixed cols header  (Read 1145 times)

apeoperaio

  • Full Member
  • ***
  • Posts: 230
[SOLVED] TStringGrid image on fixed cols header
« on: October 05, 2021, 12:31:38 pm »
I would like to show an image on a the header of a fixed col, is it possible?

See attached image.
« Last Edit: October 06, 2021, 10:07:23 am by apeoperaio »

howardpc

  • Hero Member
  • *****
  • Posts: 3829
Re: TStringGrid image on fixed cols header
« Reply #1 on: October 05, 2021, 01:36:21 pm »
See the example provided in the sources in
.../lazarus/examples/gridexamples/title_images/

apeoperaio

  • Full Member
  • ***
  • Posts: 230
Re: TStringGrid image on fixed cols header
« Reply #2 on: October 05, 2021, 01:53:03 pm »
I know how to put images on row headers. But it does not work for fixed cols.

In the example included in .../lazarus/examples/gridexamples/title_images/ if I click on the cell 0,0 (fixed column) I got a List Index out of bounds error...

howardpc

  • Hero Member
  • *****
  • Posts: 3829
Re: TStringGrid image on fixed cols header
« Reply #3 on: October 05, 2021, 03:23:33 pm »
It seems that TStringGrid is designed so that you can add columns only after fixed columns.
The error you get in the example is a minor bug: the HeaderClick routine should guard against referencing a nonexistent column.

I think the only "simple" way to add an image to a "fixed" column header would be to set FixedCols to 0, and add the columns you want (which would now include the very first one of course) with appropriate images in an associated imagelist.
Then you would have to paint column 0 to simulate the fixed look, and set the ReadOnly property of the TGridColumn with index 0 to True.

apeoperaio

  • Full Member
  • ***
  • Posts: 230
Re: TStringGrid image on fixed cols header
« Reply #4 on: October 05, 2021, 03:29:06 pm »
I supposed it but thank you for the clarification.

wp

  • Hero Member
  • *****
  • Posts: 9012
Re: TStringGrid image on fixed cols header
« Reply #5 on: October 05, 2021, 04:33:35 pm »
Of course, even if you use columns, you can always custom-draw cells:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. const
  4.   IMAGE_INDEX = 5;
  5. begin
  6.   if (aCol = 0) and (aRow = 0) then
  7.     ImageList1.Draw(StringGrid1.Canvas, varCellPadding, varCellPadding, IMAGE_INDEX);
  8. end;
This code draws the image with index 5 to the top-left fixed cell.

See also attachment for the rest of this project.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

apeoperaio

  • Full Member
  • ***
  • Posts: 230
Re: TStringGrid image on fixed cols header
« Reply #6 on: October 06, 2021, 10:07:03 am »
Thank you!

 

TinyPortal © 2005-2018