gpt4 book ai didi

delphi - 如何右键弹出窗体?

转载 作者:行者123 更新时间:2023-12-02 06:10:13 26 4
gpt4 key购买 nike

当我右键单击 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com