From 46303c976c17c8228a9b79092a3f3cee8a9efd66 Mon Sep 17 00:00:00 2001 From: Xu <34770031+Blinue@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:36:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E8=B4=B4=E9=9D=A0?= =?UTF-8?q?=E5=B8=83=E5=B1=80=E5=BC=B9=E7=AA=97=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Magpie/MainWindow.cpp | 33 ++++++++++++++++++++++++++++++++- src/Magpie/MainWindow.h | 1 + 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Magpie/MainWindow.cpp b/src/Magpie/MainWindow.cpp index 9a26b1fa7..381fd0d7c 100644 --- a/src/Magpie/MainWindow.cpp +++ b/src/Magpie/MainWindow.cpp @@ -123,6 +123,20 @@ bool MainWindow::Create(HINSTANCE hInstance, const RECT& windowRect, bool isMaxi ); SetLayeredWindowAttributes(_hwndTitleBar, 0, 255, LWA_ALPHA); + if (Win32Utils::GetOSVersion().IsWin11()) { + // 如果鼠标正位于一个按钮上,贴靠布局弹窗会出现在按钮下方。我们利用这个特性来修正贴靠布局弹窗的位置 + _hwndMaximizeButton = CreateWindow( + L"BUTTON", + L"", + WS_VISIBLE | WS_CHILD | WS_DISABLED | BS_OWNERDRAW, + 0, 0, 0, 0, + _hwndTitleBar, + NULL, + hInstance, + NULL + ); + } + _content.TitleBar().SizeChanged([this](winrt::IInspectable const&, winrt::SizeChangedEventArgs const&) { _ResizeTitleBarWindow(); }); @@ -233,6 +247,11 @@ LRESULT MainWindow::_TitleBarWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM LRESULT MainWindow::_TitleBarMessageHandler(UINT msg, WPARAM wParam, LPARAM lParam) noexcept { switch (msg) { + case WM_CTLCOLORBTN: + { + // 使原生按钮控件透明,虽然整个标题栏窗口都是不可见的 + return NULL; + } case WM_NCHITTEST: { POINT cursorPos{ GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam) }; @@ -427,16 +446,28 @@ void MainWindow::_ResizeTitleBarWindow() noexcept { const float dpiScale = _currentDpi / float(USER_DEFAULT_SCREEN_DPI); // 将标题栏窗口置于 XAML Islands 窗口上方 + const int titleBarWidth = (int)std::ceilf(rect.Width * dpiScale); SetWindowPos( _hwndTitleBar, HWND_TOP, (int)std::floorf(rect.X * dpiScale), (int)std::floorf(rect.Y * dpiScale) + _GetTopBorderHeight(), - (int)std::ceilf(rect.Width * dpiScale), + titleBarWidth, (int)std::floorf(rect.Height * dpiScale + 1), // 不知为何,直接向上取整有时无法遮盖 TitleBarControl SWP_SHOWWINDOW ); + if (_hwndMaximizeButton) { + static const float captionButtonHeightInDips = [&]() { + return titleBar.CaptionButtons().CaptionButtonSize().Height; + }(); + + const int captionButtonHeightInPixels = (int)std::ceilf(captionButtonHeightInDips * dpiScale); + + // 确保原生按钮和标题栏按钮高度相同 + MoveWindow(_hwndMaximizeButton, 0, 0, titleBarWidth, captionButtonHeightInPixels, FALSE); + } + // 设置标题栏窗口的最大化样式,这样才能展示正确的文字提示 LONG_PTR style = GetWindowLongPtr(_hwndTitleBar, GWL_STYLE); SetWindowLongPtr(_hwndTitleBar, GWL_STYLE, diff --git a/src/Magpie/MainWindow.h b/src/Magpie/MainWindow.h index 54dd10eba..7972ac8b2 100644 --- a/src/Magpie/MainWindow.h +++ b/src/Magpie/MainWindow.h @@ -24,6 +24,7 @@ class MainWindow : public XamlWindowT void _ResizeTitleBarWindow() noexcept; HWND _hwndTitleBar = NULL; + HWND _hwndMaximizeButton = NULL; bool _trackingMouse = false; };