自绘进度条TProgressbarControl类

来源:互联网 发布:人工智能的利弊300字 编辑:程序博客网 时间:2024/06/06 13:58
#ifndef __TPROGRESSCONTROL__#define __TPROGRESSCONTROL__#include "gdi.h"#include <math.h>enum TProgressbarControlStyle{oneStyle,twoStyle,threeStyle,fourStyle,fiveStyle};class TProgressBarControl :public TCustomControl{   private:      TCanvas * FCanvas;      int m_left,m_top,m_width,m_height;      Graphics::TBitmap * m_background_bmp;//背景图      TNotifyEvent  FOnClick;  //定义外部事件      float m_start_value ; //进度条起始位置      float m_max_value;    //进度条最大值      float m_current_value;//当前位置      float m_scale_value;  //宽度/最大值            unsigned int m_background_color;//背景色      unsigned int m_foreground_color;//前景色      unsigned int m_out_border_color;//外边框颜色      unsigned int m_in_border_color; //内边框颜色      TProgressbarControlStyle m_style;//风格      AnsiString m_caption;      bool m_visiable_caption; //是否显示标题   protected:      void Draw();      BEGIN_MESSAGE_MAP          VCL_MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, MouseLeave)          VCL_MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, MouseEnter)      END_MESSAGE_MAP(TCustomControl)      void __fastcall MouseLeave(TMessage & msg);//鼠标离开控件      void __fastcall MouseEnter(TMessage & msg);//鼠标进入控件      void DrawStyleOne();  //第一种风格      void DrawStyleTwo();  //第二种风格      void DrawStyleThree();//第三种风格      void DrawStyleFour(); //画第四种风格      void DrawStyleFive(); //第五种风格      void DrawCaption(bool value);   //画标题%进度,参数为是否显示   public:      void SetSize(TRect &r);      void SetStartValue(float value);      void SetMaxValue(float value);      void SetCurrentValue(float value);      void SetBackgroundColor(unsigned int value);      void SetForegroundColor(unsigned int value);      void SetStyle(TProgressbarControlStyle value);      void SetVisiableCaption(bool value);//进度文字是否显示      //--------------------------------------------------      __fastcall TProgressBarControl(TComponent* Owner);      __fastcall ~TProgressBarControl();      //--------------------------------------------------父类成员函数      void __fastcall Paint(void);      void __fastcall CreateParams(TCreateParams &Params);      DYNAMIC void __fastcall MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);      DYNAMIC void __fastcall MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);      DYNAMIC void __fastcall MouseMove(Classes::TShiftState Shift, int X, int Y);      //属性      __property Graphics::TCanvas* Canvas = {read=FCanvas};      __property Width;      __property Height;      __property Font;      __property float StartProgress={read=m_start_value,write=SetStartValue};      __property float MaxProgress  ={read=m_max_value,write=SetMaxValue};      __property float CurrentProgress={read=m_current_value,write=SetCurrentValue};      __property unsigned int BackgroundColor={read=m_background_color,write=SetBackgroundColor};      __property unsigned int ForegroundColor={read=m_foreground_color,write=SetForegroundColor};      __property TProgressbarControlStyle Style={read=m_style,write=SetStyle};      __property bool VisiableCaption ={read=m_visiable_caption,write=SetVisiableCaption};}; __fastcall  TProgressBarControl::TProgressBarControl(TComponent* Owner)        : TCustomControl(Owner){     TCustomControl * Win = (TCustomControl *)(Owner);     this->Parent = Win;     m_background_bmp =  new Graphics::TBitmap;     FCanvas = TCustomControl::Canvas;     m_start_value = 0;     m_max_value   = 100;     m_background_color = 0xffCEE3F4;     m_foreground_color = 0xff279CFC;     m_style = oneStyle;     Font->Name = "微软雅黑";     Font->Size = 10;     GdiInit();}void __fastcall TProgressBarControl::CreateParams(TCreateParams &Params){     TWinControl::CreateParams(Params);}void TProgressBarControl::SetSize(TRect &r){    this->Left   = r.left;    this->Top    = r.top;    this->Width  = r.Width();    this->Height = r.Height();    m_left = r.left;    m_top  = r.top;    m_width= r.Width();    m_height=r.Height();    m_background_bmp->Width = m_width;    m_background_bmp->Height= m_height;    m_start_value = 0;    m_scale_value = (float)this->Width/(float)(m_max_value+4);    //--------------------------------------------    if(m_start_value > m_max_value)m_start_value=m_max_value;    if(m_current_value > m_max_value)m_current_value=m_max_value;    //--------------------------------------------}void TProgressBarControl::Draw(){    GdiCreateHandle1(m_background_bmp->Canvas->Handle);    if(m_style == oneStyle)       DrawStyleOne();    if(m_style == twoStyle)       DrawStyleTwo();    if(m_style == threeStyle)       DrawStyleThree();    if(m_style == fourStyle)       DrawStyleFour();    if(m_style == fiveStyle)       DrawStyleFive();     DrawCaption(m_visiable_caption);         GdiReleaseGraphics();}void __fastcall TProgressBarControl::Paint(void){   TRect a(0,0,m_width,m_height);   TRect b(0,0,m_width,m_height);      if(m_style == oneStyle || m_style == threeStyle)     FCanvas->CopyRect(a,m_background_bmp->Canvas,b);   if(m_style == twoStyle || m_style == fourStyle || m_style == fiveStyle)   {      m_background_bmp->Transparent = true;      m_background_bmp->TransparentColor = clWhite;      FCanvas->Draw(0,0,m_background_bmp);   }}//鼠标进入控件区void __fastcall TProgressBarControl::MouseEnter(TMessage & msg){}//鼠标离开控件区void __fastcall TProgressBarControl::MouseLeave(TMessage & msg){}void __fastcall TProgressBarControl::MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y){}void __fastcall TProgressBarControl::MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y){   if(FOnClick)   {      FOnClick(this);   }}void __fastcall TProgressBarControl::MouseMove(Classes::TShiftState Shift, int X, int Y){}__fastcall TProgressBarControl::~TProgressBarControl(){   delete m_background_bmp;}//todo:设置起始值void TProgressBarControl::SetStartValue(float value){   if(value > m_max_value)     m_start_value = m_max_value;   else     m_start_value = value;   Draw();   Paint();}//todo:设置最大值void TProgressBarControl::SetMaxValue(float value){   m_max_value   = value;   m_scale_value = this->Width / value;   Draw();   Paint();}//todo::设置当前值void TProgressBarControl::SetCurrentValue(float value){   if(value >= m_max_value)     m_current_value = m_max_value-1;   else     m_current_value = value;   if(m_current_value < 0) return;   Draw();   Paint();}void TProgressBarControl::SetBackgroundColor(unsigned int value){   m_background_color = value;   Draw();   Paint();}void TProgressBarControl::SetForegroundColor(unsigned int value){   m_foreground_color = value;   Draw();   Paint();}//画第一种风格void TProgressBarControl::DrawStyleOne(){    GdiPen(0xff0C80E8,1);    GdiDrawRectangle(0,0,m_width-1,m_height-1);    GdiFillRectSolidBrush(m_background_color,0,0,m_width-1,m_height-1);    GdiFillRectSolidBrush(m_foreground_color,1,1,m_scale_value*m_start_value-2+m_current_value*m_scale_value,m_height-3);}void TProgressBarControl::DrawStyleTwo(){    GdiFillRectSolidBrush(0xffffffff,0,0,m_width,m_height);    GdiPen(0xff0C80E8,1);    GdiDrawRoundRect(0,0,m_width-1,m_height-1,5,5);    GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,5,5);    if(m_start_value+m_current_value >3 )      GdiFillRoundSolidBrush(m_foreground_color,1,1,m_scale_value*m_start_value+m_current_value*m_scale_value,m_height-3,3,3);}void TProgressBarControl::DrawStyleThree(){    GdiPen(0xff0C80E8,1);    GdiDrawRectangle(0,0,m_width-1,m_height-1);    GdiFillRectSolidBrush(m_background_color,0,0,m_width-1,m_height-1);    GdiFillRectSolidBrush(m_foreground_color,1,1,m_scale_value*m_start_value-2+m_current_value*m_scale_value,m_height-3);    GdiFillRectSolidBrush(0x3fffffff,1,1,m_scale_value*m_start_value-2+m_current_value*m_scale_value,m_height/2-3);}void TProgressBarControl::DrawStyleFour(){    GdiFillRectSolidBrush(0xffffffff,0,0,m_width,m_height);    GdiPen(0xff0C80E8,1);    GdiDrawRoundRect(0,0,m_width-1,m_height-1,5,5);    GdiFillRoundSolidBrush(m_background_color,0,0,m_width-1,m_height-1,5,5);    if(m_start_value+m_current_value >3 )    {      GdiFillRoundSolidBrush(m_foreground_color,1,1,m_scale_value*m_start_value+m_current_value*m_scale_value,m_height-3,3,3);      GdiFillRectSolidBrush(0x4fffffff,1,1,m_scale_value*m_start_value+m_current_value*m_scale_value,m_height/2-2);    }}//第五种风格void TProgressBarControl::DrawStyleFive(){    //外边框    GdiPen(0xffCCCDCE,1);    GdiBursh(0,0,m_width,m_height,0xffCCCDCE,0xffE1E3E3,1);    GdiDrawRoundRect(0,0,m_width-2,m_height-1,5,5);    GdiFillRoundRectangle(0,0,m_width-2,m_height-1,5,5);    int l_width = (m_start_value+m_current_value)*m_scale_value;    int l_round = 0;    if(l_width > 0 )    {      if(l_width > 3)l_round = 3;      //内边框      GdiPen(0xff69AC17,1);      GdiDrawRoundRect(0,0,l_width,m_height-1,l_round,l_round);      //内填充      GdiBursh(1,1,m_width-2,m_height,0xff7DBE29,0xffAFE81A,0);      GdiFillRoundRectangle(1,1,l_width-2,m_height-3,l_round,l_round);    }}//风格选择void TProgressBarControl::SetStyle(TProgressbarControlStyle value){    m_style = value;    m_scale_value = (float)this->Width/(float)m_max_value;        Draw();    Paint();}//画百分比void TProgressBarControl::DrawCaption(bool value){   if(value)   {       if(m_current_value >= m_max_value-1)m_current_value = m_max_value;       float l_percent = m_current_value*100.0 / m_max_value;//百分比       GdiSetFont(Font->Name,Font->Size);       AnsiString l_per_str = AnsiString(int(l_percent))+AnsiString("%");       TSize a = GdiMeasureString(l_per_str);       if(l_percent < 50)         GdiDrawString(l_per_str,(m_width-a.cx)/2,(m_height-a.cy)/2,0xff7E7967);//白色文字,显示进度       else         GdiDrawString(l_per_str,(m_width-a.cx)/2,(m_height-a.cy)/2,0xffffffff);//白色文字,显示进度   }}//进度文字是否显示void TProgressBarControl::SetVisiableCaption(bool value){   m_visiable_caption =  value;   Draw();   Paint();}#endif 

__fastcall TForm1::TForm1(TComponent* Owner)        : TForm(Owner){     tControl = new TProgressBarControl(this);     tControl->SetSize(TRect(130,150,370,170));     tControl->Style = fiveStyle;//先于其他方法调用     tControl->StartProgress = 0;     tControl->VisiableCaption = true;//是否显示进度文字     }//---------------------------------------------------------------------------int i=0;void __fastcall TForm1::Button1Click(TObject *Sender){    tControl->SetCurrentValue(i++);}//---------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender){   tControl->SetCurrentValue(i--);}//---------------------------------------------------------------------------


0 0