@@ -239,6 +239,7 @@ protected override void OnOpened(EventArgs e)
239239
240240 SetupMicaAndAccentBorder ( ) ;
241241 UpdateWindowCornerPreference ( ) ;
242+ UpdateCaptionButtonInset ( ) ;
242243
243244 ActualThemeVariantChanged += ( _ , _ ) =>
244245 {
@@ -1198,6 +1199,28 @@ private void UpdateWindowCornerPreference()
11981199 NativeMethods . DwmSetWindowAttribute ( handle , DWMWA_WINDOW_CORNER_PREFERENCE , ref corner , sizeof ( int ) ) ;
11991200 }
12001201
1202+ private void UpdateCaptionButtonInset ( )
1203+ {
1204+ if ( ! OperatingSystem . IsWindows ( ) || ! WindowButtons . IsVisible )
1205+ return ;
1206+
1207+ if ( TryGetPlatformHandle ( ) ? . Handle is not { } handle || handle == 0 )
1208+ return ;
1209+
1210+ double inset = 0 ;
1211+ if ( ! NativeMethods . IsZoomed ( handle ) )
1212+ {
1213+ uint dpi = NativeMethods . GetDpiForWindow ( handle ) ;
1214+ if ( dpi == 0 ) dpi = 96 ;
1215+ uint style = ( uint ) NativeMethods . GetWindowLongPtr ( handle , GWL_STYLE ) . ToInt64 ( ) ;
1216+ var frame = default ( NativeMethods . RECT ) ;
1217+ if ( NativeMethods . AdjustWindowRectExForDpi ( ref frame , style , false , 0 , dpi ) )
1218+ inset = frame . Right / ( dpi / 96.0 ) ;
1219+ }
1220+
1221+ WindowButtons . Margin = new Thickness ( 0 , 0 , inset , 0 ) ;
1222+ }
1223+
12011224 private void ApplyWindowBorderColor ( nint handle )
12021225 {
12031226 int color ;
@@ -1241,21 +1264,32 @@ private static nint OnWindowsWndProc(nint hWnd, uint msg, nint wParam, nint lPar
12411264 if ( ( msg == WM_SHOWWINDOW || ( msg == WM_SIZE && ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED ) ) )
12421265 && Instance is { } cornerOwner )
12431266 {
1244- Dispatcher . UIThread . Post ( cornerOwner . UpdateWindowCornerPreference ) ;
1267+ Dispatcher . UIThread . Post ( ( ) =>
1268+ {
1269+ cornerOwner . UpdateWindowCornerPreference ( ) ;
1270+ cornerOwner . UpdateCaptionButtonInset ( ) ;
1271+ } ) ;
12451272 }
12461273
12471274 if ( msg == WM_SETTINGCHANGE && Instance is { } trayOwner )
12481275 {
12491276 trayOwner . UpdateSystemTrayStatus ( ) ;
12501277 }
12511278
1279+ int borderEdge = msg == WM_NCHITTEST ? HitTestResizeBorder ( hWnd , lParam ) : HTNOWHERE ;
1280+
12521281 // ── Snap Layouts: report HTMAXBUTTON over the custom maximize button so Win11 shows the
12531282 // layout flyout, and emulate hover/press/click since input now arrives as NC messages. ──
12541283 if ( Instance is { } self && self . WindowButtons . IsVisible )
12551284 {
12561285 switch ( msg )
12571286 {
12581287 case WM_NCHITTEST :
1288+ if ( borderEdge is not HTNOWHERE and not HTTOP )
1289+ {
1290+ handled = true ;
1291+ return borderEdge ;
1292+ }
12591293 if ( self . HitTestMaximizeButton ( lParam ) )
12601294 {
12611295 handled = true ;
@@ -1308,14 +1342,10 @@ private static nint OnWindowsWndProc(nint hWnd, uint msg, nint wParam, nint lPar
13081342 }
13091343 }
13101344
1311- if ( msg == WM_NCHITTEST )
1345+ if ( borderEdge != HTNOWHERE )
13121346 {
1313- int edge = HitTestResizeBorder ( hWnd , lParam ) ;
1314- if ( edge != HTNOWHERE )
1315- {
1316- handled = true ;
1317- return edge ;
1318- }
1347+ handled = true ;
1348+ return borderEdge ;
13191349 }
13201350
13211351 // Force client = full window rect. Avalonia's ExtendClientArea handler only overrides
0 commit comments