Recent

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

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
[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: 4144
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

  • Sr. Member
  • ****
  • Posts: 272
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: 4144
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

  • Sr. Member
  • ****
  • Posts: 272
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: 11830
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.

apeoperaio

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

 

TinyPortal © 2005-2018