Obter a célula de um StringGrid que está sob o cursor do mouse

Veja nesta dica como obter a célula de um StringGrid apenas marcando com o mouse.

 

uses Windows;

 

procedure MouseCell(Grid: TStringGrid;

  var Coluna, Linha: integer);

var

  Pt: TPoint;

begin

  GetCursorPos(Pt);

  Pt := Grid.ScreenToClient(Pt);

  if PtInRect(Grid.ClientRect, Pt) then

    Grid.MouseToCell(Pt.X, Pt.Y, Coluna, Linha)

  else

  begin

    Coluna := -1;

    Linha := -1;

  end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

  Coluna, Linha: integer;

begin

  MouseCell(StringGrid1, Coluna, Linha);

  if (Coluna >= 0) and (Linha >= 0) then

    Caption := 'Coluna: ' + IntToStr(Coluna) + ' - ' +

      'Linha: ' + IntToStr(Linha);

  else

  Caption := 'O mouse não está no StringGrid';

end;