简体   繁体   English

使用Windows API下载文件时如何创建进度条?

[英]How to create a progress bar while downloading a file using the windows API?

i'm working on an application in MS Visual C++ using Windows API that must download a file and place it in a folder. 我正在使用Windows API在MS Visual C ++中处理应用程序,该应用程序必须下载文件并将其放置在文件夹中。

I have already implemented the download using URLDownloadToFile function, but i want to create a PROGRESS_CLASS progress bar with marquee style while the file is being downloaded, but it doesn't seems to get animated in the process. 我已经使用URLDownloadToFile函数实现了下载,但是我想在下载文件时创建一个带有字幕样式的PROGRESS_CLASS进度条,但是在此过程中似乎没有动画效果。

This is the function I use for downloading: 这是我用来下载的功能:

BOOL SOXDownload()
{   
    HRESULT hRez = URLDownloadToFile(NULL,
        "url","C:\\sox.zip", 0, NULL);
    if (hRez == E_OUTOFMEMORY ) {
        MessageBox(hWnd, "Out of memory Error","", MB_OK);
        return FALSE;
    }
    if (hRez != S_OK) {
        MessageBox(hWnd, "Error downloading sox.", "Error!", MB_ICONERROR | MB_SYSTEMMODAL);
        return FALSE;
    }
    if (hRez == S_OK) {
        BSTR file = SysAllocString(L"C:\\sox.zip");
        BSTR folder = SysAllocString(L"C:\\");
        Unzip2Folder(file, folder);
        ::MessageBoxA(hWnd, "Sox Binaries downloaded succesfully", "Success", MB_OK);
    }
    return TRUE;
}

Later I call inside WM_CREATE (in my main window's message processor): 稍后,我在WM_CREATE内部调用(在主窗口的消息处理器中):

if (!fileExists("C:\\SOX\\SOX.exe")) {
    components[7] = CreateWindowEx(0, PROGRESS_CLASS,
                                NULL, WS_VISIBLE | PBS_MARQUEE,
                                GetSystemMetrics(SM_CXSCREEN) / 2 - 80,
                                GetSystemMetrics(SM_CYSCREEN) / 2 + 25,
                                200, 50, hWnd, NULL, NULL, NULL);
    SetWindowText(components[7], "Downloading SoX");
    SendMessage(components[7], PBM_SETRANGE, 0, (LPARAM) MAKELPARAM(0, 50));
    SendMessage(components[7], PBM_SETMARQUEE, TRUE, MAKELPARAM( 0, 50));
    SOXDownload();
    SendMessage(components[7], WM_CLOSE, NULL, NULL);
}

And as I want, I get a tiny progress bar... But it's not animated, and when I place the cursor over the bar, the cursor indicates that the program is busy downloading the file. 而且,根据需要,我得到了一个很小的进度条...但是它没有动画,当我将光标放在该条上时,光标指示该程序正在忙于下载文件。

When the download is complete, the window closes as i requested: SendMessage(components[7], WM_CLOSE, NULL, NULL); 下载完成后,窗口将按照我的请求关闭: SendMessage(components[7], WM_CLOSE, NULL, NULL);

So the question is how can I make the bar move while downloading the file? 因此,问题是下载文件时如何使栏移动? Considering that i want it done with marquee style for simplicity. 考虑到我希望它具有字幕风格以简化操作。

Thanks in advance. 提前致谢。

Create a class that implements the IBindStatusCallback interface and then pass it to the last parameter of URLDownloadToFile() . 创建一个实现IBindStatusCallback接口的类,然后将其传递给URLDownloadToFile()的最后一个参数。 You will then receive OnProgress events during the download, which you can use to update your UI as needed, pump the message queue for pending messages, etc. 然后,您将在下载过程中收到OnProgress事件,可用于根据需要更新UI,为待处理消息添加消息队列等。

I think I'd use InternetReadFile (or InternetReadFileEx ). 我想我会使用InternetReadFile (或InternetReadFileEx )。 This will let you read a small amount at a time (eg, 4 kilobytes) so you can update your status bar periodically (and handle any other messages as well). 这将使您一次读取少量内容(例如4 KB),因此您可以定期更新状态栏(并处理其他任何消息)。

To maximize UI responsiveness, you can do an asynchronous read. 为了最大程度地提高UI响应速度,您可以执行异步读取。 This will let you process messages immediately during the download instead of waiting for the next 4K (or whatever) block to finish. 这样一来,您就可以在下载过程中立即处理消息,而不必等待下一个4K(或任何其他大小)块完成。 Over a fast connection, it probably won't make a noticeable difference (4K doesn't normally take long) but over a slow or undependable connection, it could be a fairly major help. 通过快速连接,它可能不会产生明显的变化(4K通常不会花费很长时间),但是在速度较慢或不可靠的连接中,这可能是一个相当大的帮助。 Doing asynchronous downloading also gives you a chance to cleanly cancel the transaction if it takes too long. 如果执行时间太长,执行异步下载还可以使您有机会彻底取消事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM