delphi - 如何右键弹出窗体?

标签 delphi

当我右键单击 TPaintBox 时,我需要能够弹出一个 TForm(表单的内容将取决于我单击的位置)。如果用户单击其他任何地方,我希望原始表单被销毁(或至少消失)。如果新的单击恰好是在 TPaintBox 上再次右键单击,则必须出现一个新的 TForm。基本上,这是一个右键单击属性查询类型的操作,即右键单击以获取 TPaintBox 区域的属性。

这似乎比我想象的要困难。当使用 OnDeactivate 事件停用弹出窗口时,我首先尝试销毁弹出窗口。这导致弹出窗口未显示。

最佳答案

这是我的解决方案(经过测试且有效)...

type
  TForm1 = class(TForm)
    ...
  private
    ContextForm: TfrmContext;
  end;

...

implementation

procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ContextForm <> nil then
    FreeAndNil(ContextForm);
 if Button = mbRight then
  begin
    ContextForm := TfrmContext.Create(Application);
    ContextForm.SetParentComponent(Application);
    ContextForm.Left := Mouse.CursorPos.X;
    ContextForm.Top := Mouse.CursorPos.Y;
    ContextForm.Show;
  end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ContextForm <> nil then
    FreeAndNil(ContextForm);
end;

在此演示中,右键单击 Button1 将创建“上下文表单”(这是一个 TForm)并设置其位置,以便“上下文表单”的左上角将恰好位于鼠标光标所在的位置。

单击表单上的任何其他位置都会破坏上下文表单。

享受吧!

关于delphi - 如何右键弹出窗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280454/

相关文章:

windows - 检查特定过程在delphi中是否升高

delphi - 如何区分来自具有相同 IP 地址的多个客户端的连接?

Delphi TNetHTTPRequest/TNetHTTPClient 适用于 Win 10,但不适用于 Win 7

delphi - 是否可以在 Synchronize 内部使用简单的循环?

android - 在从不同计算机编译的旧 APK 上安装新 APK

delphi - TStringList - 奇怪的行为

android - Delphi XE6 如何使用样式更改 TMetropolisUIListBoxItem 的突出显示颜色

delphi - 修复Delphi的错误

delphi - 如何在 TListView 列上显示排序箭头?

javascript - 如何使用 Chromium 和 Delphi 6 在网页中将 "native functions"公开给 Javascript?