C++builder彩色汽泡提示

本文地址:http://tongxinmao.com/Article/Detail/id/199

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef __BalloonTip__
#define __BalloonTip__
#include <windows>
/*
    .cpp调用方法:
    TBalloonTip * tip = new TBalloonTip;
 
    tip->SetBackColor(0xFDA528);
    tip->SetTextColor(0xFFFFFF);
    tip->ShowBalloonTip(Button1, TTI_INFO, "标题","这是一个汽泡自定义提示");
    tip->DelayTime(3000,tip);
    delete tip;
*/
#define ID_TIMER 1
int   CALLBACK   TimerProc();
//-------------------------------------------
class TBalloonTip : public TObject
{
    private:
 
        HWND hPareWnd;
        TOOLINFO ti;
 
        static int w    ;//汽泡窗口坐标
    static int h    ;
    static int Left ;
    static int Top  ;
 
        TWinControl * Control1;//保存控件指针
        void __fastcall MoveBalloonTip(HANDLE & handle, TWinControl * tWinCtrl);
    public:
        __fastcall TBalloonTip();
        void __fastcall ShowBalloonTip(TWinControl *Control, int  Icon,char *Title, char *Text);
        void deleteBalloonTip();
        void __fastcall SetBackColor(TColor);
        void __fastcall SetTextColor(TColor);
        void __fastcall DelayTime(int t);
        static UINT_PTR timerid; //定时器返回值
        void __fastcall OnHitTest(TMessage & message);
        static LRESULT CALLBACK  DlgMsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
        static int   CALLBACK   TimerProc();
        TColor backColor;
        TColor textColor;
        static HWND hBalloonTip;
        void __fastcall myAppMessage(tagMSG &Msg, bool &Handled);
        HANDLE __fastcall GetHandle();
         __fastcall ~TBalloonTip();
         
        enum{TIP_NONE=0,TIP_INFO,TIP_WARNING,TIP_ERROR};
   protected:
         
};
__fastcall TBalloonTip::~TBalloonTip()
{
    KillTimer(NULL,timerid);
    SendMessage(hBalloonTip, WM_CLOSE, 0, 0);
}
 
UINT_PTR TBalloonTip::timerid = 0;  //定时器返回值
HWND TBalloonTip::hBalloonTip = NULL;
 
HANDLE __fastcall TBalloonTip::GetHandle()
{
    return  hBalloonTip;
}
void __fastcall TBalloonTip::DelayTime(int t)
{
     timerid = SetTimer(NULL, 1, t, TimerProc);
}
 
int TBalloonTip::w =0;
int TBalloonTip::h=0;
int TBalloonTip::Left=0;
int TBalloonTip::Top =0;
__fastcall TBalloonTip::TBalloonTip()
{
    w = 0;
    h =0;
    Left = 0;
    Top  = 0;
    timerid = 0;
    Application->OnMessage = myAppMessage;
}
//显示汽泡提示
void __fastcall TBalloonTip::ShowBalloonTip(TWinControl *Control, int Icon,char *Title, char *Text)
{
    SendMessage(hBalloonTip, WM_CLOSE, 0, 0);//如果有汽泡先清除
    hPareWnd    = Control->Parent->Handle;
    Control1    = Control;
    hBalloonTip = CreateWindow(TOOLTIPS_CLASS, NULL,
                WS_POPUP| TTS_NOPREFIX | TTS_BALLOON | TTS_ALWAYSTIP ,
                CW_USEDEFAULT,CW_USEDEFAULT,0,0,
        hPareWnd, 0, HInstance, NULL);
 
    if( hBalloonTip )
    {
        ti.cbSize = sizeof(ti);
        /*
         *指定位置上显示气泡提示
        */
    ti.uFlags =   TTF_ABSOLUTE | TTF_SUBCLASS ;//| TTF_TRACK ; /*|TTF_TRANSPARENT | TTF_ABSOLUTE | TTF_TRACK */;
        ti.hwnd = hPareWnd;
        ti.lpszText = Text;
 
 
        SetWindowPos(hBalloonTip, HWND_TOP,
                0,0,0,0,
                SWP_NOACTIVATE);
 
    //set color
        SendMessage(hBalloonTip, TTM_SETTIPBKCOLOR, backColor, 0);    //背景颜色
        SendMessage(hBalloonTip, TTM_SETTIPTEXTCOLOR, textColor, 0);   //文字颜色
 
        SendMessage(hBalloonTip, TTM_ADDTOOL, WPARAM(1), LPARAM(&ti));       //添加汽泡
        SendMessage(hBalloonTip, TTM_SETTITLE, WPARAM(Icon), LPARAM(Title)); //文字标题
 
        // 定义气泡位置,粘附在指定控件旁边
        GetWindowRect(hPareWnd, &ti.rect);
        Left = ti.rect.left;
        Top =  ti.rect.top;
        w = ti.rect.right - ti.rect.left;
        h = ti.rect.bottom - ti.rect.top;
 
       LONG re = SendMessage(hBalloonTip,
                        TTM_TRACKPOSITION, WPARAM(1),
                        MAKELONG(w+Left,h+Top));
 
 
        //激活显示气泡
        re=SendMessage(hBalloonTip,
                        TTM_TRACKACTIVATE,
                        WPARAM(true), LPARAM(&ti));
 
    //注意销毁窗口
        UpdateWindow(hBalloonTip);
        //移动汽泡位置
        MoveBalloonTip(hPareWnd,Control1);
    }
}
//关闭汽泡
void  TBalloonTip::deleteBalloonTip()
{
    ti.cbSize = sizeof(ti);
    ti.hwnd = hPareWnd;
    SendMessage(hBalloonTip, TTM_DELTOOL, WPARAM(0), LPARAM(&ti));
}
//设置背景颜色
void __fastcall TBalloonTip::SetBackColor(TColor cl)
{
    backColor =  cl;
    SendMessage(hBalloonTip, TTM_SETTIPBKCOLOR, backColor, 0);
}
//设置文字颜色
void __fastcall TBalloonTip::SetTextColor(TColor cl)
{
    textColor = cl;
    SendMessage(hBalloonTip, TTM_SETTIPTEXTCOLOR, textColor, 0);
}
//------------------------------------
//定时器回调函数
int   CALLBACK   TBalloonTip::TimerProc()
{
    KillTimer(NULL,timerid);
    SendMessage(hBalloonTip, WM_CLOSE, 0, 0);
}
 
void __fastcall TBalloonTip::myAppMessage(tagMSG &Msg, bool &Handled)
{
    if (hBalloonTip && Msg.hwnd == hBalloonTip)
    {
        switch(Msg.message)
        {
          case WM_LBUTTONDOWN:
             KillTimer(NULL,timerid);
             SendMessage(hBalloonTip, WM_CLOSE, 0, 0);
             break;
        }
    }
 
    Handled = false;
}
//移动汽泡提示的位置
void __fastcall TBalloonTip::MoveBalloonTip(HANDLE & handle, TWinControl * Control1)
{
    long dwStyle = GetWindowLongA(hBalloonTip,GWL_STYLE);//获取旧样式
    dwStyle &= ~WS_POPUP;
    dwStyle |= WS_CHILD;    //按位与将旧样式去掉
    SetWindowLong(hBalloonTip,GWL_STYLE,dwStyle);
    SetParent(hBalloonTip,hPareWnd);
    TWinControl * tWinCrl;
    tWinCrl = FindControl(handle);
    INT CaptionHeight = tWinCrl->Height-tWinCrl->ClientHeight;//标题栏高度
    TRect rc;
    GetClientRect(hBalloonTip,&rc);
    MoveWindow(hBalloonTip,Control1->Left+Control1->Width/2,Control1->Top-Control1->Height-CaptionHeight,rc.Width(),rc.Height(),true);
 
}
#endif



http://blog.csdn.net/chinayu2007/article/details/40559189


上一篇:C++builder自绘控件框架
下一篇:共享单车(摩拜 小蓝 OFO 小鸣 永安,优拜,骑贝,快兔, hellobike )车锁技术方案