Skip to content

Commit 5cea64e

Browse files
Desktop: Fix window extending outside the screen on windows when maximized
1 parent d15f63f commit 5cea64e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

desktop/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ windows = { version = "0.58.0", features = [
6060
"Win32_System_LibraryLoader",
6161
"Win32_UI_Controls",
6262
"Win32_UI_WindowsAndMessaging",
63+
"Win32_UI_HiDpi",
6364
], optional = true }
6465

6566
# macOS-specific dependencies

desktop/src/native_window/windows.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use std::sync::OnceLock;
1313
use wgpu::rwh::{HasWindowHandle, RawWindowHandle};
1414
use windows::Win32::Foundation::*;
15-
use windows::Win32::Graphics::{Dwm::*, Gdi::HBRUSH};
15+
use windows::Win32::Graphics::Dwm::*;
16+
use windows::Win32::Graphics::Gdi::*;
1617
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
1718
use windows::Win32::UI::Controls::MARGINS;
19+
use windows::Win32::UI::HiDpi::*;
1820
use windows::Win32::UI::WindowsAndMessaging::*;
1921
use windows::core::PCWSTR;
2022
use winit::window::Window;
@@ -173,11 +175,24 @@ unsafe fn ensure_helper_class() {
173175

174176
// Main window message handler, called on the UI thread for every message the main window receives.
175177
unsafe extern "system" fn main_window_handle_message(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
176-
if msg == WM_NCCALCSIZE {
177-
if wparam.0 != 0 {
178-
// Return 0 to to tell Windows to skip the default non-client area calculation and drawing.
179-
return LRESULT(0);
178+
if msg == WM_NCCALCSIZE && wparam.0 != 0 {
179+
// When maximized, shrink to visible frame so content doesn't extend beyond it.
180+
if unsafe { IsZoomed(hwnd).as_bool() } {
181+
let params = unsafe { &mut *(lparam.0 as *mut NCCALCSIZE_PARAMS) };
182+
183+
let dpi = unsafe { GetDpiForWindow(hwnd) };
184+
let size = unsafe { GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) };
185+
let pad = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) };
186+
let inset = (size + pad) as i32;
187+
188+
params.rgrc[0].left += inset;
189+
params.rgrc[0].top += inset;
190+
params.rgrc[0].right -= inset;
191+
params.rgrc[0].bottom -= inset;
180192
}
193+
194+
// Return 0 to to tell Windows to skip the default non-client area calculation and drawing.
195+
return LRESULT(0);
181196
}
182197

183198
let Some(handle) = registry::find_by_main(hwnd) else {

0 commit comments

Comments
 (0)