CSDN博客

img cker

ProgressBar 实现自定义显示进度值

发表于2004/6/23 22:45:00  4239人阅读

分类: CSDN问答集

Q: http://community.csdn.net/Expert/TopicView.asp?id=3116178

VCL类库中的TProgressBar是对Common Control的封装。所以如果想要一个自定义进度值显示的ProgressBar的话,虽然可以自己从TCustomControl继承一个完全自行实现,SubClass子类化ProgressBar的WindowProc也不失为一个可取的方法。

#include

#pragma hdrstop

 

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

{

 FOldWndProc = ProgressBar1->WindowProc;

 ProgressBar1->WindowProc = ProgressBarWndProc;

 

 ProgressBar1->Smooth = true;

}

//---------------------------------------------------------------------------

__fastcall TForm1::~TForm1()

{

 

 ProgressBar1->WindowProc = FOldWndProc;

 FOldWndProc = 0;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::ProgressBarWndProc(TMessage& Message)

{

 TProgressBar* ProgressBar = ProgressBar1;

 TCanvas* Canvas;

 Graphics::TBitmap* B;

 switch(Message.Msg)

 {

   case WM_PAINT:

   {

       Canvas = new TCanvas;

       try

       {

 

         Canvas->Handle = GetDC(ProgressBar->Handle);

         try

         {

           // erase background

           TRect R = ProgressBar->ClientRect;

           Canvas->Brush->Color = this->Color;

           Canvas->FillRect(R);

           FOldWndProc(Message);

           // draw text

           B = new Graphics::TBitmap;

           try

           {

             B->Width = R.Right - R.Left;

             B->Height = R.Bottom - R.Top;

             B->Canvas->Brush->Color = clBlack;

             B->Canvas->FillRect(R);

             B->Canvas->Font->Style = TFontStyles()<

             B->Canvas->Font->Color = clWhite;

             AnsiString Str;

             Str.sprintf("%dk/%dk", ProgressBar->Position , ProgressBar->Max);

             DrawText(B->Canvas->Handle, Str.c_str(), -1, &R, DT_NOCLIP | DT_CENTER);

             BitBlt(Canvas->Handle, 0, 0, B->Width, B->Height, B->Canvas->Handle, 0, 0, SRCINVERT);

           }

           __finally

           {

             delete B;

           }

         }

         __finally

         {

           ReleaseDC(ProgressBar->Handle, Canvas->Handle);

         }

       }

       __finally

       {

         delete Canvas;

       }

   }

   break;

 

   default:

        FOldWndProc(Message);

 }

 

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  ProgressBar1->Position = 0;

  while(ProgressBar1->Position<100)

  {

    ++ ProgressBar1->Position ;

    Sleep(50);

    Application->ProcessMessages();

  }

}

 

0 0

相关博文

我的热门文章

img
取 消
img