2014-10-08 7 views
8

TBalloonHint에 'x'(닫기) 아이콘을 표시하는 방법은 무엇입니까? TBalloonHint에 'x'아이콘 표시

enter image description here

내가 프로그래밍 형태에 시스템 트레이에 통지처럼 보이는 풍선 힌트를 제어 근처 을 표시합니다. 이것이 TBalloonHint이 할 수없는 것이라면 무엇을 사용해야합니까?

+1

바탕 윈도우 툴팁 컨트롤에 'TTS_CLOSE' 스타일을 설정하여 닫기 버튼을 추가했습니다. 그러나 당신이 델파이에서 어떻게 할 것인지 모르겠습니다. –

+3

'TBalloonHint'는 Windows 툴팁 컨트롤을 감싸는'TCustomHint'에서 파생되지만'TBalloonHint'는'TTS_CLOSE'에 필요한'TTS_BALLOON' 스타일을 사용하지 않습니다. 'TBalloonHint'는 Windows와 관련된 풍선 도움말을 모방 한 사용자 지정 그리기 툴팁입니다. 'TBalloonHint'에서 파생되고'PaintHint()'를 오버라이드하여 자신의 닫기 버튼을 그릴 수 있지만 버튼처럼 행동하지는 않습니다. –

답변

7

먼저 당신이 당신의 힌트 보여주기 위해 절차를 필요

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; 
+1

아주 좋습니다! 최신 버전의 Delphi (저는 XE5를 사용하고 있습니다)에서 이러한 상수와 TOOLINFO 레코드는 유니 코드 버전이 아니므로 사용해서는 안됩니다. CommCtrl에서 올바른 것을 정의했습니다. SendMessage의 정수형 변환은 wparam으로 변경되어야하며 lparam도 변환되어야합니다. – MarkF

+1

업데이트 해 주셔서 감사합니다. @MarkF 구성 요소를 만들려고했는데 방금 설명한 문제를 해결할 수 있습니다. –

+1

매우 잘 작동합니다. Windows Vista 이상에서는 다음 아이콘을 사용할 수 있습니다. 4 = TTI_INFO_LARGE, 5 = TTI_WARNING_LARGE, 6 = TTI_ERROR_LARGE (http://msdn.microsoft.com/en-us/library/windows/desktop/bb760414(v=vs). 85) .aspx) – Pol