Skip to content

Commit 3b8729c

Browse files
committed
Fix #2066
On Windows 8 a window with height 23px resizes to 39px after dragging
1 parent eb82f8e commit 3b8729c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

docs/release-notes/1.5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
- [#2871](https://github.com/MahApps/MahApps.Metro/issues/2871) SplitButton CornerRadius
3030
- [#2862](https://github.com/MahApps/MahApps.Metro/issues/2862) Flyout close button icon is incorrect when setting flow direction right to left
3131
- [#1938](https://github.com/MahApps/MahApps.Metro/issues/1938) Problem with window size
32+
- [#2066](https://github.com/MahApps/MahApps.Metro/issues/2066) On Windows 8 a window with height 23px resizes to 39px after dragging

src/MahApps.Metro/MahApps.Metro.Shared/Behaviours/BorderlessWindowBehavior.cs

+40-2
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,44 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
281281
returnval = UnsafeNativeMethods.DefWindowProc(hwnd, msg, wParam, new IntPtr(-1));
282282
handled = true;
283283
break;
284+
case (int)Standard.WM.WINDOWPOSCHANGING:
285+
{
286+
var pos = (Standard.WINDOWPOS)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(Standard.WINDOWPOS));
287+
if ((pos.flags & (int)Standard.SWP.NOMOVE) != 0)
288+
{
289+
return IntPtr.Zero;
290+
}
291+
292+
var wnd = this.AssociatedObject;
293+
if (wnd == null || this.hwndSource?.CompositionTarget == null)
294+
{
295+
return IntPtr.Zero;
296+
}
297+
298+
bool changedPos = false;
299+
300+
// Convert the original to original size based on DPI setting. Need for x% screen DPI.
301+
var matrix = this.hwndSource.CompositionTarget.TransformToDevice;
302+
303+
var minWidth = wnd.MinWidth * matrix.M11;
304+
var minHeight = wnd.MinHeight * matrix.M22;
305+
if (pos.cx < minWidth) { pos.cx = (int)minWidth; changedPos = true; }
306+
if (pos.cy < minHeight) { pos.cy = (int)minHeight; changedPos = true; }
307+
308+
var maxWidth = wnd.MaxWidth * matrix.M11;
309+
var maxHeight = wnd.MaxHeight * matrix.M22;
310+
if (pos.cx > maxWidth && maxWidth > 0) { pos.cx = (int)Math.Round(maxWidth); changedPos = true; }
311+
if (pos.cy > maxHeight && maxHeight > 0) { pos.cy = (int)Math.Round(maxHeight); changedPos = true; }
312+
313+
if (!changedPos)
314+
{
315+
return IntPtr.Zero;
316+
}
317+
318+
System.Runtime.InteropServices.Marshal.StructureToPtr(pos, lParam, true);
319+
handled = true;
320+
}
321+
break;
284322
}
285323

286324
return returnval;
@@ -319,7 +357,7 @@ private void HandleMaximize()
319357
IntPtr monitor = UnsafeNativeMethods.MonitorFromWindow(this.handle, Constants.MONITOR_DEFAULTTONEAREST);
320358
if (monitor != IntPtr.Zero)
321359
{
322-
var monitorInfo = new MONITORINFO();
360+
var monitorInfo = new Native.MONITORINFO();
323361
UnsafeNativeMethods.GetMonitorInfo(monitor, monitorInfo);
324362

325363
var desktopRect = ignoreTaskBar ? monitorInfo.rcMonitor : monitorInfo.rcWork;
@@ -433,7 +471,7 @@ private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
433471
this.AssociatedObject.Invoke(() =>
434472
{
435473
this.AssociatedObject.InvalidateMeasure();
436-
RECT rect;
474+
Native.RECT rect;
437475
if (UnsafeNativeMethods.GetWindowRect(this.handle, out rect))
438476
{
439477
UnsafeNativeMethods.SetWindowPos(this.handle, new IntPtr(-2), rect.left, rect.top, rect.Width, rect.Height, 0x0040);

0 commit comments

Comments
 (0)