|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using Microsoft.UI.Xaml; |
| 4 | +using static AppUIBasics.Win32; |
| 5 | + |
| 6 | +namespace WinUIGallery.Helper |
| 7 | +{ |
| 8 | + internal class Win32WindowHelper |
| 9 | + { |
| 10 | + private static WinProc newWndProc = null; |
| 11 | + private static nint oldWndProc = nint.Zero; |
| 12 | + |
| 13 | + private POINT? minWindowSize = null; |
| 14 | + private POINT? maxWindowSize = null; |
| 15 | + |
| 16 | + private readonly Window window; |
| 17 | + |
| 18 | + public Win32WindowHelper(Window window) |
| 19 | + { |
| 20 | + this.window = window; |
| 21 | + } |
| 22 | + |
| 23 | + public void SetWindowMinMaxSize(POINT? minWindowSize = null, POINT? maxWindowSize = null) |
| 24 | + { |
| 25 | + this.minWindowSize = minWindowSize; |
| 26 | + this.maxWindowSize = maxWindowSize; |
| 27 | + |
| 28 | + var hwnd = GetWindowHandleForCurrentWindow(window); |
| 29 | + |
| 30 | + newWndProc = new WinProc(WndProc); |
| 31 | + oldWndProc = SetWindowLongPtr(hwnd, WindowLongIndexFlags.GWL_WNDPROC, newWndProc); |
| 32 | + } |
| 33 | + |
| 34 | + private static nint GetWindowHandleForCurrentWindow(object target) => |
| 35 | + WinRT.Interop.WindowNative.GetWindowHandle(target); |
| 36 | + |
| 37 | + private nint WndProc(nint hWnd, WindowMessage Msg, nint wParam, nint lParam) |
| 38 | + { |
| 39 | + switch (Msg) |
| 40 | + { |
| 41 | + case WindowMessage.WM_GETMINMAXINFO: |
| 42 | + var dpi = GetDpiForWindow(hWnd); |
| 43 | + var scalingFactor = (float)dpi / 96; |
| 44 | + |
| 45 | + var minMaxInfo = Marshal.PtrToStructure<MINMAXINFO>(lParam); |
| 46 | + if (minWindowSize != null) |
| 47 | + { |
| 48 | + minMaxInfo.ptMinTrackSize.x = (int)(minWindowSize.Value.x * scalingFactor); |
| 49 | + minMaxInfo.ptMinTrackSize.y = (int)(minWindowSize.Value.y * scalingFactor); |
| 50 | + } |
| 51 | + if (maxWindowSize != null) |
| 52 | + { |
| 53 | + minMaxInfo.ptMaxTrackSize.x = (int)(maxWindowSize.Value.x * scalingFactor); |
| 54 | + minMaxInfo.ptMaxTrackSize.y = (int)(minWindowSize.Value.y * scalingFactor); |
| 55 | + } |
| 56 | + |
| 57 | + Marshal.StructureToPtr(minMaxInfo, lParam, true); |
| 58 | + break; |
| 59 | + |
| 60 | + } |
| 61 | + return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam); |
| 62 | + } |
| 63 | + |
| 64 | + private nint SetWindowLongPtr(nint hWnd, WindowLongIndexFlags nIndex, WinProc newProc) |
| 65 | + { |
| 66 | + if (nint.Size == 8) |
| 67 | + return SetWindowLongPtr64(hWnd, nIndex, newProc); |
| 68 | + else |
| 69 | + return new nint(SetWindowLong32(hWnd, nIndex, newProc)); |
| 70 | + } |
| 71 | + |
| 72 | + internal struct POINT |
| 73 | + { |
| 74 | + public int x; |
| 75 | + public int y; |
| 76 | + } |
| 77 | + |
| 78 | + [StructLayout(LayoutKind.Sequential)] |
| 79 | + private struct MINMAXINFO |
| 80 | + { |
| 81 | + public POINT ptReserved; |
| 82 | + public POINT ptMaxSize; |
| 83 | + public POINT ptMaxPosition; |
| 84 | + public POINT ptMinTrackSize; |
| 85 | + public POINT ptMaxTrackSize; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments