更新器改为 VS2019 暗色风格,窗口改分层绘制去掉圆角毛边
This commit is contained in:
+340
-205
@@ -1,4 +1,4 @@
|
||||
#define NOMINMAX
|
||||
#define NOMINMAX
|
||||
|
||||
#include "UpdateWindow.h"
|
||||
|
||||
@@ -37,127 +37,123 @@ 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;
|
||||
// VS 2019 是直角窗口,窗口和控件都不做圆角
|
||||
const int kTitleH = 32; // 标题栏高度
|
||||
const int kCloseSize = 32; // 关闭按钮是标题栏右侧的正方形
|
||||
const int kCloseMarginX = 0;
|
||||
const int kCloseMarginY = 0;
|
||||
|
||||
// 更新进度面板
|
||||
const int kPanelX = 48;
|
||||
const int kPanelY = 300;
|
||||
const int kPanelW = kWidth - kPanelX * 2;
|
||||
const int kPanelH = 132;
|
||||
const int kMarginX = 24;
|
||||
|
||||
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 kHeadY = 46;
|
||||
const int kHeadH = 26;
|
||||
const int kSubY = 74;
|
||||
const int kSubH = 20;
|
||||
|
||||
// 更新日志卡片
|
||||
const int kLogX = kMarginX;
|
||||
const int kLogY = 100;
|
||||
const int kLogW = kWidth - kMarginX * 2;
|
||||
const int kLogH = 180;
|
||||
const int kLogPadX = 16;
|
||||
const int kLogTitleY = kLogY + 12;
|
||||
const int kLogTitleH = 22;
|
||||
const int kLogTextY = kLogTitleY + kLogTitleH + 4;
|
||||
const int kLogTextH = kLogY + kLogH - 12 - kLogTextY;
|
||||
const int kLogTextW = kLogW - kLogPadX * 2;
|
||||
|
||||
// 状态行与进度条
|
||||
const int kStatusY = kLogY + kLogH + 12;
|
||||
const int kStatusH = 22;
|
||||
const int kBarX = kMarginX;
|
||||
const int kBarY = kStatusY + kStatusH + 10;
|
||||
const int kBarW = kWidth - kMarginX * 2;
|
||||
const int kBarH = 10;
|
||||
|
||||
// 安装目录选择面板
|
||||
const int kPickPanelX = 48;
|
||||
const int kPickPanelY = 176;
|
||||
const int kPickPanelW = kWidth - kPickPanelX * 2;
|
||||
const int kPickPanelH = 248;
|
||||
const int kPickPanelX = kMarginX;
|
||||
const int kPickPanelW = kWidth - kMarginX * 2;
|
||||
|
||||
const int kPickTextX = 72;
|
||||
const int kPickTitleY = 196;
|
||||
const int kPickHintY = 234;
|
||||
const int kPickTextX = kPickPanelX;
|
||||
const int kPickTitleY = 46;
|
||||
const int kPickHintY = 78;
|
||||
|
||||
const int kPathX = 72;
|
||||
const int kPathY = 268;
|
||||
const int kPathW = 596;
|
||||
const int kPathH = 38;
|
||||
const int kPathX = kPickPanelX;
|
||||
const int kPathY = 126;
|
||||
const int kPathW = kPickPanelW - 12 - 104;
|
||||
const int kPathH = 34;
|
||||
|
||||
const int kBrowseX = kPathX + kPathW + 12;
|
||||
const int kBrowseW = 116;
|
||||
const int kBrowseW = 104;
|
||||
|
||||
const int kSpaceY = 318;
|
||||
const int kSpaceY = 176;
|
||||
|
||||
const int kButtonY = 356;
|
||||
const int kButtonH = 44;
|
||||
const int kInstallW = 148;
|
||||
const int kInstallX = kPickPanelX + kPickPanelW - kInstallW;
|
||||
const int kCancelW = 108;
|
||||
const int kButtonY = 298;
|
||||
const int kButtonH = 38;
|
||||
const int kInstallW = 140;
|
||||
const int kInstallX = kWidth - kMarginX - kInstallW;
|
||||
const int kCancelW = 104;
|
||||
const int kCancelX = kInstallX - 12 - kCancelW;
|
||||
|
||||
const wchar_t* const kFontName = L"Microsoft YaHei";
|
||||
// 更新日志一屏滚动的步长
|
||||
const int kScrollStep = 24;
|
||||
|
||||
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();
|
||||
const wchar_t* const kFontName = L"Segoe UI";
|
||||
|
||||
// Visual Studio 2019 暗色主题取色
|
||||
const Gdiplus::ARGB kArgbWindow = 0xFF1E1E1E; // 窗口背景
|
||||
const Gdiplus::ARGB kArgbTitleBar = 0xFF2D2D30; // 标题栏
|
||||
const Gdiplus::ARGB kArgbSurface = 0xFF252526; // 卡片/面板
|
||||
const Gdiplus::ARGB kArgbControl = 0xFF333337; // 输入框/按钮/进度条轨道
|
||||
const Gdiplus::ARGB kArgbBorder = 0xFF3F3F46; // 分隔线/边框
|
||||
const Gdiplus::ARGB kArgbHover = 0xFF3E3E42; // 控件悬停
|
||||
const Gdiplus::ARGB kArgbAccent = 0xFF007ACC; // 强调蓝
|
||||
const Gdiplus::ARGB kArgbAccentHover = 0xFF1B8AD6;
|
||||
const Gdiplus::ARGB kArgbAccentPress = 0xFF005C9E;
|
||||
const Gdiplus::ARGB kArgbText = 0xFFFFFFFF;
|
||||
const Gdiplus::ARGB kArgbTextDim = 0xFFCCCCCC;
|
||||
const Gdiplus::ARGB kArgbTextWeak = 0xFF999999;
|
||||
const Gdiplus::ARGB kArgbCloseHover = 0xFFE81123; // 关闭按钮悬停红
|
||||
const Gdiplus::ARGB kArgbClosePress = 0xFFB01018;
|
||||
const Gdiplus::ARGB kArgbError = 0xFFE81123;
|
||||
|
||||
// 1 像素描边:GDI+ 默认会把整数坐标的边框画成两像素灰边,内缩半像素才是清晰的一条线
|
||||
void DrawHairline(Gdiplus::Graphics& graphics, const Gdiplus::Rect& rect, Gdiplus::ARGB color) {
|
||||
Gdiplus::Pen pen(color, 1.0f);
|
||||
graphics.DrawRectangle(&pen,
|
||||
Gdiplus::RectF(static_cast<float>(rect.X) + 0.5f,
|
||||
static_cast<float>(rect.Y) + 0.5f,
|
||||
static_cast<float>(rect.Width) - 1.0f,
|
||||
static_cast<float>(rect.Height) - 1.0f));
|
||||
}
|
||||
|
||||
// VS 2019 的按钮是直角矩形:主按钮强调蓝实底,次按钮深灰底 + 灰描边
|
||||
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;
|
||||
Gdiplus::Rect rect(x, y, w, h);
|
||||
Gdiplus::Color textColor;
|
||||
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);
|
||||
}
|
||||
Gdiplus::ARGB fill = state == 2 ? kArgbAccentPress : state == 1 ? kArgbAccentHover : kArgbAccent;
|
||||
Gdiplus::SolidBrush brush(fill);
|
||||
graphics.FillRectangle(&brush, rect);
|
||||
textColor = Gdiplus::Color(kArgbText);
|
||||
} 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::ARGB fill = state == 2 ? kArgbSurface : state == 1 ? kArgbHover : kArgbControl;
|
||||
Gdiplus::SolidBrush brush(fill);
|
||||
graphics.FillRectangle(&brush, rect);
|
||||
DrawHairline(graphics, rect, kArgbBorder);
|
||||
textColor = Gdiplus::Color(state == 0 ? kArgbTextDim : kArgbText);
|
||||
}
|
||||
|
||||
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);
|
||||
Gdiplus::Font font(kFontName, 13.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::SolidBrush textBrush(textColor);
|
||||
Gdiplus::RectF textRect(static_cast<float>(x), static_cast<float>(y), static_cast<float>(w),
|
||||
static_cast<float>(h));
|
||||
graphics.DrawString(text, -1, &font, textRect, &format, &textBrush);
|
||||
}
|
||||
|
||||
std::wstring PercentText(int percent) {
|
||||
@@ -191,8 +187,6 @@ bool QueryFreeSpace(const std::wstring& path, unsigned long long& freeBytes) {
|
||||
|
||||
struct UpdateWindow::RenderContext {
|
||||
ULONG_PTR token = 0;
|
||||
Gdiplus::Image* background = nullptr;
|
||||
Gdiplus::Image* closeImages[3] = {nullptr, nullptr, nullptr};
|
||||
};
|
||||
|
||||
UpdateWindow::UpdateWindow() : render_(new RenderContext()) {
|
||||
@@ -203,8 +197,6 @@ UpdateWindow::UpdateWindow() : render_(new RenderContext()) {
|
||||
|
||||
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;
|
||||
@@ -216,17 +208,9 @@ 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.style = CS_HREDRAW | CS_VREDRAW;
|
||||
wc.lpfnWndProc = &UpdateWindow::WndProc;
|
||||
wc.hInstance = instance;
|
||||
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
||||
@@ -239,14 +223,12 @@ bool UpdateWindow::Create(HINSTANCE instance, const UpdateOptions& options) {
|
||||
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);
|
||||
// 分层窗口:整体画在 32 位位图上再提交,圆角由 GDI+ 抗锯齿,不会有锯齿边
|
||||
hwnd_ = CreateWindowExW(WS_EX_LAYERED | 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();
|
||||
@@ -259,8 +241,9 @@ bool UpdateWindow::Create(HINSTANCE instance, const UpdateOptions& options) {
|
||||
status_ = options.fullMode ? L"正在准备完整包更新..." : L"正在检查版本更新...";
|
||||
}
|
||||
|
||||
Render();
|
||||
ShowWindow(hwnd_, SW_SHOW);
|
||||
::UpdateWindow(hwnd_);
|
||||
Render();
|
||||
|
||||
if (!pickDir) StartWorker(options);
|
||||
return true;
|
||||
@@ -282,6 +265,12 @@ void UpdateWindow::StartWorker(const UpdateOptions& options) {
|
||||
state->finished = true;
|
||||
state->success = ok;
|
||||
PostMessageW(hwnd, kWmState, 0, reinterpret_cast<LPARAM>(state));
|
||||
},
|
||||
[this, hwnd](const UpdateInfo& info) {
|
||||
UiState* state = new UiState();
|
||||
state->hasInfo = true;
|
||||
state->info = info;
|
||||
PostMessageW(hwnd, kWmState, 0, reinterpret_cast<LPARAM>(state));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -404,8 +393,38 @@ void UpdateWindow::RequestClose() {
|
||||
DestroyWindow(hwnd_);
|
||||
}
|
||||
|
||||
void UpdateWindow::ApplyInfo(const UpdateInfo& info) {
|
||||
info_ = info;
|
||||
|
||||
// 后台可能只填了其中一个,两个都没有时给句占位说明
|
||||
std::wstring text = info.changelog;
|
||||
if (text.empty()) text = info.updateNotes;
|
||||
if (text.empty()) text = L"本次更新包含功能优化与稳定性改进。";
|
||||
changelogText_ = text;
|
||||
|
||||
if (info.currentVersion.empty() && !options_.clientVersion.empty()) {
|
||||
info_.currentVersion = options_.clientVersion;
|
||||
}
|
||||
changelogScroll_ = 0;
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
void UpdateWindow::ScrollChangelog(int delta) {
|
||||
int maxScroll = (std::max)(0, changelogContentH_ - kLogTextH);
|
||||
int next = changelogScroll_ + delta;
|
||||
next = (std::max)(0, (std::min)(maxScroll, next));
|
||||
if (next != changelogScroll_) {
|
||||
changelogScroll_ = next;
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateWindow::OnState(const UiState& state) {
|
||||
if (closing_) return;
|
||||
if (state.hasInfo) {
|
||||
ApplyInfo(state.info);
|
||||
return;
|
||||
}
|
||||
percent_ = std::max(0, std::min(100, state.percent));
|
||||
status_ = state.status;
|
||||
if (state.finished) {
|
||||
@@ -417,22 +436,17 @@ void UpdateWindow::OnState(const UiState& state) {
|
||||
}
|
||||
|
||||
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::SolidBrush textBrush(kArgbText);
|
||||
Gdiplus::SolidBrush dimBrush(kArgbTextDim);
|
||||
|
||||
Gdiplus::Font titleFont(kFontName, 18.0f, Gdiplus::FontStyleBold, Gdiplus::UnitPixel);
|
||||
Gdiplus::Font titleFont(kFontName, 16.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::RectF titleRect(static_cast<float>(kPickTextX), static_cast<float>(kPickTitleY),
|
||||
static_cast<float>(kPickPanelW - 48), 28.0f);
|
||||
static_cast<float>(kPickPanelW), 26.0f);
|
||||
std::wstring appName = UPDATER_APP_NAME;
|
||||
std::wstring titleText =
|
||||
appName.empty() ? L"选择安装目录" : (L"选择 " + appName + L" 的安装目录");
|
||||
@@ -440,17 +454,15 @@ void UpdateWindow::DrawPickPanel(Gdiplus::Graphics& graphics) {
|
||||
|
||||
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);
|
||||
static_cast<float>(kPickPanelW), 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::Rect boxRect(kPathX, kPathY, kPathW, kPathH);
|
||||
Gdiplus::SolidBrush boxBrush(kArgbControl);
|
||||
graphics.FillRectangle(&boxBrush, boxRect);
|
||||
DrawHairline(graphics, boxRect, kArgbBorder);
|
||||
|
||||
Gdiplus::StringFormat pathFormat;
|
||||
pathFormat.SetAlignment(Gdiplus::StringAlignmentNear);
|
||||
@@ -475,7 +487,7 @@ void UpdateWindow::DrawPickPanel(Gdiplus::Graphics& graphics) {
|
||||
}
|
||||
if (!spaceText.empty()) {
|
||||
Gdiplus::RectF spaceRect(static_cast<float>(kPickTextX), static_cast<float>(kSpaceY),
|
||||
static_cast<float>(kPickPanelW - 48), 22.0f);
|
||||
static_cast<float>(kPickPanelW), 22.0f);
|
||||
graphics.DrawString(spaceText.c_str(), -1, &hintFont, spaceRect, &nearFormat, &dimBrush);
|
||||
}
|
||||
|
||||
@@ -484,87 +496,194 @@ void UpdateWindow::DrawPickPanel(Gdiplus::Graphics& graphics) {
|
||||
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);
|
||||
// VS 2019 暗色主题:纯色窗口背景 + 一条标题栏,不做渐变和装饰
|
||||
void UpdateWindow::DrawBackground(Gdiplus::Graphics& graphics) {
|
||||
Gdiplus::SolidBrush windowBrush(kArgbWindow);
|
||||
graphics.FillRectangle(&windowBrush, 0, 0, kWidth, kHeight);
|
||||
|
||||
Gdiplus::SolidBrush textBrush(Gdiplus::Color(255, 240, 244, 250));
|
||||
Gdiplus::SolidBrush dimBrush(Gdiplus::Color(200, 176, 186, 202));
|
||||
Gdiplus::SolidBrush titleBrush(kArgbTitleBar);
|
||||
graphics.FillRectangle(&titleBrush, 0, 0, kWidth, kTitleH);
|
||||
|
||||
// 标题栏与内容区之间的分隔线,取整像素位置避免被抗锯齿糊掉
|
||||
Gdiplus::Pen line(kArgbBorder, 1.0f);
|
||||
graphics.DrawLine(&line, Gdiplus::PointF(0.0f, static_cast<float>(kTitleH) - 0.5f),
|
||||
Gdiplus::PointF(static_cast<float>(kWidth), static_cast<float>(kTitleH) - 0.5f));
|
||||
|
||||
Gdiplus::Font font(kFontName, 12.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::StringFormat format;
|
||||
format.SetAlignment(Gdiplus::StringAlignmentNear);
|
||||
format.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
||||
format.SetTrimming(Gdiplus::StringTrimmingEllipsisWord);
|
||||
Gdiplus::SolidBrush textBrush(kArgbTextDim);
|
||||
Gdiplus::RectF rect(10.0f, 0.0f, static_cast<float>(kWidth - kCloseSize - 16),
|
||||
static_cast<float>(kTitleH));
|
||||
graphics.DrawString(UPDATER_TITLE, -1, &font, rect, &format, &textBrush);
|
||||
}
|
||||
|
||||
// 更新日志:先量出文本总高度,再按滚动偏移裁剪绘制
|
||||
void UpdateWindow::DrawChangelog(Gdiplus::Graphics& graphics) {
|
||||
Gdiplus::Rect card(kLogX, kLogY, kLogW, kLogH);
|
||||
Gdiplus::SolidBrush cardBrush(kArgbSurface);
|
||||
graphics.FillRectangle(&cardBrush, card);
|
||||
DrawHairline(graphics, card, kArgbBorder);
|
||||
|
||||
Gdiplus::SolidBrush titleBrush(kArgbText);
|
||||
Gdiplus::Font titleFont(kFontName, 13.0f, Gdiplus::FontStyleBold, Gdiplus::UnitPixel);
|
||||
Gdiplus::RectF titleRect(static_cast<float>(kLogX + kLogPadX), static_cast<float>(kLogTitleY),
|
||||
static_cast<float>(kLogTextW), static_cast<float>(kLogTitleH));
|
||||
graphics.DrawString(L"更新内容", -1, &titleFont, titleRect, nullptr, &titleBrush);
|
||||
|
||||
std::wstring text = changelogText_.empty() ? L"正在获取更新说明..." : changelogText_;
|
||||
|
||||
Gdiplus::Font textFont(kFontName, 12.5f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::StringFormat format;
|
||||
format.SetAlignment(Gdiplus::StringAlignmentNear);
|
||||
format.SetLineAlignment(Gdiplus::StringAlignmentNear);
|
||||
format.SetTrimming(Gdiplus::StringTrimmingNone);
|
||||
|
||||
Gdiplus::RectF measureRect(0.0f, 0.0f, static_cast<float>(kLogTextW), 10000.0f);
|
||||
Gdiplus::RectF needed;
|
||||
graphics.MeasureString(text.c_str(), -1, &textFont, measureRect, &format, &needed);
|
||||
changelogContentH_ = static_cast<int>(needed.Height + 0.999f);
|
||||
|
||||
int maxScroll = (std::max)(0, changelogContentH_ - kLogTextH);
|
||||
if (changelogScroll_ > maxScroll) changelogScroll_ = maxScroll;
|
||||
if (changelogScroll_ < 0) changelogScroll_ = 0;
|
||||
|
||||
graphics.SetClip(Gdiplus::Rect(kLogX + kLogPadX, kLogTextY, kLogTextW, kLogTextH));
|
||||
Gdiplus::SolidBrush textBrush(kArgbTextDim);
|
||||
Gdiplus::RectF drawRect(static_cast<float>(kLogX + kLogPadX),
|
||||
static_cast<float>(kLogTextY - changelogScroll_),
|
||||
static_cast<float>(kLogTextW), static_cast<float>(needed.Height));
|
||||
graphics.DrawString(text.c_str(), -1, &textFont, drawRect, &format, &textBrush);
|
||||
graphics.ResetClip();
|
||||
|
||||
// 内容超出时给个滚动提示
|
||||
if (maxScroll > 0) {
|
||||
Gdiplus::SolidBrush hintBrush(kArgbTextWeak);
|
||||
Gdiplus::Font hintFont(kFontName, 11.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::StringFormat farFormat;
|
||||
farFormat.SetAlignment(Gdiplus::StringAlignmentFar);
|
||||
farFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
|
||||
Gdiplus::RectF hintRect(static_cast<float>(kLogTextW - 150),
|
||||
static_cast<float>(kLogTitleY), 150.0f,
|
||||
static_cast<float>(kLogTitleH));
|
||||
graphics.DrawString(L"滚轮可滚动", -1, &hintFont, hintRect, &farFormat, &hintBrush);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateWindow::DrawProgressPanel(Gdiplus::Graphics& graphics) {
|
||||
Gdiplus::SolidBrush textBrush(kArgbText);
|
||||
Gdiplus::SolidBrush dimBrush(kArgbTextDim);
|
||||
|
||||
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);
|
||||
std::wstring headText = L"正在检查更新";
|
||||
std::wstring subText = UPDATER_APP_NAME;
|
||||
if (info_.hasUpdate) {
|
||||
if (!info_.latestVersion.empty()) headText = L"发现新版本 " + info_.latestVersion;
|
||||
if (!info_.currentVersion.empty() && !info_.latestVersion.empty()) {
|
||||
subText = L"当前 " + info_.currentVersion + L" → 最新 " + info_.latestVersion;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
Gdiplus::Font headFont(kFontName, 17.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::RectF headRect(static_cast<float>(kMarginX), static_cast<float>(kHeadY),
|
||||
static_cast<float>(kWidth - kMarginX * 2 - kCloseSize),
|
||||
static_cast<float>(kHeadH));
|
||||
graphics.DrawString(headText.c_str(), -1, &headFont, headRect, &nearFormat, &textBrush);
|
||||
|
||||
Gdiplus::Font subFont(kFontName, 12.5f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::RectF subRect(static_cast<float>(kMarginX), static_cast<float>(kSubY),
|
||||
static_cast<float>(kWidth - kMarginX * 2), static_cast<float>(kSubH));
|
||||
graphics.DrawString(subText.c_str(), -1, &subFont, subRect, &nearFormat, &dimBrush);
|
||||
|
||||
DrawChangelog(graphics);
|
||||
|
||||
Gdiplus::Font statusFont(kFontName, 13.0f, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
|
||||
Gdiplus::RectF statusRect(static_cast<float>(kBarX), static_cast<float>(kStatusY),
|
||||
static_cast<float>(kBarW - 90), static_cast<float>(kStatusH));
|
||||
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::Font percentFont(kFontName, 13.0f, Gdiplus::FontStyleRegular, 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);
|
||||
static_cast<float>(kStatusY), 80.0f, static_cast<float>(kStatusH));
|
||||
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);
|
||||
Gdiplus::SolidBrush trackBrush(kArgbControl);
|
||||
graphics.FillRectangle(&trackBrush, kBarX, kBarY, kBarW, kBarH);
|
||||
|
||||
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);
|
||||
|
||||
if (fillW > 0) {
|
||||
// 失败时转 VS 的错误红,正常时是主题蓝
|
||||
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);
|
||||
Gdiplus::SolidBrush fillBrush(failed ? kArgbError : kArgbAccent);
|
||||
graphics.FillRectangle(&fillBrush, kBarX, kBarY, fillW, kBarH);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateWindow::OnPaint() {
|
||||
PAINTSTRUCT ps;
|
||||
HDC hdc = BeginPaint(hwnd_, &ps);
|
||||
// 标题栏右侧的关闭按钮:悬停/按下时整块变红,和 VS 2019 一致
|
||||
void UpdateWindow::DrawCloseButton(Gdiplus::Graphics& graphics) {
|
||||
int left = kWidth - kCloseSize;
|
||||
|
||||
RECT rc;
|
||||
GetClientRect(hwnd_, &rc);
|
||||
int width = rc.right - rc.left;
|
||||
int height = rc.bottom - rc.top;
|
||||
if (closeState_ != 0) {
|
||||
Gdiplus::SolidBrush bg(closeState_ == 2 ? kArgbClosePress : kArgbCloseHover);
|
||||
graphics.FillRectangle(&bg, left, 0, kCloseSize, kTitleH);
|
||||
}
|
||||
|
||||
HDC memDc = CreateCompatibleDC(hdc);
|
||||
HBITMAP memBmp = CreateCompatibleBitmap(hdc, width, height);
|
||||
HGDIOBJ old = SelectObject(memDc, memBmp);
|
||||
float cx = static_cast<float>(left + kCloseSize / 2);
|
||||
float cy = static_cast<float>(kTitleH / 2);
|
||||
float a = 5.0f;
|
||||
Gdiplus::Pen pen(kArgbText, 1.4f);
|
||||
pen.SetStartCap(Gdiplus::LineCapRound);
|
||||
pen.SetEndCap(Gdiplus::LineCapRound);
|
||||
pen.SetLineJoin(Gdiplus::LineJoinRound);
|
||||
graphics.DrawLine(&pen, cx - a, cy - a, cx + a, cy + a);
|
||||
graphics.DrawLine(&pen, cx + a, cy - a, cx - a, cy + a);
|
||||
}
|
||||
|
||||
// 分层窗口:整窗画到 32 位 DIB 上再一次性提交。
|
||||
// 圆角、斜切色块、进度条全部走 GDI+ 抗锯齿,边缘不会出现锯齿
|
||||
void UpdateWindow::Render() {
|
||||
if (!hwnd_) return;
|
||||
|
||||
BITMAPINFO bi;
|
||||
ZeroMemory(&bi, sizeof(bi));
|
||||
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bi.bmiHeader.biWidth = kWidth;
|
||||
bi.bmiHeader.biHeight = -kHeight; // 负值 = 自上而下,省一次翻转
|
||||
bi.bmiHeader.biPlanes = 1;
|
||||
bi.bmiHeader.biBitCount = 32;
|
||||
bi.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
void* bits = nullptr;
|
||||
HDC screenDc = GetDC(nullptr);
|
||||
HBITMAP dib = CreateDIBSection(screenDc, &bi, DIB_RGB_COLORS, &bits, nullptr, 0);
|
||||
ReleaseDC(nullptr, screenDc);
|
||||
if (!dib || !bits) {
|
||||
if (dib) DeleteObject(dib);
|
||||
return;
|
||||
}
|
||||
|
||||
HDC memDc = CreateCompatibleDC(nullptr);
|
||||
HGDIOBJ oldBmp = SelectObject(memDc, dib);
|
||||
|
||||
{
|
||||
Gdiplus::Graphics graphics(memDc);
|
||||
graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
|
||||
Gdiplus::Bitmap bitmap(kWidth, kHeight, kWidth * 4, PixelFormat32bppARGB,
|
||||
static_cast<BYTE*>(bits));
|
||||
Gdiplus::Graphics graphics(&bitmap);
|
||||
graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAliasGridFit);
|
||||
graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
|
||||
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
|
||||
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
|
||||
|
||||
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);
|
||||
}
|
||||
DrawBackground(graphics);
|
||||
|
||||
if (phase_ == Phase::PickDir) {
|
||||
DrawPickPanel(graphics);
|
||||
@@ -572,25 +691,33 @@ void UpdateWindow::OnPaint() {
|
||||
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);
|
||||
}
|
||||
DrawCloseButton(graphics);
|
||||
|
||||
DrawHairline(graphics, Gdiplus::Rect(0, 0, kWidth, kHeight), kArgbBorder);
|
||||
}
|
||||
|
||||
BitBlt(hdc, 0, 0, width, height, memDc, 0, 0, SRCCOPY);
|
||||
SelectObject(memDc, old);
|
||||
DeleteObject(memBmp);
|
||||
POINT ptSrc = {0, 0};
|
||||
RECT wr;
|
||||
GetWindowRect(hwnd_, &wr);
|
||||
POINT ptDst = {wr.left, wr.top};
|
||||
SIZE size = {kWidth, kHeight};
|
||||
BLENDFUNCTION blend;
|
||||
blend.BlendOp = AC_SRC_OVER;
|
||||
blend.BlendFlags = 0;
|
||||
blend.SourceConstantAlpha = 255;
|
||||
blend.AlphaFormat = AC_SRC_ALPHA;
|
||||
UpdateLayeredWindow(hwnd_, nullptr, &ptDst, &size, memDc, &ptSrc, 0, &blend, ULW_ALPHA);
|
||||
|
||||
SelectObject(memDc, oldBmp);
|
||||
DeleteDC(memDc);
|
||||
DeleteObject(dib);
|
||||
}
|
||||
|
||||
void UpdateWindow::OnPaint() {
|
||||
PAINTSTRUCT ps;
|
||||
BeginPaint(hwnd_, &ps);
|
||||
EndPaint(hwnd_, &ps);
|
||||
Render();
|
||||
}
|
||||
|
||||
LRESULT UpdateWindow::HandleMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
|
||||
@@ -637,6 +764,14 @@ LRESULT UpdateWindow::HandleMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_MOUSEWHEEL: {
|
||||
if (phase_ != Phase::PickDir) {
|
||||
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
|
||||
ScrollChangelog(-delta / WHEEL_DELTA * kScrollStep);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_MOUSELEAVE:
|
||||
tracking_ = false;
|
||||
pressed_ = 0;
|
||||
|
||||
Reference in New Issue
Block a user