TProgressBarStepIt (C++)
Description
This example reads bytes from a file in tenths of the size of the file. The ProgressBar displays the status of each 1/10th step as it is read into the buffer. This example requires that a TButton and a TProgressBar control are on the form.
Code
#include <stdio.h> // For FILE, fopen, fstat, fileno, fread and fclose
#include <sys\stat.h> // For fstat and the stat struct
#include <Math.hpp> // For Min
void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *F;
char MyData[32751];
long BytesRead;
F = fopen("../about.txt", "r"); // Path relative to Debug
// Use a file larger than 32752 bytes to make it interesting.
if (F)
{
struct stat statbuf;
fstat(fileno(F), &statbuf);
ProgressBar1->Max = statbuf.st_size;
if (ProgressBar1->Max > 10)
{
ProgressBar1->Step = (ProgressBar1->Max)/10;
ProgressBar1->Step = Min(ProgressBar1->Step, 32751);
}
else
ProgressBar1->Step = ProgressBar1->Max;
Label6->Caption = IntToStr(ProgressBar1->Max);
Label6->Repaint();
Label2->Caption = IntToStr(ProgressBar1->Step);
Label2->Repaint();
Label4->Caption = IntToStr(0);
Label4->Repaint();
Char* DataBuffer = new Char[ProgressBar1->Step];
for (ProgressBar1->Position = 0;
ProgressBar1->Position < ProgressBar1->Max;
ProgressBar1->StepIt()) // Move the ProgressBar Position using StepIt.
{
fread(DataBuffer, ProgressBar1->Step, 1, F);
// Do this or else the read wraps and starts over.
ProgressBar1->Step =
Min(ProgressBar1->Step, ProgressBar1->Max - ProgressBar1->Position);
Label4->Caption = IntToStr(ProgressBar1->Position);
Label4->Repaint();
Label2->Caption = IntToStr(ProgressBar1->Step);
Label2->Repaint();
Sleep(1000);
}
Label4->Caption = IntToStr(ProgressBar1->Position);
Label4->Repaint();
fclose(F);
delete [] DataBuffer;
}
}
Uses
- Vcl.ComCtrls.TProgressBar.Step ( fr | de | ja )
- Vcl.ComCtrls.TProgressBar.StepIt ( fr | de | ja )