Recent

Author Topic: Determine the right cell in TDrawgrid with mousecursor  (Read 5186 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Determine the right cell in TDrawgrid with mousecursor
« on: October 30, 2014, 12:07:52 pm »
In this topic I got showhint on every cell. But there's something strange. My TDrawgrid is indexed to 500 cels (20 cols * 25 rows). To determine the position of the mousecursor I had to use OnMouseMove and saved X and Y in a variabele. This peace of code will tel me on which cell my mousecursor is focused.
Code: [Select]
procedure TFrmPuzzelstukjes.PuzzelGridMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  fXas := X;
  fYas := Y;
end;

procedure TFrmPuzzelstukjes.DoShowHint(Sender: TObject; HintInfo: PHintInfo);
var ax,ay     : longint;
    col, row  : integer;
    celnumber : integer;
begin
  TDrawGrid(Sender).MouseToCell(HintInfo^.CursorPos.X,HintInfo^.CursorPos.Y,ax,ay);
  col := fXas div 20;
  row := fYas div 20;
  celnumber := (row * 20) + col + 1;
  HintInfo^.HintStr:= format('col (%d): %d, row (%d): %d, celnumber: %d',[fXas,col,fYas,row,celnumber]);
end;
But when my mousecursor is located on cell 20, it's telling me cell 25 is focused. A small calculation: 485 (X) div 20 (number of cols in a row) = 24.

OnMouseMove is giving te right X position, because TDrawgrid.width is 504.

How can I calculate the right index of the cell where the mousecursor is focused?
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #1 on: October 30, 2014, 12:21:56 pm »
IMO you are doing it wrong. You should calculate

CellColumn = MouseX / WidthOfColumn.

And it will work, of course, only if all columns has the same width and grid is not scrolled out.

And the same is for rows.

CellRow = MouseY / HeightOfRows.

EDIT: I realized now that there's property GridLineWidth (default 1) which you should take into account too.
« Last Edit: October 30, 2014, 12:29:52 pm by Blaazen »
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/

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #2 on: October 30, 2014, 12:45:49 pm »
@mangakissa: Did you look at the source in grids.pas? Always a good idea... You'll see that TCustomGrid has a method for determining the rectangle enclosing a cell:

Code: [Select]
    function CellRect(ACol, ARow: Integer): TRect;
« Last Edit: October 30, 2014, 03:03:07 pm by wp »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #3 on: October 30, 2014, 04:20:42 pm »
@wp

AFAIK CellRect is nice if you know the cell's col and row. You don't know it if you are using a mouse on it.

@blaazen
Thanks.  I knew I did something wrong.
Just found out it should be this
Code: [Select]
col := fXas div 25;
row := fYas div 25;
Because the cell is 25 * 25 pixels.
But your solution is much better and dynamic.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #4 on: October 30, 2014, 04:52:32 pm »
Quote
if you know the cell's col and row.
But you know that from MouseToCell which you are calling ?!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #5 on: October 30, 2014, 06:11:00 pm »
Col, Row is in those ax, ay you ignored?

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #6 on: October 31, 2014, 08:23:59 am »
I read the ax and yx and those gave me the range of the cell, not the col and row. But I solved it (thanks to Bart) with this:
Code: [Select]
TCustomGrid(Sender).MouseToCell(HintInfo^.CursorPos.X,HintInfo^.CursorPos.Y,Col, Row);
It seems property MouseToCell from TDrawgrid has a different approach than TCustomgrid.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #7 on: October 31, 2014, 09:28:06 am »
Quote
It seems property MouseToCell from TDrawgrid has a different approach than TCustomgrid.
Nonsense - look at the source code: TDrawGrid inherits from TCustomDrawGrid which in turn inherits from TCustomGrid. MouseToCell is implemented only in TCustomGrid, therefore, TDrawGrid uses the same MouseToCell code as TCustomGrid.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #8 on: October 31, 2014, 01:19:10 pm »
You're right. My apologies. I thought I saw different values but now I see the same (put a memo on screen to see what happens).
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Determine the right cell in TDrawgrid with mousecursor
« Reply #9 on: October 31, 2014, 01:43:28 pm »
No need to apologize - sometimes we all don't see the obvious.

 

TinyPortal © 2005-2018