在TBalloonHint中显示"x"图标 [英] Displaying 'x' icon in TBalloonHint

查看:54
本文介绍了在TBalloonHint中显示"x"图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to display 'x' (close) icon in TBalloonHint?

我想以编程方式显示窗体上的控件附近一个气球提示,该气球提示看起来像系统托盘中的通知.如果这不是 TBalloonHint 所能做的,那我应该怎么用?

I want to programmatically display near a control on form a balloon hint that looks like notifications in system tray. If this is not what TBalloonHint can do, what should I use?

推荐答案

首先,您需要一个过程来显示提示:

First you need a procedure to show your hint :

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消息.

Hint: if you don’t have hWnd (e.g. Speed Buttons or other graphic component) or want to show the baloon elsewhere send TTM_TRACKPOSITION message after TTM_SETTITLE.

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

***** EDIT *****

这也可以通过类帮助器

首先使用 Class helper

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;

这篇关于在TBalloonHint中显示"x"图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆