OfficeAssistant_Win10/OfficeAssistant_msvc/qminiblink.cpp

139 lines
4.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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){
this->parent = parent;
//QSizePolicy sizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::QSizePolicy::Expanding);
//setSizePolicy(sizePolicy);
}
void QMiniBlink::init() {
ui.setupUi(this);
mbSettings settings;
memset(&settings, 0, sizeof(mbSettings));
mbInit(&settings);
RECT rect;
GetWindowRect((HWND)parent->winId(), &rect);
resize(rect.right - rect.left, rect.bottom - rect.top);
this->mbView = mbCreateWebWindow(MB_WINDOW_TYPE_CONTROL, (HWND)this->winId(), 0, 0, rect.right - rect.left, rect.bottom - rect.top);
mbSetNavigationToNewWindowEnable(this->mbView, false);
mbShowWindow(this->mbView, TRUE);
SetParent(mbGetHostHWND(this->mbView), (HWND)this->winId());
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();
}
QMiniBlink::~QMiniBlink(){
qDebug() << "Destroyed";
}
void QMiniBlink::ondestroy() {
mbDestroyWebView(mbView);
qDebug() << "Destroyed";
mbUninit();
}
void QMiniBlink::switchUrl(QString url) {
mbLoadURL(mbView, url.toStdString().c_str());
}
LRESULT CALLBACK QMiniBlink::subClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
QMiniBlink *obj = (QMiniBlink *)dwRefData;
if (obj == nullptr)
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
switch (uMsg) {
case WM_SIZE:
obj->autojustWebViewSize();
break;
default:
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
void QMiniBlink::autojustWebViewSize()
{
RECT rect;
GetWindowRect((HWND)this->winId(), &rect);
mbMoveWindow(mbView, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
}