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

Raymond Chen

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.

2 comments

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

    • LB 0

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

Feedback usabilla icon