【问题标题】:delphi TStringGrid and right mouse buttondelphi TStringGrid 和鼠标右键
【发布时间】:2017-09-27 09:04:45
【问题描述】:

我正在使用 Delphi 10.1 Berlin 制作多设备应用程序。我有一个TStringGrid 来列出查询中的一些数据。

我还有一个弹出菜单(编辑、删除、...),但为了编辑/删除一个项目,我必须使用鼠标左键单击一个单元格。

是否可以在显示弹出菜单之前仅使用右键“选择一行”?

我试过了:

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;

但它在mbRightPerform() 上显示错误。

【问题讨论】:

  • 我看到您还添加了“多设备应用程序”。请始终为 FMX 应用程序添加 firemonkey 标签。
  • 您是如何添加OnMouseDown 事件的? FMX TStringGrid 不会公开此类事件。
  • 是的@Victoria 仍然不是OnMouseDown:) 我在他们的代码上取笑 OP,这显然是从其他 Q/A 中复制的。但请根据pastebin输入答案
  • 在 formCreate 事件上我写了 StringGrid1.OnMouseDown:=StringGrid1MouseDown;

标签: delphi mouseevent firemonkey right-click tstringgrid


【解决方案1】:

您可以使用以下代码:

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 是在启用Scoped Enumerations 的情况下编译的,因此mbRight 按钮的问题可以通过在值前面加上其枚举类型 (TMouseButton.mbRight) 来解决。

【讨论】:

  • 哇!有用!我修改 PopupMenu1.Popup(pf.X, pf.Y);到 PopupMenu1.Popup(pf.X, pf.Y+70);为了在鼠标光标附近有弹出菜单!非常感谢@tom
  • 谢谢@Rube,很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 2014-05-06
相关资源
最近更新 更多