C++builder彩色汽泡提示

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

    
    #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 )车锁技术方案