delphi TStringGrid and right mouse button

南楼画角 提交于 2021-02-05 11:12:31

问题


I am using Delphi 10.1 Berlin to make a Multi-Device application. I have a TStringGrid in order to list some data from a query.

I also have a popup menu (edit, delete, ...), but in order to edit/delete an item I have to click on a cell using the left mouse button.

Is it possible to "select a row" using only the right button before showing the popup menu?

I tried:

procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    StringGrid1.Perform(WM_LBUTTONDOWN, 0, MakeLParam(Word(X), Word(Y)));
end;

But it displays an error on mbRight and on Perform().


回答1:


You can use the following code:

procedure TForm39.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: single);
var
  pf: TPointF;
begin
  if Button = TMouseButton.mbRight then
  begin
    with Sender as TStringGrid do
      SelectRow(RowByPoint(X, Y));
  // Do not use the grids PopupMenu property, it seems it
  // prevents this event handler comletely.
  // Instead, activate the menu manually here.
    pf := ClientToScreen(TPointF.Create(X, Y));
    PopupMenu1.Popup(pf.X, pf.Y);
  end;
end;

FireMonkey is compiled with Scoped Enumerations enabled, so the problem with the mbRight button is solved by prefixing the value with its enum type (TMouseButton.mbRight).



来源:https://stackoverflow.com/questions/46443746/delphi-tstringgrid-and-right-mouse-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!