Files

393 lines
10 KiB
C++
Raw Permalink Normal View History

2024-02-06 01:11:59 +08:00
/**
* FileName: CustomButton.cpp
*
* Copyright (C) 2024 Maplespe
*
* This file is part of MToolBox and DWMBlurGlass.
* DWMBlurGlass is free software: you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation, either version 3
* of the License, or any later version.
*
* DWMBlurGlass is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with Foobar.
* If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
#include "CustomButton.h"
#include "../DWMBlurGlass.h"
2024-04-27 21:42:40 +08:00
#include "../Backdrops/ButtonGlowBackdrop.hpp"
2024-02-06 01:11:59 +08:00
#include <shellscalingapi.h>
2025-12-05 16:49:54 +08:00
#include <algorithm>
2025-01-08 14:31:32 +08:00
//#include <mutex>
2024-02-06 01:11:59 +08:00
#pragma comment(lib, "shcore.lib")
namespace MDWMBlurGlassExt::CustomButton
{
using namespace MDWMBlurGlass;
using namespace CommonDef;
using namespace DWM;
std::atomic_bool g_startup = false;
2024-04-27 21:42:40 +08:00
struct buttonData
{
std::unordered_map<CButton*, std::pair<int, RECT>> buttonList;
};
2025-01-08 14:31:32 +08:00
std::unordered_map<CTopLevelWindow*, buttonData> g_cbuttonList;
2024-11-27 10:46:05 +08:00
std::unordered_map<CTopLevelWindow*, com_ptr<CButtonGlowBackdrop>> g_glowbackdropMap{};
2025-01-08 14:31:32 +08:00
//std::mutex g_dslock;
2024-04-27 21:42:40 +08:00
inline auto FindWindowFromButton(CButton* btn)
{
2025-01-07 11:09:33 +08:00
for (auto wnditer = g_cbuttonList.begin(); wnditer != g_cbuttonList.end(); ++wnditer)
2024-04-27 21:42:40 +08:00
{
2025-01-07 11:09:33 +08:00
if (auto iter = wnditer->second.buttonList.find(btn); iter != wnditer->second.buttonList.end())
2024-04-27 21:42:40 +08:00
return wnditer;
}
return g_cbuttonList.end();
}
2024-02-06 01:11:59 +08:00
2024-11-27 10:46:05 +08:00
com_ptr<CButtonGlowBackdrop> GetOrCreateGlow(CTopLevelWindow* target, bool createIfNecessary)
{
auto it{ g_glowbackdropMap.find(target) };
if (createIfNecessary)
{
if (it == g_glowbackdropMap.end())
{
auto visual = target->GetVisual();
auto result{ g_glowbackdropMap.emplace(target, winrt::make_self<CButtonGlowBackdrop>(visual)) };
if (result.second == true)
{
it = result.first;
}
}
}
return it == g_glowbackdropMap.end() ? nullptr : it->second;
}
void RemoveGlow(CTopLevelWindow* target)
{
auto it{ g_glowbackdropMap.find(target) };
if (it != g_glowbackdropMap.end())
{
g_glowbackdropMap.erase(it);
}
}
2024-02-06 01:11:59 +08:00
MinHook g_funCButton_UpdateLayout
{
"CButton::UpdateLayout", CButton_UpdateLayout
};
2024-04-27 21:42:40 +08:00
MinHook g_funCButton_Destructor
{
"CButton::~CButton", CButton_Destructor
};
2024-11-27 10:46:05 +08:00
MinHook g_funCButton_RedrawVisual
2024-04-27 21:42:40 +08:00
{
2024-11-27 10:46:05 +08:00
"CButton::RedrawVisual", CButton_RedrawVisual
2024-04-27 21:42:40 +08:00
};
2024-02-06 01:11:59 +08:00
void Attach()
{
if (g_startup) return;
g_startup = true;
g_funCButton_UpdateLayout.Attach();
2024-04-27 21:42:40 +08:00
g_funCButton_Destructor.Attach();
g_CTopLevelWindow_CTopLevelWindow_Destructor_HookDispatcher.enable_hook_routine<1, true>();
2024-02-07 20:41:51 +08:00
g_CTopLevelWindow_ValidateVisual_HookDispatcher.enable_hook_routine<1, true>();
2024-11-27 10:46:05 +08:00
g_funCButton_RedrawVisual.Attach();
2024-02-06 01:11:59 +08:00
}
void Detach()
{
if (!g_startup) return;
2024-02-07 20:41:51 +08:00
g_CTopLevelWindow_ValidateVisual_HookDispatcher.enable_hook_routine<1, false>();
2024-02-06 01:11:59 +08:00
g_funCButton_UpdateLayout.Detach();
2024-04-27 21:42:40 +08:00
g_CTopLevelWindow_CTopLevelWindow_Destructor_HookDispatcher.enable_hook_routine<1, false>();
2024-11-27 10:46:05 +08:00
g_funCButton_RedrawVisual.Detach();
2024-02-06 01:11:59 +08:00
g_cbuttonList.clear();
2024-11-27 10:46:05 +08:00
g_glowbackdropMap.clear();
2024-04-27 21:42:40 +08:00
g_funCButton_Destructor.Detach();
2024-02-06 01:11:59 +08:00
g_startup = false;
}
void Refresh()
{
2024-11-27 10:46:05 +08:00
if (!g_startup
&& (
(g_configData.blurmethod != blurMethod::DWMAPIBlur && g_configData.oldBtnHeight)
|| (g_configData.blurmethod == blurMethod::CustomBlur && g_configData.titlebtnGlow)
2025-01-07 11:09:33 +08:00
))
2024-02-06 01:11:59 +08:00
Attach();
2024-11-27 10:46:05 +08:00
else if (g_startup
&& ((!g_configData.oldBtnHeight && !g_configData.titlebtnGlow) || g_configData.blurmethod == blurMethod::DWMAPIBlur)
)
2024-02-06 01:11:59 +08:00
Detach();
}
HRESULT CTopLevelWindow_ValidateVisual(CTopLevelWindow* This)
{
auto data = This->GetData();
2024-11-27 10:46:05 +08:00
if ((!g_configData.oldBtnHeight && !g_configData.titlebtnGlow) || !data)
2024-02-06 01:11:59 +08:00
return S_OK;
g_window = data->GetHWND();
2025-12-05 16:49:54 +08:00
if (!g_window)
2024-02-06 01:11:59 +08:00
return S_OK;
2025-12-05 16:49:54 +08:00
bool isToolWindow = GetWindowLongPtrW(g_window, GWL_EXSTYLE) & WS_EX_TOOLWINDOW;
2024-04-27 21:42:40 +08:00
auto GetButtonPtr = [&](int index) -> CButton*
2024-02-06 01:11:59 +08:00
{
CButton** button;
if (os::buildNumber < 22000)
button = (CButton**)((DWORD64)This + 8 * (index + 61));
else if (os::buildNumber < 22621)
button = (CButton**)((DWORD64)This + 8 * (index + 63));
2024-11-27 10:46:05 +08:00
else if (os::buildNumber < 26100)
2024-02-06 01:11:59 +08:00
button = (CButton**)((DWORD64)This + 8 * (index + 66));
2024-11-27 10:46:05 +08:00
else
button = (CButton**)((DWORD64)This + 488 + 8 * index);
2024-02-06 01:11:59 +08:00
if (button && *button)
2024-04-27 21:42:40 +08:00
return *button;
return nullptr;
};
auto PushButtonPtr = [&](int index, int x, int width, int height)
{
if (auto button = GetButtonPtr(index))
2024-02-06 01:11:59 +08:00
{
2025-12-19 16:42:23 +08:00
if (isToolWindow)
{
width = height = -1;
}
2024-04-27 21:42:40 +08:00
g_cbuttonList[This].buttonList[button] = std::make_pair(index, RECT{ x, 0, width, height });
2024-11-27 10:46:05 +08:00
2025-12-19 16:42:23 +08:00
// 不要修改工具栏窗口的按钮 它应该是固定大小和偏移
if (!g_configData.oldBtnHeight || isToolWindow) return true;
2024-11-27 10:46:05 +08:00
if (os::buildNumber < 22000 || os::buildNumber >= 26100)
2024-04-26 21:38:17 +08:00
{
DWORD right = x;
2024-04-27 21:42:40 +08:00
This->UpdateNCAreaButton(index, height, button->GetPoint()->y, &right);
2024-04-26 21:38:17 +08:00
}
else
2024-04-27 21:42:40 +08:00
((CVisual*)button)->SetInsetFromParentRight(x);
2024-02-06 01:11:59 +08:00
return true;
}
return false;
};
RECT rect;
This->GetActualWindowRect(&rect, 0, 0, false);
auto monitor = MonitorFromPoint({ max(0, rect.left), max(0, rect.top) }, 0);
UINT dpi = 96;// CDesktopManager::s_pDesktopManagerInstance->MonitorDpiFromPoint({ rect.left, rect.top });
GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpi, &dpi);
2024-04-26 21:38:17 +08:00
const float scale = (float)dpi / 96.f;
2024-02-06 01:11:59 +08:00
RECT client;
This->GetActualWindowRect(&client, 0, 0, true);
2024-02-06 13:37:11 +08:00
if (IsIconic(g_window))
return S_OK;
2024-02-06 01:11:59 +08:00
int borderW = ((rect.right - rect.left) - (client.right - client.left)) / 2;
2024-04-26 21:38:17 +08:00
if (scale != 1.f)
borderW -= (int)ceil(1.1f * scale);
2025-01-07 11:09:33 +08:00
if (os::buildNumber < 22000 && borderW <= 0)
2024-02-06 01:11:59 +08:00
{
2024-04-26 21:38:17 +08:00
borderW = 6;
if (IsZoomed(g_window))
borderW = 10;
2024-02-06 01:11:59 +08:00
}
OffsetRect(&rect, -rect.left, -rect.top);
2024-04-26 21:38:17 +08:00
if (g_configData.titlebtnOffsetX != -1)
borderW = g_configData.titlebtnOffsetX;
2024-02-07 20:41:51 +08:00
2024-04-26 21:38:17 +08:00
borderW = int((float)borderW * scale);
2025-12-05 16:49:54 +08:00
// 如果启用了自定义尺寸 使用配置中的按钮尺寸
// 默认使用 Windows7 的数值尺寸
int minbtn_width_base = 29;
int maxbtn_width_base = 27;
int closebtn_width_base = 49;
int frameY = 20;
if (g_configData.customTitleBtnSize)
{
closebtn_width_base = std::clamp(g_configData.customCloseBtnW, 14, 200);
maxbtn_width_base = std::clamp(g_configData.customMaxBtnW, 14, 200);
minbtn_width_base = std::clamp(g_configData.customMinBtnW, 14, 200);
frameY = std::clamp(g_configData.customBtnFrameH, 14, 100);
}
const int minbtn_width = int((float)minbtn_width_base * scale);
const int maxbtn_width = int((float)maxbtn_width_base * scale);
const int closebtn_width = int((float)closebtn_width_base * scale);
2024-02-06 01:11:59 +08:00
const int height = int((float)frameY * scale);
2024-04-27 21:42:40 +08:00
bool closebtn = false;
2024-04-26 21:38:17 +08:00
bool maxbtn = false;
bool minbtn = false;
2024-02-06 01:11:59 +08:00
int offset = rect.right - borderW;
//关闭按钮
2024-04-26 21:38:17 +08:00
if (PushButtonPtr(3, rect.right - closebtn_width - borderW, closebtn_width, height))
2024-04-27 21:42:40 +08:00
{
2024-04-26 21:38:17 +08:00
offset -= closebtn_width + maxbtn_width;
2024-04-27 21:42:40 +08:00
closebtn = true;
}
2024-02-06 01:11:59 +08:00
//最大化按钮
2024-04-26 21:38:17 +08:00
if (PushButtonPtr(2, offset, maxbtn_width, height))
{
offset -= minbtn_width;
maxbtn = true;
}
2024-02-06 01:11:59 +08:00
//最小化按钮
2024-04-26 21:38:17 +08:00
if (PushButtonPtr(1, offset, minbtn_width, height))
{
offset -= minbtn_width;
minbtn = true;
}
2024-02-06 01:11:59 +08:00
//帮助按钮
2024-04-26 21:38:17 +08:00
if (!maxbtn && !minbtn)
offset -= int(2.f * scale);
PushButtonPtr(0, offset, minbtn_width, height);
2024-11-27 10:46:05 +08:00
if (!g_configData.titlebtnGlow) return S_OK;
//按钮发光
2024-04-27 21:42:40 +08:00
if (CWindowData* windowData = This->GetData())
{
com_ptr<CButtonGlowBackdrop> backdrop{ nullptr };
if (
This->HasNonClientBackground() &&
AccentBlur::CheckWindowType(windowData->GetHWND())
)
{
if (closebtn)
2024-11-27 10:46:05 +08:00
backdrop = GetOrCreateGlow(This, true);
else if ((backdrop = GetOrCreateGlow(This, false)))
RemoveGlow(This);
2024-04-27 21:42:40 +08:00
auto closebtnptr = GetButtonPtr(3);
if (backdrop && closebtnptr)
{
backdrop->Update(3, *closebtnptr->GetPoint(), *closebtnptr->GetSize(), scale, 0.f);
}
}
}
2024-02-06 01:11:59 +08:00
return S_OK;
}
2024-11-27 10:46:05 +08:00
void CTopLevelWindow_Destructor(CTopLevelWindow* This)
2024-04-27 21:42:40 +08:00
{
2024-11-27 10:46:05 +08:00
if (g_configData.titlebtnGlow)
2024-04-27 21:42:40 +08:00
{
2024-11-27 10:46:05 +08:00
RemoveGlow(This);
2024-04-27 21:42:40 +08:00
}
2025-01-08 14:31:32 +08:00
//std::lock_guard lock{ g_dslock };
2024-04-27 21:42:40 +08:00
if (auto iter = g_cbuttonList.find(This); iter != g_cbuttonList.end())
2025-01-07 11:09:33 +08:00
{
2024-04-27 21:42:40 +08:00
g_cbuttonList.erase(iter);
2025-01-07 11:09:33 +08:00
}
2024-04-27 21:42:40 +08:00
}
void CButton_Destructor(CButton* This)
{
g_funCButton_Destructor.call_org(This);
2024-11-27 10:46:05 +08:00
2024-04-27 21:42:40 +08:00
if (auto iter = FindWindowFromButton(This); iter != g_cbuttonList.end())
2024-11-27 10:46:05 +08:00
{
2024-04-27 21:42:40 +08:00
iter->second.buttonList.erase(This);
2024-11-27 10:46:05 +08:00
}
2024-04-27 21:42:40 +08:00
}
2024-02-06 01:11:59 +08:00
HRESULT CButton_UpdateLayout(CButton* This)
{
if (g_configData.oldBtnHeight)
{
POINT* pt = This->GetPoint();
SIZE* size = This->GetSize();
2024-04-27 21:42:40 +08:00
auto iter = FindWindowFromButton(This);
2024-02-06 01:11:59 +08:00
if (iter == g_cbuttonList.end())
return g_funCButton_UpdateLayout.call_org(This);
2024-04-27 21:42:40 +08:00
auto& data = iter->second.buttonList[This].second;
2025-12-19 16:42:23 +08:00
// 如果宽高为 -1 则表示使用默认尺寸(工具窗口按钮) 不进行修改
if ((data.right == -1 && data.bottom == -1))
{
return g_funCButton_UpdateLayout.call_org(This);
}
2024-04-27 21:42:40 +08:00
size->cx = data.right;
size->cy = data.bottom;
2024-02-06 01:11:59 +08:00
auto ret = g_funCButton_UpdateLayout.call_org(This);
2024-04-27 21:42:40 +08:00
pt->x = data.left;
2024-02-06 01:11:59 +08:00
return ret;
}
return g_funCButton_UpdateLayout.call_org(This);
}
2024-04-27 21:42:40 +08:00
2024-11-27 10:46:05 +08:00
HRESULT CButton_RedrawVisual(CButton* This)
2024-04-27 21:42:40 +08:00
{
2024-11-27 10:46:05 +08:00
auto hr = g_funCButton_RedrawVisual.call_org(This);
if (!g_configData.titlebtnGlow) return hr;
2024-04-27 21:42:40 +08:00
2025-01-08 14:31:32 +08:00
//std::lock_guard lock{ g_dslock };
2024-04-27 21:42:40 +08:00
auto iter = FindWindowFromButton(This);
if (iter == g_cbuttonList.end())
return hr;
2025-01-07 11:09:33 +08:00
auto cwnd = iter->first;
2024-04-27 21:42:40 +08:00
RECT rect;
2025-01-07 11:09:33 +08:00
cwnd->GetActualWindowRect(&rect, 0, 0, false);
2024-04-27 21:42:40 +08:00
auto monitor = MonitorFromPoint({ max(0, rect.left), max(0, rect.top) }, 0);
UINT dpi = 96;
GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpi, &dpi);
const float scale = (float)dpi / 96.f;
2025-01-07 11:09:33 +08:00
if (auto backdrop = GetOrCreateGlow(cwnd, false))
2024-04-27 21:42:40 +08:00
{
auto& data = iter->second.buttonList[This];
2024-11-27 10:46:05 +08:00
int state = 0;
if (os::buildNumber >= 26100)
state = *(int*)((char*)This + 328);
else
state = *(int*)((char*)This + 376);
2024-05-01 00:37:01 +08:00
//OutputDebugStringW((L"state:" + std::to_wstring(state) + L"\n").c_str());
2024-11-27 10:46:05 +08:00
SIZE size = { data.second.right, data.second.bottom };
2025-12-19 16:42:23 +08:00
if (!g_configData.oldBtnHeight || (size.cx == -1 && size.cy == -1))
2024-11-27 10:46:05 +08:00
size = *This->GetSize();
backdrop->Update(data.first, *This->GetPoint(), size, scale, state == 2 || state == 1);
2024-04-27 21:42:40 +08:00
}
return hr;
}
2024-02-06 01:11:59 +08:00
}