Last active
March 9, 2023 02:33
-
-
Save mlalic/87151a520814603c8fb6aec46f5326df to your computer and use it in GitHub Desktop.
A sample demonstrating how to obtain the background color that apps should use from a simple Win32 process, using only C++ and the base Windows SDK with no additional library support (such as C++/WinRT or WRL).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <windows.h> | |
| #include <roapi.h> | |
| #include <iostream> | |
| #include <cwchar> | |
| #include <string> | |
| // 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::IUISettings3* 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 background color..." << std::endl; | |
| ::ABI::Windows::UI::Color color; | |
| hr = uiSettings->GetColorValue(::ABI::Windows::UI::ViewManagement::UIColorType_Background, &color); | |
| if (FAILED(hr)) { | |
| std::cout << std::hex << hr << std::endl; | |
| return 3; | |
| } | |
| std::string colorName; | |
| if (color.A == 0xff && color.R == 0 && color.G == 0 && color.B == 0) { | |
| colorName = "Black/Dark"; | |
| } else if (color.A == 0xff && color.R == 0xff && color.G == 0xff && color.B == 0xff) { | |
| colorName = "White/Light"; | |
| } else { | |
| colorName = "Other"; | |
| } | |
| std::cout << "Current background color is " << colorName << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment