Hello @Sachi,
Welcome to Microsoft Q&A!
As a workaround, you can set the minimum height and minimum width of the Window to prevent the fonts from being clipped.
Since WinUI3 cannot directly set this size, you need to use win32 API WM_GETMINMAXINFO in WinProc to set.
You can refer to this to set the minimum height and minimum width of the Window.
You also need install PInvoke.User32 in NuGet.
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
SubClassing();
}
private delegate IntPtr WinProc(IntPtr hWnd, PInvoke.User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam);
private WinProc newWndProc = null;
private IntPtr oldWndProc = IntPtr.Zero;
[DllImport("user32")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, PInvoke.User32.WindowLongIndexFlags nIndex, WinProc newProc);
[DllImport("user32.dll")]
static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, PInvoke.User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam);
private void SubClassing()
{
//Get the Window's HWND
var hwnd = this.As<IWindowNative>().WindowHandle;
newWndProc = new WinProc(NewWindowProc);
oldWndProc = SetWindowLong(hwnd, PInvoke.User32.WindowLongIndexFlags.GWL_WNDPROC, newWndProc);
}
int MinWidth = 800;
int MinHeight = 600;
[StructLayout(LayoutKind.Sequential)]
struct MINMAXINFO
{
public PInvoke.POINT ptReserved;
public PInvoke.POINT ptMaxSize;
public PInvoke.POINT ptMaxPosition;
public PInvoke.POINT ptMinTrackSize;
public PInvoke.POINT ptMaxTrackSize;
}
private IntPtr NewWindowProc(IntPtr hWnd, PInvoke.User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam)
{
switch (Msg)
{
case PInvoke.User32.WindowMessage.WM_GETMINMAXINFO:
var dpi = PInvoke.User32.GetDpiForWindow(hWnd);
float scalingFactor = (float)dpi / 96;
MINMAXINFO minMaxInfo = Marshal.PtrToStructure<MINMAXINFO>(lParam);
minMaxInfo.ptMinTrackSize.x = (int)(MinWidth * scalingFactor);
minMaxInfo.ptMinTrackSize.y = (int)(MinHeight * scalingFactor);
Marshal.StructureToPtr(minMaxInfo, lParam, true);
break;
}
return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
IntPtr WindowHandle { get; }
}
}
Thank you!
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.