struct REMFILE {
struct REMFILE *next;
int fsz; //文件大小
char *fnm; //文件名称
};
//发送进度信息给主窗口进行显示
static int log_progress(netbuf *ctl, int xfered, void *arg)
{
struct REMFILE *f = (struct REMFILE *) arg;
int pct = (int)(xfered*1.0/f->fsz*100);
HWND hWnd = (HWND)(AfxGetApp()->GetMainWnd()->GetSafeHwnd());
PostMessage(hWnd,WM_PROGRESS,pct,NULL);
return 1;
}
//FTP上传
bool MyFtp::up(const string& sourceFileName, const string& targetFileName)
{
int sts=0;
int fsz;
if (!connect())
return false;
FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);
struct REMFILE *f;
f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
memset(f,0,sizeof(struct REMFILE));
f->fnm = strdup(sourceFileName.c_str());
fsz = TPub::getFileSize(f->fnm);
f->fsz = fsz;
fsz = fsz/100;
if (fsz)
{
FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
FtpOptions(FTPLIB_CALLBACKARG, (long) f, conn);
FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);
}
sts = FtpPut(f->fnm,targetFileName.c_str(),'I',conn);
if (sts)
{
::PostMessage((HWND)(AfxGetApp()->GetMainWnd()->GetSafeHwnd()),WM_PROGRESS,100,NULL);
}
free(f->fnm);
free(f);
if (!sts)
{
_err = "ftp error: ";
_err = _err + FtpLastResponse(conn);
return false;
}
return true;
}
HWND hWnd = (HWND)(AfxGetApp()->GetMainWnd()->GetSafeHwnd());
PostMessage(hWnd,WM_PROGRESS,pct,NULL);
实现了获取主窗口句柄并发送消息给主窗口,主窗口接收到这个消息后进行运行进度的显示。代码如下:
LRESULT CFtpTestDlg::OnProgress(WPARAM wParam,LPARAM lParam)
{
UpdateData(TRUE);
int progress=(int)wParam;
string str = "";
str = str + TPub::intToString(progress) + "%";
m_percent = str.c_str();
UpdateData(FALSE);
return 0;
}
最后程序运行起来之后效果如下:
写的不对的地方请大家批评。