C++builder自绘控件框架

#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


在上述框架的基础上,用GDI+编写仿360按钮,调用如下:

void __fastcall TForm1::isOnClick(TObject * Sender)//按钮的响应事件
{
    if(p->Status)
      Label1->Caption = "点击开启";
    else
      Label1->Caption = "点击关闭";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    p = new TSwitchButton(Form1);
    p->Setsize(TRect(20,20,150,50));
    p->OnClick = isOnClick;

}

调用代码,.h文件如下:
<pre name="code" class="cpp">#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "TScrollBoxControl.hpp"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        TLabel *Label1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
        TScrollBoxControl * p;//滚动条对象针
        void __fastcall show(int a,int b);//滚动条事件
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
.CPP文件代码,如下:
void __fastcall TForm1::show(int a,int b)
{ 
     Label1->Caption = "滚动条事件:"+(AnsiString(a)+","+b);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{ 
     p = new TScrollBoxControl(Form1); 
     p->SetWinodwColor(clBtnFace);   //设置背景颜色
     p->Setsize(TRect(20,20,36,300));//显示位置 
     p->SetShow(30,10);              //总数据,一页数据
     p->OnScrolloBox = show;         //事件
}

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值