2013-09-23 17 views
2

我試圖用TCustomHint向我的用戶顯示一條消息,很好地淡入淡出,不要太分心。但是,當我用點對我的對象調用ShowHint時,提示框似乎圍繞着我給出的要點。我想讓我的盒子出現,以便它的左上角座標是給定的點。停止TCustomHint圍繞我自己的中心

下面是我使用這樣顯示提示代碼:

procedure ShowNotification(ATitle: UnicodeString; AMsg: UnicodeString); 
var 
    Box: TCustomHint; 
    P: TPoint; 
begin 
    Box := TCustomHint.Create(MyForm); 
    Box.Title := ATitle; 
    Box.Description := AMsg; 
    Box.Delay := 0; 
    Box.HideAfter := 5000; 
    Box.Style := bhsStandard; 

    P.X := 0; 
    P.Y := 0; 

    Box.ShowHint(P); 
end; 

我知道,我點的X/Y座標是不是相對於形式,這不是問題。

我已經通過,當我打電話ShowHint會發生什麼追查,看來,如果我能以某種方式控制TCustomHint.ShowHint(Rect: TRect)內底層TCustomHintWindow的最終寬度話,我可能會在企業。

所以我的問題是:有沒有一種明顯的方法來阻止TCustomHint從我的重點?或者我將不得不經歷繼承過程,覆蓋繪製方法等等?我希望我只是想念一件簡單的事情。

回答

2

有沒有特別簡單的方法來做你想做的。 TCustomHint類旨在用於非常特定的目的。它被設計爲由TControl.CustomHint屬性使用。您可以通過查看TCustomHint.ShowHint的代碼來了解它是如何調用的。相關的摘錄:

if Control.CustomHint = Self then 
begin 
    .... 
    GetCursorPos(Pos); 
end 
else 
    Pos := Control.ClientToScreen(Point(Control.Width div 2, Control.Height)); 
ShowHint(Pos); 

所以,無論是控制被示爲水平地圍繞當前光標位置爲中心,或周圍的相關聯的控制的中間水平居中。

我認爲這裏的底線是TCustomHint不是按照您使用它的方式設計的。

無論如何,有一種相當可怕的方式讓你的代碼做你想做的。你可以創建一個臨時的TCustomHintWindow,你永遠不會顯示,並用它來計算你想要顯示的提示窗口的寬度。然後用它來轉移你傳遞給真實提示窗口的點。爲了使它飛行,你需要破解TCustomHintWindow的私人成員。

type 
    TCustomHintWindowCracker = class helper for TCustomHintWindow 
    private 
    procedure SetTitleDescription(const Title, Description: string); 
    end; 

procedure TCustomHintWindowCracker.SetTitleDescription(const Title, Description: string); 
begin 
    Self.FTitle := Title; 
    Self.FDescription := Description; 
end; 

procedure ShowNotification(ATitle: UnicodeString; AMsg: UnicodeString); 
var 
    Box: TCustomHint; 
    SizingWindow: TCustomHintWindow; 
    P: TPoint; 
begin 
    Box := TCustomHint.Create(Form5); 
    Box.Title := ATitle; 
    Box.Description := AMsg; 
    Box.Delay := 0; 
    Box.HideAfter := 5000; 
    Box.Style := bhsStandard; 

    P := Point(0, 0); 
    SizingWindow := TCustomHintWindow.Create(nil); 
    try 
    SizingWindow.HintParent := Box; 
    SizingWindow.HandleNeeded; 
    SizingWindow.SetTitleDescription(ATitle, AMsg); 
    SizingWindow.AutoSize; 
    inc(P.X, SizingWindow.Width div 2); 
    finally 
    SizingWindow.Free; 
    end; 
    Box.ShowHint(P); 
end; 

這就是你要求的,但說實話,這讓我感覺很尷尬。

+1

感謝您的回覆。我將不得不嘲笑這樣做是否更好,或者我應該使用TCustomHint作爲「靈感」,並從頭開始建立自己的課堂。更多地傾向於後者。 – bstamour