-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Adding a minimum size for the apps window and also the TabView tearout sample for better user experience. ## Motivation and Context Closes #1181 Co-authored-by: Niels Laute <[email protected]>
- Loading branch information
Showing
5 changed files
with
145 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.UI.Xaml; | ||
using static AppUIBasics.Win32; | ||
|
||
namespace WinUIGallery.Helper | ||
{ | ||
internal class Win32WindowHelper | ||
{ | ||
private static WinProc newWndProc = null; | ||
private static nint oldWndProc = nint.Zero; | ||
|
||
private POINT? minWindowSize = null; | ||
private POINT? maxWindowSize = null; | ||
|
||
private readonly Window window; | ||
|
||
public Win32WindowHelper(Window window) | ||
{ | ||
this.window = window; | ||
} | ||
|
||
public void SetWindowMinMaxSize(POINT? minWindowSize = null, POINT? maxWindowSize = null) | ||
{ | ||
this.minWindowSize = minWindowSize; | ||
this.maxWindowSize = maxWindowSize; | ||
|
||
var hwnd = GetWindowHandleForCurrentWindow(window); | ||
|
||
newWndProc = new WinProc(WndProc); | ||
oldWndProc = SetWindowLongPtr(hwnd, WindowLongIndexFlags.GWL_WNDPROC, newWndProc); | ||
} | ||
|
||
private static nint GetWindowHandleForCurrentWindow(object target) => | ||
WinRT.Interop.WindowNative.GetWindowHandle(target); | ||
|
||
private nint WndProc(nint hWnd, WindowMessage Msg, nint wParam, nint lParam) | ||
{ | ||
switch (Msg) | ||
{ | ||
case WindowMessage.WM_GETMINMAXINFO: | ||
var dpi = GetDpiForWindow(hWnd); | ||
var scalingFactor = (float)dpi / 96; | ||
|
||
var minMaxInfo = Marshal.PtrToStructure<MINMAXINFO>(lParam); | ||
if (minWindowSize != null) | ||
{ | ||
minMaxInfo.ptMinTrackSize.x = (int)(minWindowSize.Value.x * scalingFactor); | ||
minMaxInfo.ptMinTrackSize.y = (int)(minWindowSize.Value.y * scalingFactor); | ||
} | ||
if (maxWindowSize != null) | ||
{ | ||
minMaxInfo.ptMaxTrackSize.x = (int)(maxWindowSize.Value.x * scalingFactor); | ||
minMaxInfo.ptMaxTrackSize.y = (int)(minWindowSize.Value.y * scalingFactor); | ||
} | ||
|
||
Marshal.StructureToPtr(minMaxInfo, lParam, true); | ||
break; | ||
|
||
} | ||
return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam); | ||
} | ||
|
||
private nint SetWindowLongPtr(nint hWnd, WindowLongIndexFlags nIndex, WinProc newProc) | ||
{ | ||
if (nint.Size == 8) | ||
return SetWindowLongPtr64(hWnd, nIndex, newProc); | ||
else | ||
return new nint(SetWindowLong32(hWnd, nIndex, newProc)); | ||
} | ||
|
||
internal struct POINT | ||
{ | ||
public int x; | ||
public int y; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
private struct MINMAXINFO | ||
{ | ||
public POINT ptReserved; | ||
public POINT ptMaxSize; | ||
public POINT ptMaxPosition; | ||
public POINT ptMinTrackSize; | ||
public POINT ptMaxTrackSize; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters