c++实现进度条

shaowutaojiang 2010-03-11 03:11:20
现在下载的程序已经做好,如何在下载的时候出现进度条。。求教
#include "stdafx.h"
#include <afx.h>
#include <afxdb.h>
#include <afxinet.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <string>
using namespace std;
using std::string;

#define APP_NAME_STR "setup"

bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */);

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static char subkey[] = "SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\JavaQuickStarterService";
static char vname[] = "EventMessageFile";
ULONG dType=REG_SZ,len=0;
HKEY hKey;

//实现对话框的参数
char format[] = "\\%s\\%s\n";
char hello[] = "网络连接异常。请确认您计算机的互联网连接正常,再重新运行安装程序!";
char world[] = "警告";

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
ShowWindow(FindWindow("ConsoleWindowClass",argv[0]),0); //隐藏自身
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}

string remoteFilename[6] = {"a001","a002","a003","a004","a005","a006"};
string localFilename[6] = {"a001","a002","a003","a004","a005","a006"};

//循环下载文件
for(int i=0;i<6;i++)
{
bool state;
string rfn = remoteFilename[i];
string lfn = localFilename[i];
//////////////////////////////////////////////////////////////////////////
// //
// 这里是本地测试用到的FTP服务器信息,在测试时换成你的FTP服务器信息 //
// //
//////////////////////////////////////////////////////////////////////////
const char pcLocalFile[40]="pcLocalFile";//本地文件名
strcpy((char*)pcLocalFile,rfn.c_str());

//to do ......

const char pcRemoteFile[40]="pcRemoteFile";//远程文件名
strcpy((char*)pcRemoteFile,lfn.c_str());//需要从服务器上下载的东西


const char pcServer[50]="192.168.100.80";//ip地址
const char pcUserName[6]="";//用户名
const char pcPassword[10]="";//密码

// printf("请输入下到本机的路径:\n");

// scanf("%s",pcLocalFile);
printf("正在下载,请稍后。。。1\n");

state=FtpDownload(pcLocalFile,pcRemoteFile,pcServer,pcUserName,pcPassword,110,TRUE);
printf("正在下载,请稍后。。。2\n");
if(state)
{
printf("下载完毕!\n");
}
else
{
printf("下载失败,请从新运行本程序!\n");
}
}
system("cmd /c copy /b a*. JRE.exe");
ShellExecute(NULL, "open", "JRE.exe", NULL, NULL, SW_SHOWNORMAL);

while(1)
{
RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);//再次打开注册表
if(RegQueryValueEx(hKey,vname,0,&dType,NULL,&len))
{ //如果没有EventMessageFile就延时2秒
Sleep(3000);
}
else
{
Sleep(3000);
ShowWindow(FindWindow("ConsoleWindowClass",argv[0]),0); //隐藏自身
ShellExecute(NULL, "open", "http://app2.oksbt.com/jnlp/FshTaobaoSimLimitMainServlet/USERC19BE5E7263A793C03F158D53F64EDB5_31328734.jnlp", NULL, NULL, SW_SHOWNORMAL);//打开指定网页
break;
}
}

Sleep(20000);
return nRetCode;
}
int fileExists(char * filename)
{
return (access(filename,0)==0);
}

//用于实现从ftp服务器上下载文件

bool FtpDownload(const char * pcLocalFile,const char * pcRemoteFile,const char * pcServer,const char * pcUserName,const char * pcPassword, int nPort /* = 21 */, BOOL bPassive /* = TRUE */)
{
CInternetSession * pInetSession = NULL;
CFtpConnection * pFtpConnection = NULL;
bool bRet = false;

if (pInetSession = new CInternetSession(APP_NAME_STR, 1, PRE_CONFIG_INTERNET_ACCESS))
{
pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
pInetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pInetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 1000);
pInetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, 1000);

TRACE("[FTP Addr]\t\t%s\n", pcServer);
TRACE("[FTP User Name]\t%s\n", pcUserName);
TRACE("[FTP Password]\t%s\n", pcPassword);
try
{
pFtpConnection = pInetSession->GetFtpConnection(pcServer, pcUserName, pcPassword, nPort, bPassive);
}
catch (CInternetException *pEx)
{
pFtpConnection = NULL;
pEx->ReportError(MB_ICONEXCLAMATION);
pEx->Delete();
}
if (pFtpConnection)
{
TRACE("[Download Src]\t\t%s\n", pcRemoteFile);
TRACE("[Download Dst]\t\t%s\n", pcLocalFile);
bRet = pFtpConnection->GetFile(pcRemoteFile,pcLocalFile) ? true : false;//这是用来实现文件的传输的,成功则返回true,失败则返回false
if(bRet)
{
printf("传输文件成功\n");
}else
{
printf("传输文件失败\n");
}
}
}

if (pInetSession)
{
pInetSession->Close();
delete pInetSession;
}
if (pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
}
return bRet;
}


...全文
562 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
shaowutaojiang 2010-03-11
  • 打赏
  • 举报
回复
哪位能给我完整代码,我在加100
robin1115 2010-03-11
  • 打赏
  • 举报
回复
通过在控制台中绘制点(比如总的下载文件大小是一排白色的点),通过比较已经下载到的数据大小和总数据大小来使得点慢慢变色或许可以的···

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧