August 30th, 2023

How can I programmatically obtain the value of the “Make text bigger” slider?

The XAML UI framework automatically honors the Windows “Make text bigger” setting, but what if you’re using some other framework and want to respect the text scale factor?

You can query the value of the “Make text bigger” slider from the Windows.UI.ViewManagement.UISettings.TextScaleFactor property, and then incorporate that value into your font size calculations.¹

// C#
double GetTextScaleFactor()
{
    return (new Windows.UI.
                ViewManagement.UISettings()).TextScaleFactor;
}

// C++/WinRT
#include <winrt/Windows.UI.ViewManagement.h>

double GetTextScaleFactor()
{
    return winrt::Windows::UI::
           ViewManagement::UISettings().TextScaleFactor();
}

// C++/CX

double GetTextScaleFactor()
{
    return (ref new Windows::UI::
        ViewManagement::UISettings())->TextScaleFactor;
}

// C++/WRL
#include <Windows.UI.ViewManagement.h>

HRESULT GetTextScaleFactor(double* value)
{
    *value = 1.0;

    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Wrappers;

    ComPtr<IInspectable> instance;
    HRESULT hr = RoActivateInstance(HStringReference(
        RuntimeClass_Windows_UI_ViewManagement_UISettings).Get(),
        &instance);
    if (FAILED(hr)) return hr;

    ComPtr<ABI::Windows::UI::ViewManagement::IUISettings2>
        settings2;
    hr = instance.As(&settings2);
    if (FAILED(hr)) return hr;

    hr = settings2->get_TextScaleFactor(value);
    if (FAILED(hr)) return hr;

    return S_OK;
}

You can also subscribe to the Text­Scale­Factor­Changed event to be notified when the text scale factor changes.

¹ Note that the text scale factor is not a blind scaling applied to all fonts. It generally applies to your main body font, and the scaling applied to other fonts diminishes based on the distance of those other fonts from the body font. For example, chapter headings should not be magnified by as large a factor as body text.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Discussion is closed. Login to edit/delete existing comments.

    • LB

      C++/WinRT, despite the name, is pure modern C++17 and works with any compiler.