2012-10-11 93 views
4

我試圖在組件的運行時臨時更改提示文本, 而不更改Hint屬性本身。在delphi上攔截提示事件

我試過捕捉CM_SHOWHINT,但是這個事件似乎只能到 的形式,而不是組件本身。

插入CustomHint也不會有效,因爲它需要Hint屬性中的文本 。

我的組件是從TCustomPanel

後裔這裏就是我想要做的事:

procedure TImageBtn.WndProc(var Message: TMessage); 
begin 
    if (Message.Msg = CM_HINTSHOW) then 
    PHintInfo(Message.LParam)^.HintStr := 'CustomHint'; 
end; 

我發現這段代碼在互聯網上的某個地方,遺憾的是它不壽工作。

回答

8

CM_HINTSHOW的確是你所需要的。這裏有一個簡單的例子:

type 
    TButton = class(Vcl.StdCtrls.TButton) 
    protected 
    procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW; 
    end; 

    TMyForm = class(TForm) 
    Button1: TButton; 
    end; 

.... 

procedure TButton.CMHintShow(var Message: TCMHintShow); 
begin 
    inherited; 
    if Message.HintInfo.HintControl=Self then 
    Message.HintInfo.HintStr := 'my custom hint'; 
end; 

在問題的代碼無法調用inherited這可能是失敗的原因。或者也許類聲明省略WndProc上的override指令。無論如何,它在這個答案中的使用方式都很乾淨。

+0

是的!這正是我所需要的,非常感謝! – ertx

+0

您也可以使用「TApplication.OnShowHint」事件完成相同的事情。 –

+0

@Remy優點和缺點。如果你正在編寫一個組件,那麼這個消息是最好的。如果您想應用應用程序範圍策略,則OnShowHint會獲勝。 –

6

您可以使用OnShowHint事件

它HintInfo參數:http://docwiki.embarcadero.com/Libraries/XE3/en/Vcl.Forms.THintInfo

該參數可以讓你查詢的提示控制,提示文字和一切上下文 - 並根據需要覆蓋它們。

如果要過濾的組件來改變提示你可以,例如,宣佈某種ITemporaryHint界面像

type 
    ITemporaryHint = interface 
    ['{1234xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}'] 
    function NeedCustomHint: Boolean; 
    function HintText: string; 
    end; 

然後你可以統稱以後檢查您的任何組件,它們是否執行該接口

procedure TForm1.DoShowHint(var HintStr: string; var CanShow: Boolean; 
    var HintInfo: THintInfo); 
var 
    ih: ITemporaryHint; 
begin 
    if Supports(HintInfo.HintControl, {GUID for ITemporaryHint}, ih) then 
    if ih.NeedCustomHint then 
     HintInfo.HintStr := ih.HintText; 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    Application.ShowHint := True; 
    Application.OnShowHint := DoShowHint; 
end; 
+1

哦,@TLama,看起來不一致的做那些替換在一起:布爾 - >布爾和字符串 - 字符串。但是,DoShowHint聲明直接來自Delphi DocWiki示例。我會相信他們選擇的字符串。 –

+0

它不是我的頭,它是如何定義的... – TLama