完成下载功能

This commit is contained in:
Mike Solar
2023-09-18 21:35:41 +08:00
parent 7493ac4d2e
commit c0f45de00b
2 changed files with 78 additions and 3 deletions

View File

@ -9,9 +9,9 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<QtLastBackgroundBuild>2023-08-18T11:11:43.0072355Z</QtLastBackgroundBuild>
<QtLastBackgroundBuild>2023-09-18T12:55:53.6703998Z</QtLastBackgroundBuild>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<QtLastBackgroundBuild>2023-08-18T11:11:44.6602281Z</QtLastBackgroundBuild>
<QtLastBackgroundBuild>2023-09-18T12:55:55.7491129Z</QtLastBackgroundBuild>
</PropertyGroup>
</Project>

View File

@ -1,8 +1,82 @@
#include "qminiblink.h"
#include "qminiblink.h"
#include <functional>
#include <QThread>
#include <QDebug>
#include <thread>
#pragma comment (lib,"Comctl32.lib")
struct DownInfo {
std::string url;
size_t recvSize;
size_t allSize;
};
void MB_CALL_TYPE onNetJobDataRecvCallback(void* ptr, mbNetJob job, const char* data, int length)
{
DownInfo* info = (DownInfo*)ptr;
info->recvSize += length;
char* output = (char*)malloc(0x1000);
sprintf_s(output, 0x990, "onNetJobDataRecvCallback: %d %d, %f\n", info->allSize, info->recvSize, info->recvSize / (info->allSize + 1.0));
OutputDebugStringA(output);
free(output);
}
unsigned int __stdcall msgBoxThread(void* param)
{
std::function<void(void)>* callback = (std::function<void(void)>*)param;
(*callback)();
delete callback;
return 0;
}
void MB_CALL_TYPE onNetJobDataFinishCallback(void* ptr, mbNetJob job, mbLoadingResult result)
{
OutputDebugStringA("onNetJobDataFinishCallback\n");
std::wstring* title = new std::wstring;
*title += L" 下载完成:";
std::function<void(void)>* callback = new std::function<void(void)>([title, result] {
LPCWSTR lpCaption = (result == MB_LOADING_SUCCEEDED ? L"下载成功" : L"下载失败");
MessageBoxW(nullptr, title->c_str(), lpCaption, MB_OK);
delete title;
});
unsigned int threadId = 0; // 为了不卡blink线程messagbox放到另外个线程弹出
std::thread msgBox(msgBoxThread, callback);
msgBox.detach();
}
static mbDownloadOpt MB_CALL_TYPE onDownloadCallback(mbWebView webView,
void* param,
size_t expectedContentLength,
const char* url,
const char* mime,
const char* disposition,
mbNetJob job,
mbNetJobDataBind* dataBind)
{
char* output = (char*)malloc(0x8000);
sprintf_s(output, 0x7990, "onDownloadCallback: %d %s\n", expectedContentLength, url);
OutputDebugStringA(output);
free(output);
DownInfo* info = new DownInfo();
info->url = url;
info->recvSize = 0;
info->allSize = expectedContentLength;
mbDownloadBind bind = { 0 };
bind.param = info;
bind.recvCallback = onNetJobDataRecvCallback;
bind.finishCallback = onNetJobDataFinishCallback;
bind.saveNameCallback = nullptr;
return mbPopupDialogAndDownload(webView, nullptr, expectedContentLength, url, mime, disposition, job, dataBind, &bind);
//return mbDownloadByPath(webView, nullptr, L"P:\\", expectedContentLength, url, mime, disposition, job, dataBind, &bind);
}
QMiniBlink::QMiniBlink(QWidget *parent)
: QWidget(parent){
@ -26,6 +100,7 @@ void QMiniBlink::init() {
mbMoveWindow(this->mbView, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
//while(this->firstUrl.isEmpty()){}
//mbLoadURL(this->mbView, this->firstUrl.toStdString().c_str());
mbOnDownloadInBlinkThread(mbView,onDownloadCallback, nullptr);
SetWindowSubclass((HWND)this->winId(), subClassProc, 0, (DWORD_PTR)this);
//mbRunMessageLoop();
}