简体   繁体   中英

Determine if a double click was in the title of a TDBGrid

I want to know when a record was double clicked on in a TDBGrid, but the OnDblClick event is fired regardless of where in the grid has been clicked.

In Delphi is there a nice clean way of determining if a TDBGrid double click was on the title?

This is how I do it, it just calculates if the position coincides with the title:

function GridClickIsOnTitle(Grid: TDbGrid): Boolean;
var
  Pt: TPoint;
begin
  Pt := Grid.ScreenToClient(SmallPointToPoint(types.SmallPoint(GetMessagePos)));
  Result := (Grid.MouseCoord(Pt.X, Pt.Y).Y = 0) and (dgTitles in Grid.Options);
end;

I call it from the OnDblClick handler.

// in the class declaration

type
    THackDBGrid=Class(TDBGrid);   

// function to check if click is on the title

function isClickOnTitle(const dbGrid: TDbGrid; const rowTitleHeight : integer): Boolean;

var
  mousePoint  : TPoint;
  mouseInGrid : TPoint;

begin
  mousePoint  := Mouse.CursorPos;
  mouseInGrid := dbGrid.ScreenToClient(mousePoint);
  result      := mouseInGrid.Y <= rowTitleHeight;
end;

// grid double click event

procedure TForm.dbGridDblClick(Sender: TObject);

var
  rowTitleHeight : integer;

begin
  inherited;

  // trick to get the title row height
  rowTitleHeight := THackDBGrid(gdTestGrid).RowHeights[0];

  if not isClickOnTitle(gdTestGrid, rowTitleHeight) then begin
    bbOk.click;
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM