Recent

Author Topic: [SOLVED]Stringgrid and mouse pointer  (Read 2746 times)

Fai

  • New Member
  • *
  • Posts: 24
[SOLVED]Stringgrid and mouse pointer
« on: August 08, 2017, 05:34:48 am »
Hi,
This is my code on StringGrid OnMouseUp event:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   ACol, ARow: integer;
  5. begin
  6.   with (Sender as TStringGrid) do begin
  7.     MouseToCell(X, Y, ACol, ARow);
  8.     if (ACol = 1) and (ARow>=FixedRows) then begin
  9.       ShowMessage('Click');
  10.     end;
  11.   end;
  12. end;

If I click on the non grid area the event still fired as long as the mouse pointer inline with column 1 (see picture).
How can I prevent this? so the event fired only when the mouse pointer clicked on top of the grid cell area.
« Last Edit: August 08, 2017, 06:31:49 am by Fai »

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Stringgrid and mouse pointer
« Reply #1 on: August 08, 2017, 06:03:13 am »
Add CellRect to make sure the mouse pointer is inside the cell:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. var
  4.   ACol, ARow: Integer;
  5.   ARect: TRect;
  6. begin
  7.   with (Sender as TStringGrid) do begin
  8.     MouseToCell(X, Y, ACol, ARow);
  9.     ARect := CellRect(ACol, ARow);
  10.     if (Y > ARect.Bottom) then Exit;
  11.     if (ACol = 1) and (ARow>=FixedRows) then begin
  12.       ShowMessage('Click');
  13.     end;
  14.   end;
  15. end;

Fai

  • New Member
  • *
  • Posts: 24
Re: Stringgrid and mouse pointer
« Reply #2 on: August 08, 2017, 06:30:38 am »
Thanks Handoko, it works.

 

TinyPortal © 2005-2018