2010-07-19 92 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