Skip to content

Instantly share code, notes, and snippets.

@mlalic
Created August 24, 2018 02:02
Show Gist options
  • Select an option

  • Save mlalic/6b3f151df8472e446c806e1e2a46fc36 to your computer and use it in GitHub Desktop.

Select an option

Save mlalic/6b3f151df8472e446c806e1e2a46fc36 to your computer and use it in GitHub Desktop.
A full sample for obtaining the current system text scale. It doesn't rely on any libraries (such as WRL or C++/WinRT) and uses only base Windows SDK APIs.
#include <windows.h>
#include <roapi.h>
#include <iostream>
#include <cwchar>
// For Windows.UI.ViewManagement.UISettings
#include <windows.ui.viewmanagement.h>
int main() {
::RoInitialize(RO_INIT_MULTITHREADED);
HSTRING_HEADER header;
HSTRING hstring;
::WindowsCreateStringReference(
RuntimeClass_Windows_UI_ViewManagement_UISettings,
wcslen(RuntimeClass_Windows_UI_ViewManagement_UISettings),
&header,
&hstring);
std::cout << "Activating UISettings" << std::endl;
// TODO: Use an RAII container/wrapper (such as Microsoft::WRL::ComPtr from the WRL library) to prevent leaks.
IInspectable* uiSettingsAsInspectable = nullptr;
HRESULT hr = ::RoActivateInstance(hstring, &uiSettingsAsInspectable);
if (FAILED(hr)) {
std::cout << std::hex << hr << std::endl;
return 1;
}
std::cout << "Querying for the appropriate interface..." << std::endl;
::ABI::Windows::UI::ViewManagement::IUISettings2* uiSettings = nullptr;
hr = uiSettingsAsInspectable->QueryInterface(__uuidof(uiSettings), reinterpret_cast<void**>(&uiSettings));
if (FAILED(hr)) {
std::cout << std::hex << hr << std::endl;
return 2;
}
std::cout << "Asking for the current scale factor..." << std::endl;
double factor = 0;
hr = uiSettings->get_TextScaleFactor(&factor);
if (FAILED(hr)) {
std::cout << std::hex << hr << std::endl;
return 3;
}
std::cout << "Current text scale factor == " << factor << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment