【问题标题】:Intercepting hint event on delphi在delphi上拦截提示事件
【发布时间】:2012-10-11 11:22:26
【问题描述】:

我正在尝试在组件内部的运行时临时更改提示文本, 无需更改 Hint 属性本身。

我试过抓CM_SHOWHINT,但这个事件似乎只来了 表单,而不是组件本身。

插入 CustomHint 也不起作用,因为它需要文本 来自Hint 属性。

我的组件是TCustomPanel的后代

这是我想要做的:

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

我在互联网的某个地方找到了这段代码,不幸的是它不起作用。

【问题讨论】:

    标签: delphi delphi-xe2 windows-messages hint


    【解决方案1】:

    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 指令。没关系,我在这个答案中的方式更干净。

    【讨论】:

    • 您也可以改用TApplication.OnShowHint 事件来完成同样的事情。
    • @Remy 各有利弊。如果您正在编写一个组件,那么该消息是最好的。如果您想应用应用程序范围的策略,那么 OnShowHint 胜出。
    【解决方案2】:

    你可以使用 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;  
    

    【讨论】:

    • 哦,@TLama,将这些替换在一起似乎不一致:布尔值 -> 布尔值和字符串 - 字符串。然而,DoShowHint 声明直接取自 Delphi DocWiki 示例。我相信他们对 String 的选择。
    猜你喜欢
    • 2019-06-04
    • 2013-08-07
    • 2018-01-23
    • 2011-07-25
    • 1970-01-01
    • 2015-11-05
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多