Fixed FontSize for Textblock inside viewbox

Sachi 221 Reputation points
2023-02-06T05:54:15.4233333+00:00

Hi

I am creating Winui3 Application I have Text block inside Viewbox the text is coming very small when i resize window ,i want fixed fontsize for my Textblock or else i need Minimum Fontsize How can i Proceed 1please suggest.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
725 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,726 Reputation points
    2023-02-06T10:56:39.2333333+00:00

    With StretchDirection="UpOnly", it seems to work :

                <Viewbox x:Name="img1" HorizontalAlignment="Center" Margin="0, 0, 0, 0" Stretch="Uniform" StretchDirection="UpOnly"
                      MinWidth="200" MinHeight="80" >
                    <TextBlock FontSize="40"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
                         HELLO
                    </TextBlock>
                </Viewbox>
    
    

    ViewBox


  2. Junjie Zhu - MSFT 14,751 Reputation points Microsoft Vendor
    2023-02-10T10:12:30.5633333+00:00

    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.

    User's image

    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.