delphi - 在 TBalloonHint 中显示 'x' 图标

标签 delphi winapi balloon-tip

如何在TBalloonHint中显示“x”(关闭)图标?

enter image description here

我想以编程方式在表单上的控件附近显示一个气球提示,看起来像系统托盘中的通知。如果这不是 TBalloonHint 可以做到的,我应该使用什么?

最佳答案

首先你需要一个程序来显示你的提示:

uses
  CommCtrl;

// hWnd - control window handle to attach the baloon to.
// Icon - icon index; 0 = none, 1 = info, 2 = warning, 3 = error.
// BackCL - background color or clDefault to use system setting.
// TextCL - text and border colors or clDefault to use system setting.
// Title - tooltip title (bold first line).
// Text - tooltip text.

procedure ShowBalloonTip(hWnd: THandle; Icon: integer; BackCL, TextCL: TColor; Title: pchar; Text: PWideChar);
const
  TOOLTIPS_CLASS = 'tooltips_class32';
  TTS_ALWAYSTIP = $01;
  TTS_NOPREFIX = $02;
  TTS_BALLOON = $40;
  TTF_SUBCLASS = $0010;
  TTF_TRANSPARENT = $0100;
  TTF_CENTERTIP = $0002;
  TTM_ADDTOOL = $0400 + 50;
  TTM_SETTITLE = (WM_USER + 32);
  ICC_WIN95_CLASSES = $000000FF;
type
  TOOLINFO = packed record
    cbSize: integer;
    uFlags: integer;
    hWnd: THandle;
    uId: integer;
    rect: TRect;
    hinst: THandle;
    lpszText: PWideChar;
    lParam: integer;
  end;

var
  hWndTip: THandle;
  ti: TOOLINFO;
begin
  hWndTip := CreateWindow(TOOLTIPS_CLASS, nil, WS_POPUP or TTS_CLOSE or TTS_NOPREFIX or TTS_BALLOON or TTS_ALWAYSTIP, 0, 0, 0, 0, hWnd, 0, HInstance, nil);

  if hWndTip <> 0 then
  begin
    SetWindowPos(hWndTip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);

    ti.cbSize := SizeOf(ti);
    ti.uFlags := TTF_CENTERTIP or TTF_TRANSPARENT or TTF_SUBCLASS;
    ti.hWnd := hWnd;
    ti.lpszText := Text;

    Windows.GetClientRect(hWnd, ti.rect);
    if BackCL <> clDefault then
      SendMessage(hWndTip, TTM_SETTIPBKCOLOR, BackCL, 0);

    if TextCL <> clDefault then
      SendMessage(hWndTip, TTM_SETTIPTEXTCOLOR, TextCL, 0);

    SendMessage(hWndTip, TTM_ADDTOOL, 1, integer(@ti));
    SendMessage(hWndTip, TTM_SETTITLE, Icon mod 4, integer(Title));

    //TTM_TRACKACTIVATE => Makes sure you have to close the hint you self
    SendMessage(hWndTip, TTM_TRACKACTIVATE, integer(true), integer(@ti));
  end;
end;

然后调用它:

ShowBalloonTip(Button1.Handle, 4, clDefault, clRed, 'Baloon Title', 'Baloon text');

提示:如果您没有 hWnd(例如速度按钮或其他图形组件)或想要在其他地方显示气球,请在 TTM_SETTITLE 之后发送 TTM_TRACKPOSITION 消息。

***** 编辑 *****

这也可以通过类助手来完成

首先创建一个带有类助手的单元

unit ComponentBaloonHintU;

interface
uses
  Controls, CommCtrl, Graphics;

{$SCOPEDENUMS ON}

type
  TIconKind = (None = TTI_NONE, Info = TTI_INFO, Warning = TTI_WARNING, Error = TTI_ERROR, Info_Large = TTI_INFO_LARGE, Warning_Large = TTI_WARNING_LARGE, Eror_Large = TTI_ERROR_LARGE);
  TComponentBaloonhint = class helper for TWinControl
  public
    procedure ShowBalloonTip(Icon: TIconKind; const Title, Text: string);
  end;

implementation
uses
  Windows;

{ TComponentBaloonhint }

procedure TComponentBaloonhint.ShowBalloonTip(Icon: TIconKind; const Title, Text: string);
var
  hWndTip: THandle;
  ToolInfo: TToolInfo;
  BodyText: pWideChar;
begin
  hWndTip := CreateWindow(TOOLTIPS_CLASS, nil, WS_POPUP or TTS_CLOSE or TTS_NOPREFIX or TTS_BALLOON or TTS_ALWAYSTIP, 0, 0, 0, 0, Handle, 0, HInstance, nil);

  if hWndTip = 0 then
    exit;

  GetMem(BodyText, 2 * 256);

  try
    ToolInfo.cbSize := SizeOf(TToolInfo);
    ToolInfo.uFlags := TTF_CENTERTIP or TTF_TRANSPARENT or TTF_SUBCLASS;
    ToolInfo.hWnd := Handle;
    ToolInfo.lpszText := StringToWideChar(Text, BodyText, 2 * 356);
    SetWindowPos(hWndTip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
    ToolInfo.Rect := GetClientRect;

    SendMessage(hWndTip, TTM_ADDTOOL, 1, integer(@ToolInfo));
    SendMessage(hWndTip, TTM_SETTITLE, integer(Icon), integer(PChar(Title)));
    SendMessage(hWndTip, TTM_TRACKACTIVATE, integer(true), integer(@ToolInfo));
  finally
    FreeMem(BodyText);
  end;
end;

end.

然后调用它:

uses
  ComponentBaloonHintU;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.ShowBalloonTip(TIconKind.Eror_Large, 'Baloon Title', 'Baloon text');
end;

关于delphi - 在 TBalloonHint 中显示 'x' 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26247528/

相关文章:

c# - 如何在文本框上显示气球提示?

delphi - 如何在64位Delphi XE2中将方法转换为回调过程?

delphi - 在 Delphi 5 中,Free 可以引发异常吗?

c++ - 关于选择正确的数据类型和算法将数据插入数据库所需的建议

c# - 烦人的 NotifyIcon.ShowBalloonTip 行为

python - 如何使用 python 从气球弹出窗口中读取文本?

delphi - 我得到 RTTIMethod.Visibility = mvPublic 作为私有(private)记录方法。 - 漏洞?

c++ - 如何将 C++ _tcscpy、_tcscat 转换为 Delphi?

c++ - CreateFile,ReadDirectoryChanges 问题

c - Windows 相当于 sync()