Recent

Author Topic: [SOLVED] Fitting control to StringGrid cell  (Read 626 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Fitting control to StringGrid cell
« on: February 28, 2021, 05:55:59 pm »
Hi All,

why doesn't the control (TButton) fit the cell in a StringGrid when I do this

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect;
  2.   aState: TGridDrawState);
  3. begin
  4.   if (aCol = 1) and (aRow = 2) then
  5.     Button2.SetBounds(aRect.Left, aRect.Top, aRect.Width, aRect.Height);
  6.  
  7.   (Sender as TStringGrid).DefaultDrawCell(aCol, aRow, aRect, aState);
  8. end;
  9.  
Its close but about 10 pixels off
« Last Edit: February 28, 2021, 06:49:12 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Fitting control to StringGrid cell
« Reply #1 on: February 28, 2021, 06:26:09 pm »
It should be something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. var
  4.   TheGrid: TStringGrid;
  5. begin
  6.   if not(Sender is TStringGrid) then Exit;
  7.   TheGrid := (Sender as TStringGrid);
  8.   if (aCol = 1) and (aRow = 2) then
  9.     Button1.SetBounds(aRect.Left + TheGrid.Left, aRect.Top + TheGrid.Top,
  10.       aRect.Width, aRect.Height);
  11. end;

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Fitting control to StringGrid cell
« Reply #2 on: February 28, 2021, 06:26:34 pm »
hard to say...

could be borderspacing values set in the control?

the control may have the wrong parent..?

The control could be scrolled alittle ?
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Fitting control to StringGrid cell
« Reply #3 on: February 28, 2021, 06:30:57 pm »
Not hard.  :D

It's about the coordinate origin. The button's coordinate origin is the form's top left corner but the aRect in the event is using the grid's top left corner as its origin.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Fitting control to StringGrid cell
« Reply #4 on: February 28, 2021, 06:43:28 pm »
Thanks
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018