我想知道何时在 TDBGrid 中双击记录,但无论单击网格中的哪个位置都会触发 OnDblClick 事件。
在 Delphi 中是否有一种很好的干净方法来确定 TDBGrid 双击是否在标题上?
我想知道何时在 TDBGrid 中双击记录,但无论单击网格中的哪个位置都会触发 OnDblClick 事件。
在 Delphi 中是否有一种很好的干净方法来确定 TDBGrid 双击是否在标题上?
我就是这样做的,它只是计算位置是否与标题重合:
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;
我从OnDblClick
处理程序中调用它。
// 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;