新增设置窗口与逐像素透明度,Win7 接入 Aero 毛玻璃

This commit is contained in:
tongtong committed 2026-11-16 09:24:06 +08:00
1 parent e1e26acc49
commit 7e34929caa
12 files changed
+850 -9

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
#include "stdafx.h"
#include "aero_backdrop.h"
#include <dwmapi.h>
#include <cstdarg>
#include <cstdio>
#pragma comment(lib, "dwmapi.lib")
namespace
{
constexpr wchar_t kBackdropClassName[] = L"TTDownloaderAeroBackdrop";
void LogAero(const wchar_t* format, ...)
{
wchar_t temp[MAX_PATH] = {};
if (::GetTempPathW(MAX_PATH, temp) == 0)
return;
std::wstring path = std::wstring(temp) + L"TTDownloader_aero.log";
FILE* file = nullptr;
if (_wfopen_s(&file, path.c_str(), L"a") != 0 || file == nullptr)
return;
va_list args;
va_start(args, format);
vfwprintf(file, format, args);
va_end(args);
fputc(L'\n', file);
fclose(file);
}
}
AeroBackdrop::~AeroBackdrop()
{
Destroy();
}
bool AeroBackdrop::Create(HWND owner)
{
if (hwnd_ != nullptr)
return true;
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = &AeroBackdrop::WndProc;
wc.hInstance = ::GetModuleHandleW(nullptr);
wc.hCursor = ::LoadCursorW(nullptr, IDC_ARROW);
wc.lpszClassName = kBackdropClassName;
if (::RegisterClassExW(&wc) == 0 && ::GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
return false;
hwnd_ = ::CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE | WS_EX_TRANSPARENT,
kBackdropClassName, L"", WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, wc.hInstance, nullptr);
if (hwnd_ == nullptr)
return false;
BOOL composition = FALSE;
::DwmIsCompositionEnabled(&composition);
LogAero(L"create backdrop hwnd=%p composition=%d", hwnd_, composition);
Sync(owner);
return true;
}
void AeroBackdrop::Sync(HWND owner)
{
if (hwnd_ == nullptr || owner == nullptr)
return;
bool visible = ::IsWindowVisible(owner) != FALSE && ::IsIconic(owner) == FALSE;
if (!visible)
{
if (visible_)
{
visible_ = false;
::ShowWindow(hwnd_, SW_HIDE);
LogAero(L"sync hidden");
}
return;
}
RECT client = {};
::GetClientRect(owner, &client);
POINT origin = { client.left, client.top };
::ClientToScreen(owner, &origin);
::SetWindowPos(hwnd_, owner, origin.x, origin.y,
client.right - client.left, client.bottom - client.top, SWP_NOACTIVATE | SWP_SHOWWINDOW);
if (!visible_)
{
visible_ = true;
LogAero(L"sync visible rect=%d,%d %dx%d", origin.x, origin.y,
client.right - client.left, client.bottom - client.top);
}
ApplyGlass(client.right - client.left, client.bottom - client.top);
}
void AeroBackdrop::Refresh()
{
blur_width_ = 0;
blur_height_ = 0;
blur_enabled_ = false;
}
void AeroBackdrop::ApplyGlass(int width, int height)
{
if (blur_enabled_ && width == blur_width_ && height == blur_height_)
return;
blur_width_ = width;
blur_height_ = height;
DWM_BLURBEHIND blur = {};
blur.dwFlags = DWM_BB_ENABLE;
blur.fEnable = TRUE;
blur.hRgnBlur = nullptr;
HRESULT result = ::DwmEnableBlurBehindWindow(hwnd_, &blur);
blur_enabled_ = SUCCEEDED(result);
LogAero(L"enable blur size=%dx%d hr=0x%08X", width, height,
static_cast<unsigned int>(result));
::InvalidateRect(hwnd_, nullptr, TRUE);
}
void AeroBackdrop::Hide()
{
if (hwnd_ != nullptr && visible_)
{
visible_ = false;
::ShowWindow(hwnd_, SW_HIDE);
}
}
void AeroBackdrop::Destroy()
{
if (hwnd_ != nullptr)
{
::DestroyWindow(hwnd_);
hwnd_ = nullptr;
}
}
LRESULT CALLBACK AeroBackdrop::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_ERASEBKGND)
return 1;
if (uMsg == WM_PAINT)
{
PAINTSTRUCT ps = {};
HDC dc = ::BeginPaint(hwnd, &ps);
RECT client = {};
::GetClientRect(hwnd, &client);
::FillRect(dc, &client, static_cast<HBRUSH>(::GetStockObject(BLACK_BRUSH)));
::EndPaint(hwnd, &ps);
return 0;
}
return ::DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include <windows.h>
class AeroBackdrop
{
public:
AeroBackdrop() = default;
~AeroBackdrop();
AeroBackdrop(const AeroBackdrop&) = delete;
AeroBackdrop& operator=(const AeroBackdrop&) = delete;
bool Create(HWND owner);
void Sync(HWND owner);
void Refresh();
void Hide();
void Destroy();
bool IsValid() const { return hwnd_ != nullptr; }
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void ApplyGlass(int width, int height);
HWND hwnd_ = nullptr;
int blur_width_ = 0;
int blur_height_ = 0;
bool blur_enabled_ = false;
bool visible_ = false;
};
+300 -1
View File
@@ -1,14 +1,104 @@
#include "stdafx.h"
#include "downloader_form.h"
#include "file_drop_source.h"
#include "settings_form.h"
#include <dwmapi.h>
#include <algorithm>
#include <cstdio>
#include <cwctype>
#pragma comment(lib, "dwmapi.lib")
namespace
{
constexpr wchar_t kSkinRegKey[] = L"Software\\TTDownloader";
constexpr int kBaseAlphaMin = 26;
constexpr UINT_PTR kAeroSyncTimer = 1;
constexpr UINT kAeroSyncDelayMs = 300;
constexpr UINT kDwmCompositionChanged = 0x031E;
int OpacityToAlpha(int opacity)
{
return opacity * 255 / 100;
}
int BaseOpacityToAlpha(int opacity)
{
return kBaseAlphaMin + (255 - kBaseAlphaMin) * opacity / 100;
}
void ApplyPanelOpacity(ui::Control* panel, int opacity)
{
if (panel == nullptr)
return;
int alpha = OpacityToAlpha(opacity);
std::wstring color = nbase::StringPrintf(L"tt_panel_%d", alpha);
ui::GlobalManager::AddTextColor(color, nbase::StringPrintf(L"#%02XFFFFFF", alpha));
panel->SetBkColor(color);
}
bool IsWin7AeroAvailable()
{
HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
if (ntdll == nullptr)
return false;
using RtlGetVersionFn = LONG(WINAPI*)(OSVERSIONINFOW*);
auto rtl_get_version = reinterpret_cast<RtlGetVersionFn>(
::GetProcAddress(ntdll, "RtlGetVersion"));
if (rtl_get_version == nullptr)
return false;
OSVERSIONINFOW version = {};
version.dwOSVersionInfoSize = sizeof(version);
if (rtl_get_version(&version) != 0)
return false;
if (version.dwMajorVersion != 6 || version.dwMinorVersion != 1)
return false;
BOOL composition = FALSE;
return SUCCEEDED(::DwmIsCompositionEnabled(&composition)) && composition;
}
std::wstring ReadSkinString(const wchar_t* name)
{
HKEY key = nullptr;
if (::RegOpenKeyExW(HKEY_CURRENT_USER, kSkinRegKey, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
return std::wstring();
wchar_t buffer[1024] = {};
DWORD type = 0;
DWORD size = sizeof(buffer);
LSTATUS status = ::RegQueryValueExW(key, name, nullptr, &type,
reinterpret_cast<BYTE*>(buffer), &size);
::RegCloseKey(key);
if (status != ERROR_SUCCESS || type != REG_SZ)
return std::wstring();
buffer[(sizeof(buffer) / sizeof(buffer[0])) - 1] = L'\0';
return buffer;
}
DWORD ReadSkinDword(const wchar_t* name, DWORD fallback)
{
HKEY key = nullptr;
if (::RegOpenKeyExW(HKEY_CURRENT_USER, kSkinRegKey, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
return fallback;
DWORD type = 0;
DWORD value = 0;
DWORD size = sizeof(value);
LSTATUS status = ::RegQueryValueExW(key, name, nullptr, &type,
reinterpret_cast<BYTE*>(&value), &size);
::RegCloseKey(key);
if (status != ERROR_SUCCESS || type != REG_DWORD || size != sizeof(value))
return fallback;
return value;
}
std::wstring TrimText(const std::wstring& text)
{
size_t begin = 0;
@@ -122,7 +212,8 @@ std::wstring DownloaderForm::GetSkinFolder()
std::wstring DownloaderForm::GetSkinFile()
{
return L"downloader.xml";
aero_mode_ = IsWin7AeroAvailable();
return aero_mode_ ? L"downloader_aero.xml" : L"downloader.xml";
}
std::wstring DownloaderForm::GetWindowClassName() const
@@ -137,10 +228,21 @@ void DownloaderForm::InitWindow()
task_list_ = dynamic_cast<ui::ListBox*>(FindControl(L"task_list"));
lbl_summary_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_summary"));
lbl_selected_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_selected"));
content_root_ = FindControl(L"content_root");
panel_header_ = FindControl(L"panel_header");
panel_input_ = FindControl(L"panel_input");
panel_table_header_ = FindControl(L"panel_table_header");
panel_bottom_ = FindControl(L"panel_bottom");
ui::GlobalManager::AddTextColor(L"tt_panel_default", L"#B3FFFFFF");
ui::GlobalManager::AddTextColor(L"tt_row_hot", L"#66F0F2F5");
ui::GlobalManager::AddTextColor(L"tt_row_selected", L"#99E4E7EB");
ole_initialized_ = SUCCEEDED(::OleInitialize(nullptr));
edit_path_->SetText(GetDefaultSaveDir());
LoadSkinSettings();
ApplySkinSettings();
UpdateSummary();
m_pRoot->AttachBubbledEvent(ui::kEventClick, nbase::Bind(&DownloaderForm::OnClicked, this, std::placeholders::_1));
@@ -151,6 +253,12 @@ void DownloaderForm::InitWindow()
engine_ = std::make_unique<DownloadEngine>(GetHWND());
engine_->Start();
if (aero_mode_)
{
aero_backdrop_.Create(GetHWND());
::SetTimer(GetHWND(), kAeroSyncTimer, kAeroSyncDelayMs, nullptr);
}
}
LRESULT DownloaderForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
@@ -184,6 +292,35 @@ LRESULT DownloaderForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
return 0;
}
if (uMsg == WM_SHOWWINDOW && settings_form_ != nullptr)
settings_form_->ShowWindow(wParam != FALSE, false);
if (uMsg == WM_TIMER && wParam == kAeroSyncTimer)
{
::KillTimer(GetHWND(), kAeroSyncTimer);
aero_backdrop_.Refresh();
aero_backdrop_.Sync(GetHWND());
return 0;
}
if (uMsg == kDwmCompositionChanged && aero_mode_ && aero_backdrop_.IsValid())
{
aero_backdrop_.Refresh();
aero_backdrop_.Sync(GetHWND());
return 0;
}
if (aero_mode_ && aero_backdrop_.IsValid()
&& (uMsg == WM_SHOWWINDOW || uMsg == WM_SIZE || uMsg == WM_MOVE || uMsg == WM_ACTIVATE))
{
LRESULT result = __super::HandleMessage(uMsg, wParam, lParam);
if (uMsg == WM_SHOWWINDOW && wParam == FALSE)
aero_backdrop_.Hide();
else
aero_backdrop_.Sync(GetHWND());
return result;
}
if (uMsg == WM_LBUTTONDOWN)
{
PrepareFileDrag(lParam);
@@ -232,6 +369,8 @@ LRESULT DownloaderForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& b
LRESULT DownloaderForm::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
aero_backdrop_.Destroy();
if (engine_ != nullptr)
{
engine_->Stop();
@@ -275,6 +414,10 @@ bool DownloaderForm::OnClicked(ui::EventArgs* args)
StartSelectedTask();
else if (name == L"btn_delete")
DeleteSelectedTask();
else if (name == L"btn_settings")
ShowSettingsForm();
else if (name == L"btn_menu")
ShowTitleMenu();
return false;
}
@@ -571,6 +714,9 @@ DownloaderForm::TaskRow DownloaderForm::CreateTaskRow(const TaskInfo& task)
ui::ListContainerElement* item = new ui::ListContainerElement;
item->SetClass(L"listitem");
item->SetAttribute(L"hotcolor", L"tt_row_hot");
item->SetAttribute(L"pushedcolor", L"tt_row_selected");
item->SetAttribute(L"selectednormalcolor", L"tt_row_selected");
item->SetFixedHeight(56);
item->AttachDoubleClick(nbase::Bind(&DownloaderForm::OnListItemDoubleClick,
this, std::placeholders::_1));
@@ -747,6 +893,7 @@ void DownloaderForm::ShowTrayMenu()
HMENU menu = ::CreatePopupMenu();
::AppendMenuW(menu, MF_STRING, 1, L"显示主界面");
::AppendMenuW(menu, MF_STRING, 3, L"设置...");
::AppendMenuW(menu, MF_SEPARATOR, 0, nullptr);
::AppendMenuW(menu, MF_STRING, 2, L"退出");
@@ -756,6 +903,158 @@ void DownloaderForm::ShowTrayMenu()
if (cmd == 1)
ActiveWindow();
else if (cmd == 3)
{
ActiveWindow();
ShowSettingsForm();
}
else if (cmd == 2)
Close();
}
void DownloaderForm::ShowTitleMenu()
{
POINT pt = {};
ui::Control* button = FindControl(L"btn_menu");
if (button != nullptr)
{
ui::UiRect rc = button->GetPos();
pt.x = rc.left;
pt.y = rc.bottom;
::ClientToScreen(GetHWND(), &pt);
}
else
{
::GetCursorPos(&pt);
}
HMENU menu = ::CreatePopupMenu();
::AppendMenuW(menu, MF_STRING, 1, L"设置...");
::AppendMenuW(menu, MF_SEPARATOR, 0, nullptr);
::AppendMenuW(menu, MF_STRING, 2, L"退出");
::SetForegroundWindow(GetHWND());
UINT cmd = ::TrackPopupMenu(menu, TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
pt.x, pt.y, 0, GetHWND(), nullptr);
::DestroyMenu(menu);
if (cmd == 1)
ShowSettingsForm();
else if (cmd == 2)
Close();
}
void DownloaderForm::ShowSettingsForm()
{
if (settings_form_ != nullptr)
{
settings_form_->ActiveWindow();
return;
}
settings_form_ = new SettingsForm(&skin_,
[this]() { ApplySkinSettings(); },
[this]() { settings_form_ = nullptr; SaveSkinSettings(); });
if (settings_form_->Create(GetHWND(), L"设置", UI_WNDSTYLE_DIALOG, 0) == nullptr)
{
delete settings_form_;
settings_form_ = nullptr;
return;
}
settings_form_->CenterWindow();
settings_form_->ShowWindow();
}
void DownloaderForm::ApplySkinSettings()
{
if (content_root_ == nullptr || task_list_ == nullptr)
return;
int alpha = BaseOpacityToAlpha(skin_.window_opacity);
if (skin_.background_image.empty())
{
content_root_->SetBkImage(std::wstring());
}
else
{
std::wstring image = L"file='" + skin_.background_image + L"' fade='";
image += nbase::StringPrintf(L"%d", alpha) + L"'";
content_root_->SetBkImage(image);
}
std::wstring color = nbase::StringPrintf(L"tt_base_%d", alpha);
ui::GlobalManager::AddTextColor(color, nbase::StringPrintf(L"#%02XF0F2F5", alpha));
content_root_->SetBkColor(color);
ApplyPanelOpacity(panel_header_, skin_.header_opacity);
ApplyPanelOpacity(panel_input_, skin_.input_opacity);
ApplyPanelOpacity(panel_table_header_, skin_.list_opacity);
ApplyPanelOpacity(task_list_, skin_.list_opacity);
ApplyPanelOpacity(panel_bottom_, skin_.bottom_opacity);
}
void DownloaderForm::LoadSkinSettings()
{
skin_ = SkinSettings();
int panel_opacity = aero_mode_ ? 70 : 100;
skin_.window_opacity = aero_mode_ ? 0 : 100;
skin_.header_opacity = panel_opacity;
skin_.input_opacity = panel_opacity;
skin_.list_opacity = panel_opacity;
skin_.bottom_opacity = panel_opacity;
std::wstring background = ReadSkinString(L"BackgroundImage");
if (!background.empty() && background.find(L'\'') == std::wstring::npos
&& ::GetFileAttributesW(background.c_str()) != INVALID_FILE_ATTRIBUTES)
skin_.background_image = background;
skin_.window_opacity = std::clamp(
static_cast<int>(ReadSkinDword(L"BaseOpacity", skin_.window_opacity)), 0, 100);
skin_.header_opacity = std::clamp(
static_cast<int>(ReadSkinDword(L"HeaderOpacity", skin_.header_opacity)), 0, 100);
skin_.input_opacity = std::clamp(
static_cast<int>(ReadSkinDword(L"InputOpacity", skin_.input_opacity)), 0, 100);
skin_.list_opacity = std::clamp(
static_cast<int>(ReadSkinDword(L"ListOpacity", skin_.list_opacity)), 0, 100);
skin_.bottom_opacity = std::clamp(
static_cast<int>(ReadSkinDword(L"BottomOpacity", skin_.bottom_opacity)), 0, 100);
}
void DownloaderForm::SaveSkinSettings()
{
HKEY key = nullptr;
if (::RegCreateKeyExW(HKEY_CURRENT_USER, kSkinRegKey, 0, nullptr, 0, KEY_SET_VALUE,
nullptr, &key, nullptr) != ERROR_SUCCESS)
return;
if (skin_.background_image.empty())
{
::RegDeleteValueW(key, L"BackgroundImage");
}
else
{
::RegSetValueExW(key, L"BackgroundImage", 0, REG_SZ,
reinterpret_cast<const BYTE*>(skin_.background_image.c_str()),
static_cast<DWORD>((skin_.background_image.size() + 1) * sizeof(wchar_t)));
}
const struct
{
const wchar_t* name;
int value;
} values[] = {
{ L"BaseOpacity", skin_.window_opacity },
{ L"HeaderOpacity", skin_.header_opacity },
{ L"InputOpacity", skin_.input_opacity },
{ L"ListOpacity", skin_.list_opacity },
{ L"BottomOpacity", skin_.bottom_opacity },
};
for (const auto& entry : values)
{
DWORD value = static_cast<DWORD>(entry.value);
::RegSetValueExW(key, entry.name, 0, REG_DWORD,
reinterpret_cast<const BYTE*>(&value), sizeof(value));
}
::RegCloseKey(key);
}
+20
View File
@@ -1,9 +1,13 @@
#pragma once
#include "aero_backdrop.h"
#include "download_engine.h"
#include "skin_settings.h"
#include <memory>
class SettingsForm;
class DownloaderForm : public ui::WindowImplBase
{
public:
@@ -63,6 +67,12 @@ private:
void RemoveTrayIcon();
void ShowTrayMenu();
void ShowTitleMenu();
void ShowSettingsForm();
void ApplySkinSettings();
void LoadSkinSettings();
void SaveSkinSettings();
NOTIFYICONDATAW tray_icon_ = {};
UINT taskbar_created_message_ = 0;
@@ -71,6 +81,16 @@ private:
ui::ListBox* task_list_ = nullptr;
ui::Label* lbl_summary_ = nullptr;
ui::Label* lbl_selected_ = nullptr;
ui::Control* content_root_ = nullptr;
ui::Control* panel_header_ = nullptr;
ui::Control* panel_input_ = nullptr;
ui::Control* panel_table_header_ = nullptr;
ui::Control* panel_bottom_ = nullptr;
SkinSettings skin_;
SettingsForm* settings_form_ = nullptr;
bool aero_mode_ = false;
AeroBackdrop aero_backdrop_;
std::vector<TaskRow> rows_;
std::unique_ptr<DownloadEngine> engine_;
+1
View File
@@ -40,6 +40,7 @@ void MainThread::Init()
std::wstring module_dir = nbase::win32::GetCurrentModuleDirectory();
ui::GlobalManager::Startup(module_dir + L"resources\\", ui::CreateControlCallback(), false);
ui::GlobalManager::AddTextColor(L"tt_edit_bg", L"#D9FFFFFF");
DownloaderForm* form = new DownloaderForm();
form->Create(NULL, DownloaderForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, 0);
+154
View File
@@ -0,0 +1,154 @@
#include "stdafx.h"
#include "settings_form.h"
#include <commdlg.h>
#include <cmath>
#pragma comment(lib, "comdlg32.lib")
const std::wstring SettingsForm::kClassName = L"TTDownloaderSettingsWindow";
SettingsForm::SettingsForm(SkinSettings* settings, ChangeCallback on_changed, CloseCallback on_closed)
: settings_(settings)
, on_changed_(std::move(on_changed))
, on_closed_(std::move(on_closed))
{
}
std::wstring SettingsForm::GetSkinFolder()
{
return L"downloader";
}
std::wstring SettingsForm::GetSkinFile()
{
return L"settings.xml";
}
std::wstring SettingsForm::GetWindowClassName() const
{
return kClassName;
}
void SettingsForm::InitWindow()
{
background_label_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_background"));
slider_window_ = dynamic_cast<ui::Slider*>(FindControl(L"slider_window"));
slider_header_ = dynamic_cast<ui::Slider*>(FindControl(L"slider_header"));
slider_input_ = dynamic_cast<ui::Slider*>(FindControl(L"slider_input"));
slider_list_ = dynamic_cast<ui::Slider*>(FindControl(L"slider_list"));
slider_bottom_ = dynamic_cast<ui::Slider*>(FindControl(L"slider_bottom"));
lbl_window_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_window"));
lbl_header_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_header"));
lbl_input_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_input"));
lbl_list_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_list"));
lbl_bottom_ = dynamic_cast<ui::Label*>(FindControl(L"lbl_bottom"));
slider_window_->SetValue(settings_->window_opacity);
slider_header_->SetValue(settings_->header_opacity);
slider_input_->SetValue(settings_->input_opacity);
slider_list_->SetValue(settings_->list_opacity);
slider_bottom_->SetValue(settings_->bottom_opacity);
UpdateBackgroundLabel();
RefreshPercentLabels();
m_pRoot->AttachBubbledEvent(ui::kEventClick, nbase::Bind(&SettingsForm::OnClicked, this, std::placeholders::_1));
ui::Slider* sliders[] = { slider_window_, slider_header_, slider_input_, slider_list_, slider_bottom_ };
for (ui::Slider* slider : sliders)
slider->AttachValueChange(nbase::Bind(&SettingsForm::OnSliderChanged, this, std::placeholders::_1));
}
void SettingsForm::OnFinalMessage(HWND hWnd)
{
if (on_closed_)
on_closed_();
__super::OnFinalMessage(hWnd);
}
bool SettingsForm::OnClicked(ui::EventArgs* args)
{
std::wstring name = args->pSender->GetName();
if (name == L"btn_pick_bg")
PickBackgroundImage();
else if (name == L"btn_clear_bg")
{
settings_->background_image.clear();
UpdateBackgroundLabel();
ApplyChanged();
}
return false;
}
bool SettingsForm::OnSliderChanged(ui::EventArgs* args)
{
std::wstring name = args->pSender->GetName();
if (name == L"slider_window")
settings_->window_opacity = static_cast<int>(std::lround(slider_window_->GetValue()));
else if (name == L"slider_header")
settings_->header_opacity = static_cast<int>(std::lround(slider_header_->GetValue()));
else if (name == L"slider_input")
settings_->input_opacity = static_cast<int>(std::lround(slider_input_->GetValue()));
else if (name == L"slider_list")
settings_->list_opacity = static_cast<int>(std::lround(slider_list_->GetValue()));
else if (name == L"slider_bottom")
settings_->bottom_opacity = static_cast<int>(std::lround(slider_bottom_->GetValue()));
else
return false;
RefreshPercentLabels();
ApplyChanged();
return false;
}
void SettingsForm::PickBackgroundImage()
{
wchar_t path[MAX_PATH] = {};
if (!settings_->background_image.empty())
wcsncpy_s(path, settings_->background_image.c_str(), _TRUNCATE);
OPENFILENAMEW dialog = {};
dialog.lStructSize = sizeof(dialog);
dialog.hwndOwner = GetHWND();
dialog.lpstrFilter = L"图片文件\0*.bmp;*.jpg;*.jpeg;*.png;*.gif\0所有文件\0*.*\0";
dialog.lpstrFile = path;
dialog.nMaxFile = MAX_PATH;
dialog.lpstrTitle = L"选择窗口背景图片";
dialog.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
if (!::GetOpenFileNameW(&dialog))
return;
std::wstring selected = path;
if (selected.find(L'\'') != std::wstring::npos)
{
::MessageBoxW(GetHWND(), L"图片路径不能包含单引号。", L"TTDownloader", MB_ICONWARNING);
return;
}
settings_->background_image = selected;
UpdateBackgroundLabel();
ApplyChanged();
}
void SettingsForm::UpdateBackgroundLabel()
{
background_label_->SetText(settings_->background_image.empty()
? L"未设置" : settings_->background_image);
}
void SettingsForm::RefreshPercentLabels()
{
lbl_window_->SetText(nbase::StringPrintf(L"%d%%", settings_->window_opacity));
lbl_header_->SetText(nbase::StringPrintf(L"%d%%", settings_->header_opacity));
lbl_input_->SetText(nbase::StringPrintf(L"%d%%", settings_->input_opacity));
lbl_list_->SetText(nbase::StringPrintf(L"%d%%", settings_->list_opacity));
lbl_bottom_->SetText(nbase::StringPrintf(L"%d%%", settings_->bottom_opacity));
}
void SettingsForm::ApplyChanged()
{
if (on_changed_)
on_changed_();
}
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include "skin_settings.h"
#include <functional>
class SettingsForm : public ui::WindowImplBase
{
public:
using ChangeCallback = std::function<void()>;
using CloseCallback = std::function<void()>;
SettingsForm(SkinSettings* settings, ChangeCallback on_changed, CloseCallback on_closed);
virtual std::wstring GetSkinFolder() override;
virtual std::wstring GetSkinFile() override;
virtual std::wstring GetWindowClassName() const override;
virtual void InitWindow() override;
virtual void OnFinalMessage(HWND hWnd) override;
static const std::wstring kClassName;
private:
bool OnClicked(ui::EventArgs* args);
bool OnSliderChanged(ui::EventArgs* args);
void PickBackgroundImage();
void UpdateBackgroundLabel();
void RefreshPercentLabels();
void ApplyChanged();
SkinSettings* settings_ = nullptr;
ChangeCallback on_changed_;
CloseCallback on_closed_;
ui::Label* background_label_ = nullptr;
ui::Slider* slider_window_ = nullptr;
ui::Slider* slider_header_ = nullptr;
ui::Slider* slider_input_ = nullptr;
ui::Slider* slider_list_ = nullptr;
ui::Slider* slider_bottom_ = nullptr;
ui::Label* lbl_window_ = nullptr;
ui::Label* lbl_header_ = nullptr;
ui::Label* lbl_input_ = nullptr;
ui::Label* lbl_list_ = nullptr;
ui::Label* lbl_bottom_ = nullptr;
};
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <string>
struct SkinSettings
{
std::wstring background_image;
int window_opacity = 100;
int header_opacity = 100;
int input_opacity = 100;
int list_opacity = 100;
int bottom_opacity = 100;
};