c++实现进度条
现在下载的程序已经做好,如何在下载的时候出现进度条。。求教
#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;
}
#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;
}
...全文
相关推荐
C++实现进度条[项目代码]
本文将详细介绍如何利用C++在终端中实现一个动态的进度条,包括其项目概述、实现思路、具体的代码实现及解读、运行效果展示,以及对进度条功能的扩展优化建议。
VC.Progress.Code.rar_C++ 进度条实现_progress_vc 进度条_进度条_进度条 vc
- "进度条源码" 文件可能包含了一个或多个C++源文件,这些文件演示了如何创建和使用`CProgressCtrl` 实例来实现进度条功能。
使用C++ QT实现的水波进度条源码
该项目使用C++和Qt框架实现了具有动态水波动画效果的进度条控件,支持圆形、矩形等多种显示类型,并利用QPainter进行界面绘制,结合信号槽机制完成事件响应与界面更新,适用于可视化液体填充场景。
bitmap_progress.zip_C++进度条背景_bitmap_progress_图形_进度条
在本案例中,背景图形可能是位图,通过编程方式加载到进度条组件上。5. **关于文件**: - **AboutDlg.h/cpp**:通常用于实现关于对话框的功能,其中包含了关于程序的信息展示。
Visual_C++利用互斥量同步线程实现文件读取进度条
### Visual C++ 利用互斥量同步线程实现文件读取进度条#### 一、背景介绍在开发基于Visual C++的应用程序时,文件读取操作常常伴随着进度显示的需求,以提升用户体验。


