C++builder自绘控件框架
本文地址:http://txm.tongxinmao.com/Article/Detail/id/198
http://blog.csdn.net/chinayu2007/article/details/48244769
#ifndef __switch1__
#define __switch1__
#include "gdi.h"  //自定义GDI+函数库
typedef void __fastcall(__closure * TMyEvent)(int a,int b);//多个参数事件类型
class TSwitchButton : public TGraphicControl
{
    private:
      int m_left;
      int m_top;
      int m_width;
      int m_height;
      ::Graphics::TBitmap * m_background_bmp;
      ::Graphics::TBitmap * m_active_bmp;
      TNotifyEvent FOnClick;//定义事件类型,仅一个参数Sender
      unsigned int window_color;//设置背景色,用于控件透明
    protected:
        BEGIN_MESSAGE_MAP
            VCL_MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, MouseLeave)
        END_MESSAGE_MAP(TGraphicControl)
        void __fastcall MouseLeave(TMessage & msg);
    public:
      unsigned int background_start_color; //背景色
      unsigned int background_end_color;
      unsigned int foreground_start_color;
      unsigned int foreground_end_color;
      void SetControlTransparentColor(unsigned int color);//修改控件透明色
      bool Status;
      void Setsize(TRect & r);
      void Draw();
      virtual void __fastcall Paint(void);
      DYNAMIC void __fastcall MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
      DYNAMIC void __fastcall MouseMove(Classes::TShiftState Shift, int X, int Y);
      DYNAMIC void __fastcall MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
      __fastcall TSwitchButton(TComponent* AOwner);
      __fastcall ~TSwitchButton();
      __property Left   ;
      __property Top    ;
      __property Width  ;
      __property Height ;
      __published:  
          __property TNotifyEvent OnClick={read=FOnClick,write=FOnClick};//声明成属性器,方便调用
};
//构造函数
__fastcall TSwitchButton::TSwitchButton(TComponent* AOwner):TGraphicControl(AOwner)
{
     m_background_bmp = new ::Graphics::TBitmap;
     m_active_bmp     = new ::Graphics::TBitmap;
     this->Parent =(TWinControl *) AOwner;
     GdiInit();
}
void TSwitchButton::Setsize(TRect & r)
{
     m_background_bmp->Width  = r.Width();
     m_background_bmp->Height = r.Height();
     m_active_bmp->Width      = r.Width()/2-2;
     m_active_bmp->Height     = r.Height()-2;
     m_left   = r.left;
     m_top    = r.top;
     m_width  = r.Width();
     m_height = r.Height();
     this->Left   = m_left;
     this->Top    = m_top;
     this->Width  = m_width;
     this->Height = m_height;
     //-----------------------------------------------------
     GdiCreateHandle1(m_background_bmp->Canvas->Handle);
     GdiPen(0xffff0000,1);
     GdiDrawRoundRect(0,0,m_width-1,m_height-1,30,30);
     GdiReleaseGraphics();
}
void TSwitchButton::Draw()//绘图集中操作函数
{
     m_active_bmp->Assign(m_background_bmp);//复制背景
     GdiCreateHandle1(m_active_bmp->Canvas->Handle);
     //...绘图功能函数
     GdiReleaseGraphics();
}
void __fastcall TSwitchButton::Paint(void)
{
    TRect a(0,0,m_width,m_height);
    TRect b(0,0,m_width,m_height);
    Canvas->CopyMode = SRCCOPY;
    Canvas->CopyRect(a,m_background_bmp->Canvas,b);
}
void __fastcall TSwitchButton::MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y)
{
    Paint();
    if(FOnClick)//激活自定义事件
    {
        FOnClick(this);//调用点击外部挂接的事件
    }
}
__fastcall TSwitchButton::~TSwitchButton()
{
    delete m_background_bmp;
    delete m_active_bmp;
    GdiClose();
}
void TSwitchButton::SetControlTransparentColor(unsigned int color)
{
    window_color = color;
    Setsize(TRect(m_left,m_top,m_width+m_left,m_height+m_top));
    Draw();
    Paint();
}
void __fastcall TSwitchButton::MouseLeave(TMessage & msg)
{
    //...
    Draw();
    Paint();
}
#endif上一篇:mingw32 mingw64  msys cygwin TDM-GCC
下一篇:C++builder彩色汽泡提示