2010-07-19 88 views
1

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

这似乎比我想象的更困难。我首先试图在使用OnDeactivate事件禁用弹出窗口时销毁弹出窗体。这导致弹出窗口不显示。

回答

4

这里是我的解决方案(测试工作)...

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的)并设置其位置,您的“上下文表单”的左上角将完全位于鼠标光标的位置。

单击窗体上的任何其他位置将会破坏上下文窗体。

享受!

+0

这是一个耻辱史蒂夫“问和跑”(当然他的统计数据会显示这一点)这意味着这个问题可能永远不会被标记为回答(即使它是):( – LaKraven 2011-04-12 20:28:52

+2

如果你需要业力我可以投票给你up :-) – Johan 2011-04-15 16:15:41

相关问题