How can I programmatically retrieve text scaling value of Windows in WPF?
Asked
Active
Viewed 462 times
0
-
1Why do you need to get it? Shouldn't WPF take care of it for you? https://stackoverflow.com/questions/9373260/detect-windows-font-size-100-125-and-150 – gunr2171 Aug 18 '21 at 12:20
-
I need this value @gunr2171, I believe it's possible in UWP, unfortunatelly I need it for WPF and I can't find a way to get it. – Themelis Aug 18 '21 at 12:30
2 Answers
2
Depending on whether you target .NET 5 or an earlier version, you should either set the TargetFramework
to net5.0-windows10.0.* or install the Microsoft.Windows.SDK.Contracts
NuGet package as described here.
You can then use the Windows.UI.ViewManagement.UISettings.TextScaleFactor
property:
double factor = new UISettings().TextScaleFactor;

mm8
- 163,881
- 10
- 57
- 88
-
I am using `.NET Framework 4.7.2`, I have installed `Microsoft.Windows.SDK.Contracts` but the `Windows.UI.ViewManagement` namespace is not included( only `Windows.UI.Xaml` – Themelis Aug 18 '21 at 14:28
-
It does not, there's an error that says *must use `PackageReference`*, in the `Microsoft.Windows.SDK.Contracts.targets`. Is this about adding a reference of the nuget package to my project? – Themelis Aug 18 '21 at 14:32
-
Content of `Microsoft.Windows.SDK.Contracts.targets` file. `
-
-
-
I've restored the nuget packages of my solution, but the same error appears. Isn't that supposed to be a trivial operation? – Themelis Aug 18 '21 at 14:40
-
Yes, assuming you are using the modern project format. Apparently you (still) don't. – mm8 Aug 18 '21 at 14:41
-
As you've figgured out I have no clue if I am using the modern or the old one. I've pasted my `.csproject` file. Is that the old one? – Themelis Aug 18 '21 at 14:45
-
-
1
Windows 10
I've found a way to retrieve the Text Scaling Factor, without installing any Nuget packages. The idea is to obtain the value directly from the registry.
var userKey = Microsoft.Win32.Registry.CurrentUser;
var softKey = userKey.OpenSubKey("Software");
var micKey = softKey.OpenSubKey("Microsoft");
var accKey = micKey.OpenSubKey("Accessibility");
var factor = accKey.GetValue("TextScaleFactor");`

Themelis
- 4,048
- 2
- 21
- 45
-
I don't know if this is a Windows 11 problem, but factor returned `null` for me. – Victor Chelaru Jun 18 '22 at 16:28
-