WinINeT进度条 [英] WinINeT Progress Bar

查看:119
本文介绍了WinINeT进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当程序从ftp源下载时,如何让开始时的进度条开始并缓慢增加.. ??????它变得疯狂就像填充了100万次......但是当它完成下载时就会停止.....





这里是我的代码

  void  CSimpleftpclientDlg :: OnButton4()
{


int index;
CString strText;
index = m_dir.GetCurSel();

m_dir.GetText(index,strText);



HINTERNET handle1 = FtpOpenFile(hIConnect,strText,GENERIC_READ,FTP_TRANSFER_TYPE_BINARY, 0 );
DWORD filesized = FtpGetFileSize(handle1,NULL);
CString csNumber;
csNumber.Format( %lu,文件化);

MessageBox(csNumber,csNumber,MB_OK);
HANDLE hFile = CreateFile(strText,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
char 缓冲区[ 4024 ];
DWORD dwRead = 0 ;





while (InternetReadFile(handle1,Buffer, sizeof (缓冲区),& dwRead)== TRUE)
{
如果(dwRead = = 0
break ;
DWORD dwWrite = 0 ;
progress2 + = dwRead;


m_yay.StepIt();

WriteFile(hFile,Buffer,dwRead,& dwWrite,NULL);
}





}







请帮助



添加代码块,删除SHOUTING - OriginalGriff [/ edit]

解决方案

使用PostMessage API。将用户定义的消息发布到窗口/对话框。在那里你处理它并设置值。



也许你应该以某种方式使用PostThreadMessage:



PostThreadMessage揭秘 [ ^ ]


bool m_signed = false; // peoplewant25jan@yahoo.com如果您需要帮助,欢迎

静态DWORD WINAPI DownThread(LPVOID pParameter)// CHTTPDownlogTestDlg

{

CDownloadZipDlg * pThis =(CDownloadZipDlg *)pParameter;

HINTERNET session = NULL;

HINTERNET connect = NULL;

HINTERNET http = NULL;

TCHAR url [MAX_PATH] = {0};

TCHAR服务器[MAX_PATH] = {0};

TCHAR文件[MAX_PATH] = {0 };

TCHAR名称[MAX_PATH] = _T(C:\\MyDownLog \\);

BYTE lpReadeBuffer [1024 * 8] = { 0};

TCHAR标题[8] = {0};

TCHAR lpLength [20] = _T(Îļþ'óС:);

DWORD dwTimeout = 15000;

DWORD dwReq = 0;

DWORD dwBufferLength = 500;

DWORD dwIndex = 0;

CString str;

TCHAR * p;



pThis-> GetDlgItemText(IDC_EDIT1,url,MAX_PATH);

if(_t cslen(url)< 7)

goto theEnd;

_tcsncpy(header,url,7);

if(_tcsicmp(header, _T(http://))!= 0)

转到结尾;

_tcscpy(服务器,网址+7);

p = _tcschr(服务器,'/');

if(NULL!= p)

{

* p ='\'0' ;

_tcscat(name,_tcsrchr(url,'/')+ 1);

}

_tcscpy(file,url + _tcslen(服务器)+ 8);



session = InternetOpen(_T(Mozilla / 4.0(兼容)),INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);

if(NULL == session)

{

AfxMessageBox(_T(''½»á»°Ê§°Ü));

转到结尾;

}

connect = InternetConnect(会话,服务器,80,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);

if(NULL == connect)

{

AfxMessageBox(_T(½¨Á¢¬¬½Óʧ°Ü));

转到theSession;

}

// CHAR * pAcceptTypes =* / * ;

http = HttpOpenRequest(连接,GET,文件,_T(HTTP / 1.1),NULL,/ *(LPCSTR *)&pAcceptTypes * / NULL,INTERNET_FLAG_RELOAD,0);

if(NULL == http)

{

AfxMessageBox(_T(ÇëÇó»á»°Ê§°Ü));

转到theConnect;

}

if(HttpSendRequest(http,NULL,0,NULL,0))

{

// dwBufferLength = 4; //// InternetGetLastResponseInfo

http://www.dreamincode.net/forums/topic/101532-download-file-from- url /

if(!HttpQueryInfo(http,HTTP_QUERY_CONTENT_LENGTH,// | HTTP_QUERY_FLAG_NUMBER,

&lpReadeBuffer,&dwBufferLength,NULL))//dwBufferLengthÒ»¶¨ÒªÓгõʼ'óС

{

AfxMessageBox(_T(» ñÈ¡Îļþ'óСʧ°Ü));

转到theHttp;

}

pThis-> m_progress.SetRange( 0,100);

DWORD allLength = atol((char *)lpReadeBuffer);

memset(header,0,sizeof(TCHAR)* 8);

_itot(allLength,header,10);

_tcscat(lpLength,header);

_tcscat(lpLength,Bit);

pThis-> SetDlgItemText(IDC_STATIC_L,lpLength);



FILE * ffile = fopen(name,wb +);

if(NULL == ffile)

{

AfxMessageBox(_T(''½'μʧ°Ü));

转到Http;

}

做{

InternetReadFile(http,lpReadeBuffer,1024 * 8,&dwBufferLength);

dwReq + = dwBufferLength;

if(dwBufferLength> 0)

fwrite(lpReadeBuffer,1,dwBufferLength,ffile);

int i =(int)((float)dwReq /(float)allLength *(float)100);

pThis-> m_progress.SetPos(i);

//睡眠(100);

} while(dwBufferLength> 0);

fclose(ffile);



}



str .Format(_T(ÏÂÔØÍê³É:%s),姓名);

AfxMessageBox(str);





theHttp:

InternetCloseHandle(http);

theConnect:

InternetCloseHandle(connect);

theSession :

InternetCloseHandle(会话);

theEnd:

pThis-> m_signed = false;

返回0 ;

}



无效CSimpleftpclientDlg :: OnButton1()

{

if(false == m_signed)

{

m_signed = true;

HANDLE handle = CreateThread(NULL,0,DownThread,(LPVOID)这,0,NULL);

CloseHandle(句柄);

}

其他

{

AfxMessageBox(_T(ÏÂÔؽø³ÌÕýÔÚÔËÐÐ));

}



}


< blockquote>我在谷歌搜索发现了这个问题

然后

它会如果有人以同样的方式来到这里帮助其他



无论如何谢谢你

im新代码项目


how can I get the progress bar to start at the beginning and slowy increment as the program downloads from the ftp source..?????? its going crazy like filling 1 million times..but it does stop when its done downloading.....


here is my code

void CSimpleftpclientDlg::OnButton4() 
{


int index;
         CString strText;
         index = m_dir.GetCurSel();

         m_dir.GetText(index,strText);
        

	
		 HINTERNET handle1 = FtpOpenFile(hIConnect,strText,GENERIC_READ,FTP_TRANSFER_TYPE_BINARY,0);
		DWORD filesized =  FtpGetFileSize(handle1,NULL);
		CString csNumber;
csNumber.Format("%lu", filesized);

MessageBox(csNumber,csNumber,MB_OK);
HANDLE hFile    = CreateFile(strText, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
char Buffer[4024];
  DWORD dwRead =0;
 
  



  while(InternetReadFile(handle1, Buffer, sizeof(Buffer), &dwRead) == TRUE)
  {
    if ( dwRead == 0) 
      break;
    DWORD dwWrite = 0;
	progress2 += dwRead;
	
	
	m_yay.StepIt();

    WriteFile(hFile, Buffer, dwRead, &dwWrite, NULL);
  }





}




please help

[edit]Code block added, SHOUTING removed - OriginalGriff[/edit]

解决方案

use the PostMessage API. Post a user defined message to your window/dialog. There you handle it and set the value.

Maybe you should use PostThreadMessage somehow like that:

PostThreadMessage Demystified[^]


bool m_signed =false; // peoplewant25jan@yahoo.com if you need help , welcome
static DWORD WINAPI DownThread(LPVOID pParameter)//CHTTPDownlogTestDlg
{
CDownloadZipDlg* pThis = (CDownloadZipDlg *)pParameter;
HINTERNET session = NULL;
HINTERNET connect = NULL;
HINTERNET http = NULL;
TCHAR url[MAX_PATH] = {0};
TCHAR server[MAX_PATH] = {0};
TCHAR file[MAX_PATH] = {0};
TCHAR name[MAX_PATH] = _T("C:\\MyDownLog\\");
BYTE lpReadeBuffer[1024 * 8] = {0};
TCHAR header[8] = {0};
TCHAR lpLength[20] = _T("Îļþ´óС:");
DWORD dwTimeout = 15000;
DWORD dwReq = 0;
DWORD dwBufferLength = 500;
DWORD dwIndex = 0;
CString str;
TCHAR *p;

pThis->GetDlgItemText(IDC_EDIT1, url, MAX_PATH);
if(_tcslen(url) < 7)
goto theEnd;
_tcsncpy(header, url, 7);
if(_tcsicmp(header, _T("http://")) != 0)
goto theEnd;
_tcscpy(server, url + 7);
p = _tcschr(server, '/');
if(NULL != p)
{
*p = '\0';
_tcscat(name, _tcsrchr(url, '/') + 1);
}
_tcscpy(file, url + _tcslen(server) + 8);

session = InternetOpen(_T("Mozilla/4.0 (compatible)"), INTERNET_OPEN_TYPE_DIRECT , NULL, NULL, 0);
if(NULL == session)
{
AfxMessageBox(_T("´´½¨»á»°Ê§°Ü"));
goto theEnd;
}
connect = InternetConnect(session, server, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if(NULL == connect)
{
AfxMessageBox(_T("½¨Á¢Á¬½Óʧ°Ü"));
goto theSession;
}
// CHAR* pAcceptTypes = "*/*";
http = HttpOpenRequest(connect, "GET", file, _T("HTTP/1.1"), NULL,/* (LPCSTR *)&pAcceptTypes*/NULL, INTERNET_FLAG_RELOAD, 0);
if(NULL == http)
{
AfxMessageBox(_T("ÇëÇó»á»°Ê§°Ü"));
goto theConnect;
}
if( HttpSendRequest(http, NULL, 0, NULL, 0) )
{
//dwBufferLength = 4;//// InternetGetLastResponseInfo
http://www.dreamincode.net/forums/topic/101532-download-file-from-url/
if( !HttpQueryInfo(http, HTTP_QUERY_CONTENT_LENGTH,// | HTTP_QUERY_FLAG_NUMBER,
&lpReadeBuffer, &dwBufferLength, NULL)) //dwBufferLengthÒ»¶¨ÒªÓгõʼ´óС
{
AfxMessageBox(_T("»ñÈ¡Îļþ´óСʧ°Ü"));
goto theHttp;
}
pThis->m_progress.SetRange(0, 100);
DWORD allLength = atol((char*)lpReadeBuffer);
memset(header, 0, sizeof(TCHAR) * 8);
_itot(allLength, header, 10);
_tcscat(lpLength, header);
_tcscat(lpLength, "Bit");
pThis->SetDlgItemText(IDC_STATIC_L, lpLength);

FILE *ffile = fopen(name, "wb+");
if(NULL == ffile)
{
AfxMessageBox(_T("´´½¨Îļþʧ°Ü"));
goto theHttp;
}
do{
InternetReadFile(http, lpReadeBuffer, 1024 * 8, &dwBufferLength);
dwReq += dwBufferLength;
if(dwBufferLength > 0)
fwrite(lpReadeBuffer, 1, dwBufferLength, ffile);
int i = (int)((float)dwReq / (float)allLength * (float)100);
pThis->m_progress.SetPos(i);
//Sleep(100);
}while(dwBufferLength > 0);
fclose(ffile);

}

str.Format(_T("ÏÂÔØÍê³É:%s"), name);
AfxMessageBox(str);


theHttp:
InternetCloseHandle(http);
theConnect:
InternetCloseHandle(connect);
theSession:
InternetCloseHandle(session);
theEnd:
pThis->m_signed = false;
return 0;
}

void CSimpleftpclientDlg::OnButton1()
{
if(false == m_signed)
{
m_signed = true;
HANDLE handle = CreateThread(NULL, 0, DownThread, (LPVOID)this, 0, NULL);
CloseHandle(handle);
}
else
{
AfxMessageBox(_T("ÏÂÔؽø³ÌÕýÔÚÔËÐÐ"));
}

}


i found this question by search in Google
then
it will help other if anybody came here by same way

anyway thank u
i m new to code project


这篇关于WinINeT进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆