Update: drop Qt dependency, rewrite as pure Win32 using v142 toolset. Replace Update.ui/qrc and miniz-based Zip with Api/Crypto/Http/Json/Unzip/UpdateWindow modules. CrownClient/CrownServer: update Verify and main sources, retarget x64 configs to v142 (VS2019) with SDK 10.0.19041.0, fix VMProtect SDK paths.
722 lines
25 KiB
C++
722 lines
25 KiB
C++
#define NOMINMAX
|
|
|
|
#include "UpdateWindow.h"
|
|
|
|
#include "Config.h"
|
|
#include "Utils.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#include <windowsx.h>
|
|
|
|
#include <objbase.h>
|
|
#include <objidl.h>
|
|
#include <shlwapi.h>
|
|
#include <shobjidl.h>
|
|
|
|
#include <algorithm>
|
|
#include <string>
|
|
|
|
// GDI+ 头文件依赖未限定的 min/max
|
|
using std::min;
|
|
using std::max;
|
|
|
|
#include <gdiplus.h>
|
|
|
|
#pragma comment(lib, "gdiplus.lib")
|
|
#pragma comment(lib, "shlwapi.lib")
|
|
#pragma comment(lib, "ole32.lib")
|
|
|
|
namespace updater {
|
|
|
|
namespace {
|
|
|
|
const UINT kWmState = WM_APP + 1;
|
|
const UINT kTimerClose = 1;
|
|
|
|
const int kWidth = UPDATER_WINDOW_WIDTH;
|
|
const int kHeight = UPDATER_WINDOW_HEIGHT;
|
|
|
|
const int kCloseSize = 28;
|
|
const int kCloseMarginX = 14;
|
|
const int kCloseMarginY = 12;
|
|
|
|
// 更新进度面板
|
|
const int kPanelX = 48;
|
|
const int kPanelY = 300;
|
|
const int kPanelW = kWidth - kPanelX * 2;
|
|
const int kPanelH = 132;
|
|
|
|
const int kTextX = 72;
|
|
const int kTitleY = 316;
|
|
const int kStatusY = 356;
|
|
const int kBarX = 72;
|
|
const int kBarY = 388;
|
|
const int kBarW = kWidth - kBarX * 2;
|
|
const int kBarH = 12;
|
|
|
|
// 安装目录选择面板
|
|
const int kPickPanelX = 48;
|
|
const int kPickPanelY = 176;
|
|
const int kPickPanelW = kWidth - kPickPanelX * 2;
|
|
const int kPickPanelH = 248;
|
|
|
|
const int kPickTextX = 72;
|
|
const int kPickTitleY = 196;
|
|
const int kPickHintY = 234;
|
|
|
|
const int kPathX = 72;
|
|
const int kPathY = 268;
|
|
const int kPathW = 596;
|
|
const int kPathH = 38;
|
|
|
|
const int kBrowseX = kPathX + kPathW + 12;
|
|
const int kBrowseW = 116;
|
|
|
|
const int kSpaceY = 318;
|
|
|
|
const int kButtonY = 356;
|
|
const int kButtonH = 44;
|
|
const int kInstallW = 148;
|
|
const int kInstallX = kPickPanelX + kPickPanelW - kInstallW;
|
|
const int kCancelW = 108;
|
|
const int kCancelX = kInstallX - 12 - kCancelW;
|
|
|
|
const wchar_t* const kFontName = L"Microsoft YaHei";
|
|
|
|
void AddRoundRect(Gdiplus::GraphicsPath& path, int x, int y, int w, int h, int r) {
|
|
int d = r * 2;
|
|
path.AddArc(x, y, d, d, 180, 90);
|
|
path.AddArc(x + w - d, y, d, d, 270, 90);
|
|
path.AddArc(x + w - d, y + h - d, d, d, 0, 90);
|
|
path.AddArc(x, y + h - d, d, d, 90, 90);
|
|
path.CloseFigure();
|
|
}
|
|
|
|
void DrawButton(Gdiplus::Graphics& graphics, int x, int y, int w, int h, const wchar_t* text,
|
|
bool primary, int state) {
|
|
Gdiplus::GraphicsPath path;
|
|
AddRoundRect(path, x, y, w, h, 8);
|
|
|
|
Gdiplus::Color top;
|
|
Gdiplus::Color bottom;
|
|
if (primary) {
|
|
if (state == 2) {
|
|
top = Gdiplus::Color(255, 30, 104, 182);
|
|
bottom = Gdiplus::Color(255, 22, 78, 142);
|
|
} else if (state == 1) {
|
|
top = Gdiplus::Color(255, 74, 182, 250);
|
|
bottom = Gdiplus::Color(255, 42, 136, 212);
|
|
} else {
|
|
top = Gdiplus::Color(255, 58, 160, 238);
|
|
bottom = Gdiplus::Color(255, 32, 114, 192);
|
|
}
|
|
} else {
|
|
if (state == 2) {
|
|
top = Gdiplus::Color(255, 54, 62, 78);
|
|
bottom = Gdiplus::Color(255, 40, 46, 60);
|
|
} else if (state == 1) {
|
|
top = Gdiplus::Color(255, 82, 92, 110);
|
|
bottom = Gdiplus::Color(255, 60, 68, 86);
|
|
} else {
|
|
top = Gdiplus::Color(255, 68, 76, 94);
|
|
bottom = Gdiplus::Color(255, 50, 56, 70);
|
|
}
|
|
}
|
|
|
|
Gdiplus::LinearGradientBrush brush(Gdiplus::Point(x, y), Gdiplus::Point(x, y + h), top,
|
|
bottom);
|
|
graphics.FillPath(&brush, &path);
|
|
|
|
Gdiplus::StringFormat format;
|
|
format.SetAlignment(Gdiplus::StringAlignmentCenter);
|
|
format.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
|
Gdiplus::Font font(kFontName, primary ? 15.0f : 14.0f, Gdiplus::FontStyleBold,
|
|
Gdiplus::UnitPixel);
|
|
Gdiplus::SolidBrush textBrush(Gdiplus::Color(255, 245, 248, 252));
|
|
Gdiplus::RectF rect(static_cast<float>(x), static_cast<float>(y), static_cast<float>(w),
|
|
static_cast<float>(h));
|
|
graphics.DrawString(text, -1, &font, rect, &format, &textBrush);
|
|
}
|
|
|
|
Gdiplus::Image* LoadImageFromResource(HINSTANCE instance, int resourceId) {
|
|
HRSRC found = FindResourceW(instance, MAKEINTRESOURCEW(resourceId), RT_RCDATA);
|
|
if (!found) return nullptr;
|
|
HGLOBAL loaded = LoadResource(instance, found);
|
|
if (!loaded) return nullptr;
|
|
void* data = LockResource(loaded);
|
|
DWORD size = SizeofResource(instance, found);
|
|
if (!data || size == 0) return nullptr;
|
|
|
|
IStream* stream = SHCreateMemStream(static_cast<const BYTE*>(data), size);
|
|
if (!stream) return nullptr;
|
|
Gdiplus::Image* image = Gdiplus::Image::FromStream(stream, FALSE);
|
|
stream->Release();
|
|
return image;
|
|
}
|
|
|
|
Gdiplus::Image* LoadImageFromFile(const std::wstring& path) {
|
|
if (path.empty() || !Utils::FileExists(path)) return nullptr;
|
|
return Gdiplus::Image::FromFile(path.c_str(), FALSE);
|
|
}
|
|
|
|
std::wstring PercentText(int percent) {
|
|
return std::to_wstring(percent) + L"%";
|
|
}
|
|
|
|
// 完整包安装时的默认目录:更新器由主程序从自身目录启动,默认就装到主程序所在目录
|
|
std::wstring DefaultInstallDir() {
|
|
std::wstring custom = UPDATER_DEFAULT_INSTALL_DIR;
|
|
if (!custom.empty()) return custom;
|
|
return Utils::GetModuleDir();
|
|
}
|
|
|
|
// 目录可能还不存在,向上回溯到存在的祖先再取可用空间
|
|
bool QueryFreeSpace(const std::wstring& path, unsigned long long& freeBytes) {
|
|
std::wstring probe = path;
|
|
while (!probe.empty() && !Utils::DirExists(probe)) {
|
|
std::wstring parent = Utils::GetParentDir(probe);
|
|
if (parent.empty() || parent == probe) break;
|
|
probe = parent;
|
|
}
|
|
if (probe.empty()) return false;
|
|
|
|
ULARGE_INTEGER available;
|
|
if (!GetDiskFreeSpaceExW(probe.c_str(), &available, nullptr, nullptr)) return false;
|
|
freeBytes = available.QuadPart;
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
struct UpdateWindow::RenderContext {
|
|
ULONG_PTR token = 0;
|
|
Gdiplus::Image* background = nullptr;
|
|
Gdiplus::Image* closeImages[3] = {nullptr, nullptr, nullptr};
|
|
};
|
|
|
|
UpdateWindow::UpdateWindow() : render_(new RenderContext()) {
|
|
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
|
|
Gdiplus::GdiplusStartupInput input;
|
|
Gdiplus::GdiplusStartup(&render_->token, &input, nullptr);
|
|
}
|
|
|
|
UpdateWindow::~UpdateWindow() {
|
|
if (render_) {
|
|
delete render_->background;
|
|
for (int i = 0; i < 3; ++i) delete render_->closeImages[i];
|
|
if (render_->token) Gdiplus::GdiplusShutdown(render_->token);
|
|
delete render_;
|
|
render_ = nullptr;
|
|
}
|
|
CoUninitialize();
|
|
}
|
|
|
|
bool UpdateWindow::Create(HINSTANCE instance, const UpdateOptions& options) {
|
|
instance_ = instance;
|
|
options_ = options;
|
|
|
|
render_->background = LoadImageFromResource(instance, 101);
|
|
if (!render_->background) {
|
|
render_->background = LoadImageFromFile(Utils::JoinPath(Utils::GetModuleDir(), L"res\\bg.gif"));
|
|
}
|
|
render_->closeImages[0] = LoadImageFromResource(instance, 102);
|
|
render_->closeImages[1] = LoadImageFromResource(instance, 103);
|
|
render_->closeImages[2] = LoadImageFromResource(instance, 104);
|
|
|
|
WNDCLASSW wc;
|
|
ZeroMemory(&wc, sizeof(wc));
|
|
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DROPSHADOW;
|
|
wc.lpfnWndProc = &UpdateWindow::WndProc;
|
|
wc.hInstance = instance;
|
|
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
|
wc.hbrBackground = nullptr;
|
|
wc.lpszClassName = L"VerifyUpdaterWindowClass";
|
|
RegisterClassW(&wc);
|
|
|
|
int screenX = GetSystemMetrics(SM_CXSCREEN);
|
|
int screenY = GetSystemMetrics(SM_CYSCREEN);
|
|
int left = (screenX - kWidth) / 2;
|
|
int top = (screenY - kHeight) / 2;
|
|
|
|
hwnd_ = CreateWindowExW(WS_EX_TOPMOST | WS_EX_TOOLWINDOW, wc.lpszClassName, UPDATER_TITLE,
|
|
WS_POPUP, left, top, kWidth, kHeight, nullptr, nullptr, instance,
|
|
this);
|
|
if (!hwnd_) return false;
|
|
|
|
HRGN region = CreateRoundRectRgn(0, 0, kWidth + 1, kHeight + 1, 12, 12);
|
|
SetWindowRgn(hwnd_, region, FALSE);
|
|
|
|
// 完整包安装且没有指定目录时,先让用户选安装目录
|
|
// 必须在首次显示之前定好阶段,否则首帧会画成更新进度面板
|
|
bool pickDir = options.fullMode && options.targetDir.empty();
|
|
if (pickDir) {
|
|
phase_ = Phase::PickDir;
|
|
installDir_ = DefaultInstallDir();
|
|
status_ = L"请选择安装目录";
|
|
} else {
|
|
phase_ = Phase::Updating;
|
|
status_ = options.fullMode ? L"正在准备完整包更新..." : L"正在检查版本更新...";
|
|
}
|
|
|
|
ShowWindow(hwnd_, SW_SHOW);
|
|
::UpdateWindow(hwnd_);
|
|
|
|
if (!pickDir) StartWorker(options);
|
|
return true;
|
|
}
|
|
|
|
void UpdateWindow::StartWorker(const UpdateOptions& options) {
|
|
HWND hwnd = hwnd_;
|
|
worker_.Start(options,
|
|
[this, hwnd](int percent, const std::wstring& text) {
|
|
UiState* state = new UiState();
|
|
state->percent = percent;
|
|
state->status = text;
|
|
PostMessageW(hwnd, kWmState, 0, reinterpret_cast<LPARAM>(state));
|
|
},
|
|
[this, hwnd](bool ok, const std::wstring& text) {
|
|
UiState* state = new UiState();
|
|
state->percent = ok ? 100 : percent_;
|
|
state->status = text;
|
|
state->finished = true;
|
|
state->success = ok;
|
|
PostMessageW(hwnd, kWmState, 0, reinterpret_cast<LPARAM>(state));
|
|
});
|
|
}
|
|
|
|
bool UpdateWindow::BrowseFolder() {
|
|
IFileDialog* dialog = nullptr;
|
|
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER,
|
|
IID_PPV_ARGS(&dialog));
|
|
if (FAILED(hr)) return false;
|
|
|
|
DWORD options = 0;
|
|
if (SUCCEEDED(dialog->GetOptions(&options))) {
|
|
dialog->SetOptions(options | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST);
|
|
}
|
|
dialog->SetTitle(L"选择安装目录");
|
|
|
|
if (!installDir_.empty() && Utils::DirExists(installDir_)) {
|
|
IShellItem* folder = nullptr;
|
|
if (SUCCEEDED(SHCreateItemFromParsingName(installDir_.c_str(), nullptr,
|
|
IID_PPV_ARGS(&folder)))) {
|
|
dialog->SetFolder(folder);
|
|
folder->Release();
|
|
}
|
|
}
|
|
|
|
bool changed = false;
|
|
if (SUCCEEDED(dialog->Show(hwnd_))) {
|
|
IShellItem* item = nullptr;
|
|
if (SUCCEEDED(dialog->GetResult(&item))) {
|
|
wchar_t* path = nullptr;
|
|
if (SUCCEEDED(item->GetDisplayName(SIGDN_FILESYSPATH, &path)) && path) {
|
|
installDir_ = path;
|
|
changed = true;
|
|
CoTaskMemFree(path);
|
|
}
|
|
item->Release();
|
|
}
|
|
}
|
|
dialog->Release();
|
|
return changed;
|
|
}
|
|
|
|
void UpdateWindow::StartUpdate() {
|
|
if (started_ || installDir_.empty()) return;
|
|
started_ = true;
|
|
|
|
phase_ = Phase::Updating;
|
|
closeState_ = browseState_ = installState_ = cancelState_ = 0;
|
|
pressed_ = 0;
|
|
|
|
UpdateOptions options = options_;
|
|
options.targetDir = installDir_;
|
|
status_ = L"正在准备安装...";
|
|
Invalidate();
|
|
StartWorker(options);
|
|
}
|
|
|
|
int UpdateWindow::MessageLoop() {
|
|
MSG msg;
|
|
while (GetMessageW(&msg, nullptr, 0, 0) > 0) {
|
|
TranslateMessage(&msg);
|
|
DispatchMessageW(&msg);
|
|
}
|
|
return static_cast<int>(msg.wParam);
|
|
}
|
|
|
|
void UpdateWindow::Invalidate() {
|
|
if (hwnd_) InvalidateRect(hwnd_, nullptr, FALSE);
|
|
}
|
|
|
|
bool UpdateWindow::HitCloseButton(int x, int y) const {
|
|
int left = kWidth - kCloseMarginX - kCloseSize;
|
|
return x >= left && x <= left + kCloseSize && y >= kCloseMarginY &&
|
|
y <= kCloseMarginY + kCloseSize;
|
|
}
|
|
|
|
bool UpdateWindow::HitBrowseButton(int x, int y) const {
|
|
return phase_ == Phase::PickDir && x >= kBrowseX && x <= kBrowseX + kBrowseW && y >= kPathY &&
|
|
y <= kPathY + kPathH;
|
|
}
|
|
|
|
bool UpdateWindow::HitInstallButton(int x, int y) const {
|
|
return phase_ == Phase::PickDir && x >= kInstallX && x <= kInstallX + kInstallW &&
|
|
y >= kButtonY && y <= kButtonY + kButtonH;
|
|
}
|
|
|
|
bool UpdateWindow::HitCancelButton(int x, int y) const {
|
|
return phase_ == Phase::PickDir && x >= kCancelX && x <= kCancelX + kCancelW &&
|
|
y >= kButtonY && y <= kButtonY + kButtonH;
|
|
}
|
|
|
|
bool UpdateWindow::HitAnyButton(int x, int y) const {
|
|
return HitCloseButton(x, y) || HitBrowseButton(x, y) || HitInstallButton(x, y) ||
|
|
HitCancelButton(x, y);
|
|
}
|
|
|
|
void UpdateWindow::UpdateHover(int x, int y) {
|
|
int close = HitCloseButton(x, y) ? (pressed_ == 1 ? 2 : 1) : 0;
|
|
int browse = HitBrowseButton(x, y) ? (pressed_ == 2 ? 2 : 1) : 0;
|
|
int install = HitInstallButton(x, y) ? (pressed_ == 3 ? 2 : 1) : 0;
|
|
int cancel = HitCancelButton(x, y) ? (pressed_ == 4 ? 2 : 1) : 0;
|
|
|
|
if (close != closeState_ || browse != browseState_ || install != installState_ ||
|
|
cancel != cancelState_) {
|
|
closeState_ = close;
|
|
browseState_ = browse;
|
|
installState_ = install;
|
|
cancelState_ = cancel;
|
|
Invalidate();
|
|
}
|
|
}
|
|
|
|
void UpdateWindow::RequestClose() {
|
|
if (closing_) return;
|
|
closing_ = true;
|
|
if (phase_ == Phase::Updating && !finished_) {
|
|
status_ = L"正在取消更新...";
|
|
Invalidate();
|
|
}
|
|
worker_.Cancel();
|
|
DestroyWindow(hwnd_);
|
|
}
|
|
|
|
void UpdateWindow::OnState(const UiState& state) {
|
|
if (closing_) return;
|
|
percent_ = std::max(0, std::min(100, state.percent));
|
|
status_ = state.status;
|
|
if (state.finished) {
|
|
finished_ = true;
|
|
success_ = state.success;
|
|
if (success_) SetTimer(hwnd_, kTimerClose, 1500, nullptr);
|
|
}
|
|
Invalidate();
|
|
}
|
|
|
|
void UpdateWindow::DrawPickPanel(Gdiplus::Graphics& graphics) {
|
|
Gdiplus::GraphicsPath panelPath;
|
|
AddRoundRect(panelPath, kPickPanelX, kPickPanelY, kPickPanelW, kPickPanelH, 12);
|
|
Gdiplus::SolidBrush panelBrush(Gdiplus::Color(165, 12, 16, 24));
|
|
graphics.FillPath(&panelBrush, &panelPath);
|
|
|
|
Gdiplus::StringFormat nearFormat;
|
|
nearFormat.SetAlignment(Gdiplus::StringAlignmentNear);
|
|
nearFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
|
nearFormat.SetTrimming(Gdiplus::StringTrimmingEllipsisWord);
|
|
|
|
Gdiplus::SolidBrush textBrush(Gdiplus::Color(255, 240, 244, 250));
|
|
Gdiplus::SolidBrush dimBrush(Gdiplus::Color(200, 176, 186, 202));
|
|
|
|
Gdiplus::Font titleFont(kFontName, 18.0f, Gdiplus::FontStyleBold, Gdiplus::UnitPixel);
|
|
Gdiplus::RectF titleRect(static_cast<float>(kPickTextX), static_cast<float>(kPickTitleY),
|
|
static_cast<float>(kPickPanelW - 48), 28.0f);
|
|
std::wstring appName = UPDATER_APP_NAME;
|
|
std::wstring titleText =
|
|
appName.empty() ? L"选择安装目录" : (L"选择 " + appName + L" 的安装目录");
|
|
graphics.DrawString(titleText.c_str(), -1, &titleFont, titleRect, &nearFormat, &textBrush);
|
|
|
|
Gdiplus::Font hintFont(kFontName, 13.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
|
Gdiplus::RectF hintRect(static_cast<float>(kPickTextX), static_cast<float>(kPickHintY),
|
|
static_cast<float>(kPickPanelW - 48), 22.0f);
|
|
graphics.DrawString(L"请选择程序的安装位置,目录下的同名文件会被覆盖", -1, &hintFont, hintRect,
|
|
&nearFormat, &dimBrush);
|
|
|
|
// 路径显示框
|
|
Gdiplus::GraphicsPath boxPath;
|
|
AddRoundRect(boxPath, kPathX, kPathY, kPathW, kPathH, 8);
|
|
Gdiplus::SolidBrush boxBrush(Gdiplus::Color(130, 8, 10, 16));
|
|
graphics.FillPath(&boxBrush, &boxPath);
|
|
Gdiplus::Pen boxPen(Gdiplus::Color(190, 98, 110, 130), 1.0f);
|
|
graphics.DrawPath(&boxPen, &boxPath);
|
|
|
|
Gdiplus::StringFormat pathFormat;
|
|
pathFormat.SetAlignment(Gdiplus::StringAlignmentNear);
|
|
pathFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
|
pathFormat.SetTrimming(Gdiplus::StringTrimmingEllipsisPath);
|
|
Gdiplus::Font pathFont(kFontName, 14.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
|
Gdiplus::RectF pathRect(static_cast<float>(kPathX + 12), static_cast<float>(kPathY),
|
|
static_cast<float>(kPathW - 24), static_cast<float>(kPathH));
|
|
graphics.DrawString(installDir_.c_str(), -1, &pathFont, pathRect, &pathFormat, &textBrush);
|
|
|
|
DrawButton(graphics, kBrowseX, kPathY, kBrowseW, kPathH, L"浏览...", false, browseState_);
|
|
|
|
// 可用空间与目录状态
|
|
std::wstring spaceText;
|
|
unsigned long long freeBytes = 0;
|
|
if (QueryFreeSpace(installDir_, freeBytes)) {
|
|
spaceText = L"可用空间 " + Utils::FormatSize(freeBytes);
|
|
}
|
|
if (Utils::DirExists(installDir_)) {
|
|
if (!spaceText.empty()) spaceText += L" ";
|
|
spaceText += L"目录已存在,将覆盖其中的同名文件";
|
|
}
|
|
if (!spaceText.empty()) {
|
|
Gdiplus::RectF spaceRect(static_cast<float>(kPickTextX), static_cast<float>(kSpaceY),
|
|
static_cast<float>(kPickPanelW - 48), 22.0f);
|
|
graphics.DrawString(spaceText.c_str(), -1, &hintFont, spaceRect, &nearFormat, &dimBrush);
|
|
}
|
|
|
|
DrawButton(graphics, kCancelX, kButtonY, kCancelW, kButtonH, L"取消", false, cancelState_);
|
|
DrawButton(graphics, kInstallX, kButtonY, kInstallW, kButtonH, L"开始安装", true,
|
|
installState_);
|
|
}
|
|
|
|
void UpdateWindow::DrawProgressPanel(Gdiplus::Graphics& graphics) {
|
|
Gdiplus::GraphicsPath panelPath;
|
|
AddRoundRect(panelPath, kPanelX, kPanelY, kPanelW, kPanelH, 12);
|
|
Gdiplus::SolidBrush panelBrush(Gdiplus::Color(150, 12, 16, 24));
|
|
graphics.FillPath(&panelBrush, &panelPath);
|
|
|
|
Gdiplus::SolidBrush textBrush(Gdiplus::Color(255, 240, 244, 250));
|
|
Gdiplus::SolidBrush dimBrush(Gdiplus::Color(200, 176, 186, 202));
|
|
|
|
Gdiplus::StringFormat nearFormat;
|
|
nearFormat.SetAlignment(Gdiplus::StringAlignmentNear);
|
|
nearFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
|
nearFormat.SetTrimming(Gdiplus::StringTrimmingEllipsisWord);
|
|
|
|
Gdiplus::Font titleFont(kFontName, 18.0f, Gdiplus::FontStyleBold, Gdiplus::UnitPixel);
|
|
Gdiplus::RectF titleRect(static_cast<float>(kTextX), static_cast<float>(kTitleY),
|
|
static_cast<float>(kPanelW - 48), 28.0f);
|
|
graphics.DrawString(UPDATER_TITLE, -1, &titleFont, titleRect, &nearFormat, &textBrush);
|
|
|
|
Gdiplus::Font statusFont(kFontName, 14.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
|
Gdiplus::RectF statusRect(static_cast<float>(kTextX), static_cast<float>(kStatusY),
|
|
static_cast<float>(kBarW - 90), 24.0f);
|
|
graphics.DrawString(status_.c_str(), -1, &statusFont, statusRect, &nearFormat, &dimBrush);
|
|
|
|
Gdiplus::StringFormat farFormat;
|
|
farFormat.SetAlignment(Gdiplus::StringAlignmentFar);
|
|
farFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
|
Gdiplus::Font percentFont(kFontName, 15.0f, Gdiplus::FontStyleBold, Gdiplus::UnitPixel);
|
|
Gdiplus::RectF percentRect(static_cast<float>(kBarX + kBarW - 80),
|
|
static_cast<float>(kStatusY), 80.0f, 24.0f);
|
|
graphics.DrawString(PercentText(percent_).c_str(), -1, &percentFont, percentRect,
|
|
&farFormat, &textBrush);
|
|
|
|
Gdiplus::GraphicsPath trackPath;
|
|
AddRoundRect(trackPath, kBarX, kBarY, kBarW, kBarH, kBarH / 2);
|
|
Gdiplus::SolidBrush trackBrush(Gdiplus::Color(200, 48, 54, 68));
|
|
graphics.FillPath(&trackBrush, &trackPath);
|
|
|
|
int fillW = static_cast<int>(static_cast<double>(kBarW) * percent_ / 100.0);
|
|
if (fillW > kBarH) {
|
|
Gdiplus::GraphicsPath fillPath;
|
|
AddRoundRect(fillPath, kBarX, kBarY, fillW, kBarH, kBarH / 2);
|
|
|
|
bool failed = finished_ && !success_;
|
|
Gdiplus::Color c1 = failed ? Gdiplus::Color(255, 226, 92, 92)
|
|
: Gdiplus::Color(255, 88, 196, 248);
|
|
Gdiplus::Color c2 = failed ? Gdiplus::Color(255, 198, 58, 72)
|
|
: Gdiplus::Color(255, 46, 140, 226);
|
|
Gdiplus::LinearGradientBrush fillBrush(
|
|
Gdiplus::Point(kBarX, kBarY), Gdiplus::Point(kBarX + fillW, kBarY + kBarH), c1, c2);
|
|
graphics.FillPath(&fillBrush, &fillPath);
|
|
}
|
|
}
|
|
|
|
void UpdateWindow::OnPaint() {
|
|
PAINTSTRUCT ps;
|
|
HDC hdc = BeginPaint(hwnd_, &ps);
|
|
|
|
RECT rc;
|
|
GetClientRect(hwnd_, &rc);
|
|
int width = rc.right - rc.left;
|
|
int height = rc.bottom - rc.top;
|
|
|
|
HDC memDc = CreateCompatibleDC(hdc);
|
|
HBITMAP memBmp = CreateCompatibleBitmap(hdc, width, height);
|
|
HGDIOBJ old = SelectObject(memDc, memBmp);
|
|
|
|
{
|
|
Gdiplus::Graphics graphics(memDc);
|
|
graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
|
|
graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAliasGridFit);
|
|
graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
|
|
|
|
if (render_->background && render_->background->GetLastStatus() == Gdiplus::Ok) {
|
|
graphics.DrawImage(render_->background, 0, 0, width, height);
|
|
} else {
|
|
Gdiplus::LinearGradientBrush bgBrush(
|
|
Gdiplus::Point(0, 0), Gdiplus::Point(0, height),
|
|
Gdiplus::Color(255, 32, 38, 52), Gdiplus::Color(255, 18, 22, 32));
|
|
graphics.FillRectangle(&bgBrush, 0, 0, width, height);
|
|
}
|
|
|
|
if (phase_ == Phase::PickDir) {
|
|
DrawPickPanel(graphics);
|
|
} else {
|
|
DrawProgressPanel(graphics);
|
|
}
|
|
|
|
int closeLeft = kWidth - kCloseMarginX - kCloseSize;
|
|
Gdiplus::Image* closeImage = render_->closeImages[closeState_];
|
|
if (closeImage && closeImage->GetLastStatus() == Gdiplus::Ok) {
|
|
graphics.DrawImage(closeImage, closeLeft, kCloseMarginY, kCloseSize, kCloseSize);
|
|
} else {
|
|
Gdiplus::Color penColor(220, 235, 240, 248);
|
|
Gdiplus::Pen pen(penColor, 2.0f);
|
|
graphics.DrawLine(&pen, closeLeft + 8, kCloseMarginY + 8, closeLeft + kCloseSize - 8,
|
|
kCloseMarginY + kCloseSize - 8);
|
|
graphics.DrawLine(&pen, closeLeft + kCloseSize - 8, kCloseMarginY + 8, closeLeft + 8,
|
|
kCloseMarginY + kCloseSize - 8);
|
|
}
|
|
}
|
|
|
|
BitBlt(hdc, 0, 0, width, height, memDc, 0, 0, SRCCOPY);
|
|
SelectObject(memDc, old);
|
|
DeleteObject(memBmp);
|
|
DeleteDC(memDc);
|
|
EndPaint(hwnd_, &ps);
|
|
}
|
|
|
|
LRESULT UpdateWindow::HandleMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
switch (msg) {
|
|
case WM_PAINT:
|
|
OnPaint();
|
|
return 0;
|
|
|
|
case WM_ERASEBKGND:
|
|
return 1;
|
|
|
|
case kWmState: {
|
|
UiState* state = reinterpret_cast<UiState*>(lParam);
|
|
if (state) {
|
|
OnState(*state);
|
|
delete state;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
case WM_NCHITTEST: {
|
|
POINT pt;
|
|
pt.x = GET_X_LPARAM(lParam);
|
|
pt.y = GET_Y_LPARAM(lParam);
|
|
ScreenToClient(hwnd_, &pt);
|
|
// 按钮区域交给客户区消息处理,其余部分允许拖动窗口
|
|
if (HitAnyButton(pt.x, pt.y)) return HTCLIENT;
|
|
return HTCAPTION;
|
|
}
|
|
|
|
case WM_MOUSEMOVE: {
|
|
int x = GET_X_LPARAM(lParam);
|
|
int y = GET_Y_LPARAM(lParam);
|
|
UpdateHover(x, y);
|
|
if (!tracking_) {
|
|
TRACKMOUSEEVENT tme;
|
|
ZeroMemory(&tme, sizeof(tme));
|
|
tme.cbSize = sizeof(tme);
|
|
tme.dwFlags = TME_LEAVE;
|
|
tme.hwndTrack = hwnd_;
|
|
TrackMouseEvent(&tme);
|
|
tracking_ = true;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
case WM_MOUSELEAVE:
|
|
tracking_ = false;
|
|
pressed_ = 0;
|
|
if (closeState_ != 0 || browseState_ != 0 || installState_ != 0 || cancelState_ != 0) {
|
|
closeState_ = browseState_ = installState_ = cancelState_ = 0;
|
|
Invalidate();
|
|
}
|
|
return 0;
|
|
|
|
case WM_LBUTTONDOWN: {
|
|
int x = GET_X_LPARAM(lParam);
|
|
int y = GET_Y_LPARAM(lParam);
|
|
if (HitCloseButton(x, y)) pressed_ = 1;
|
|
else if (HitBrowseButton(x, y)) pressed_ = 2;
|
|
else if (HitInstallButton(x, y)) pressed_ = 3;
|
|
else if (HitCancelButton(x, y)) pressed_ = 4;
|
|
else pressed_ = 0;
|
|
UpdateHover(x, y);
|
|
return 0;
|
|
}
|
|
|
|
case WM_LBUTTONUP: {
|
|
int x = GET_X_LPARAM(lParam);
|
|
int y = GET_Y_LPARAM(lParam);
|
|
int hit = pressed_;
|
|
pressed_ = 0;
|
|
UpdateHover(x, y);
|
|
|
|
if (hit == 1 && HitCloseButton(x, y)) {
|
|
RequestClose();
|
|
} else if (hit == 2 && HitBrowseButton(x, y)) {
|
|
if (BrowseFolder()) Invalidate();
|
|
} else if (hit == 3 && HitInstallButton(x, y)) {
|
|
StartUpdate();
|
|
} else if (hit == 4 && HitCancelButton(x, y)) {
|
|
RequestClose();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
case WM_TIMER:
|
|
if (wParam == kTimerClose) {
|
|
KillTimer(hwnd_, kTimerClose);
|
|
closing_ = true;
|
|
DestroyWindow(hwnd_);
|
|
}
|
|
return 0;
|
|
|
|
case WM_KEYDOWN:
|
|
if (wParam == VK_ESCAPE) RequestClose();
|
|
if (phase_ == Phase::PickDir && wParam == VK_RETURN) StartUpdate();
|
|
return 0;
|
|
|
|
case WM_CLOSE:
|
|
RequestClose();
|
|
return 0;
|
|
|
|
case WM_DESTROY:
|
|
PostQuitMessage(0);
|
|
return 0;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return DefWindowProcW(hwnd_, msg, wParam, lParam);
|
|
}
|
|
|
|
LRESULT CALLBACK UpdateWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
UpdateWindow* self = nullptr;
|
|
if (msg == WM_NCCREATE) {
|
|
CREATESTRUCTW* cs = reinterpret_cast<CREATESTRUCTW*>(lParam);
|
|
self = static_cast<UpdateWindow*>(cs->lpCreateParams);
|
|
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(self));
|
|
if (self) self->hwnd_ = hwnd;
|
|
} else {
|
|
self = reinterpret_cast<UpdateWindow*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
|
|
}
|
|
if (self) return self->HandleMessage(msg, wParam, lParam);
|
|
return DefWindowProcW(hwnd, msg, wParam, lParam);
|
|
}
|
|
|
|
}
|