progressbar.h
#pragma once
#include <iostream>
#include <algorithm>
#include <tchar.h>
typedef int(*MyProgress)(double dfComplete, const char *pszMessage, void * pProgressArg);//聲明函數指針
int Progress(double dfComplete, const char *pszMessage, void * pProgressArg);
int Progress(double dfComplete, const char *pszMessage, void * pProgressArg)
{
int nThisTick = (std::min)(40, (std::max)(0, static_cast<int>(dfComplete * 40.0)));
// Have we started a new progress run?
static int nLastTick = -1;
if (nThisTick < nLastTick && nLastTick >= 39)
nLastTick = -1;
if (nThisTick <= nLastTick)
return true;
while (nThisTick > nLastTick)
{
++nLastTick;
if (nLastTick % 4 == 0)
fprintf(stdout, "%d", (nLastTick / 4) * 10);
else
fprintf(stdout, ".");
}
if (nThisTick == 40)
fprintf(stdout, " - done.\n");
else
fflush(stdout);
return true;
}
測試
#include <iostream>
#include<Windows.h>
#include "progressbar.h"
using namespace std;
void print(int Size, MyProgress callback, void * pProgressArg);
int _tmain(int argc, _TCHAR* argv[])
{
void * pProgressArg = NULL;
int nYSize = 10;
print(nYSize, Progress, pProgressArg);
return 0;
}
void print(int Size, MyProgress callback, void * pProgressArg)
{
for (int i = 0; i < Size; i++)
{
Sleep(1000);
callback( ((i + 1) / (double)Size),
"", pProgressArg);//
}
}