Magpie/tools/WindowCase/HideCursorWindow.cpp
Xu 74d26e4a0d
正在缩放窗口时禁止自动缩放它的弹窗 (#1276)
* feat(tool): Kirikiri 模拟添加弹窗

* fix: 禁止同一进程内类名相同的窗口打断缩放

* fix: 正在缩放窗口时禁止自动缩放它的弹窗

* fix: 增加两个窗口位于同一进程的条件

* chore: 添加注释
2025-08-26 20:02:26 +08:00

97 lines
2.2 KiB
C++

#include "pch.h"
#include "HideCursorWindow.h"
#include "Utils.h"
bool HideCursorWindow::Create() noexcept {
static const wchar_t* WINDOW_NAME = L"HideCursorWindow";
WNDCLASSEXW wcex{
.cbSize = sizeof(WNDCLASSEX),
.style = CS_HREDRAW | CS_VREDRAW,
.lpfnWndProc = _WndProc,
.hInstance = Utils::GetModuleInstanceHandle(),
.hCursor = LoadCursor(nullptr, IDC_ARROW),
.hbrBackground = HBRUSH(COLOR_WINDOW + 1),
.lpszClassName = WINDOW_NAME
};
if (!RegisterClassEx(&wcex)) {
return false;
}
CreateWindow(
WINDOW_NAME,
WINDOW_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
Utils::GetModuleInstanceHandle(),
this
);
if (!Handle()) {
return false;
}
const double dpiScale = _DpiScale();
SetWindowPos(Handle(), NULL, 0, 0,
std::lround(500 * dpiScale), std::lround(400 * dpiScale),
SWP_NOMOVE | SWP_SHOWWINDOW);
return true;
}
LRESULT HideCursorWindow::_MessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noexcept {
switch (msg) {
case WM_CREATE:
{
const LRESULT ret = base_type::_MessageHandler(msg, wParam, lParam);
_hwndBtn = CreateWindow(L"BUTTON", L"未隐藏光标", WS_CHILD | WS_VISIBLE,
0, 0, 0, 0, Handle(), (HMENU)1, Utils::GetModuleInstanceHandle(), 0);
_UpdateButtonPos();
SendMessage(_hwndBtn, WM_SETFONT, (WPARAM)_UIFont(), TRUE);
return ret;
}
case WM_SIZE:
{
_UpdateButtonPos();
break;
}
case WM_COMMAND:
{
if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == 1) {
ShowCursor(_isCursorHidden);
SetWindowText(_hwndBtn, _isCursorHidden ? L"未隐藏光标" : L"已隐藏光标");
_isCursorHidden = !_isCursorHidden;
return 0;
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return base_type::_MessageHandler(msg, wParam, lParam);
}
void HideCursorWindow::_UpdateButtonPos() noexcept {
RECT clientRect;
GetClientRect(Handle(), &clientRect);
const double dpiScale = _DpiScale();
const SIZE btnSize = { std::lround(120 * dpiScale),std::lround(50 * dpiScale) };
SetWindowPos(
_hwndBtn,
NULL,
((clientRect.right - clientRect.left) - btnSize.cx) / 2,
((clientRect.bottom - clientRect.top) - btnSize.cy) / 2,
btnSize.cx,
btnSize.cy,
SWP_NOACTIVATE
);
}