Get Windows 10 Accent Colour
-
wrote on 3 Feb 2019, 16:14 last edited by mourke 2 Mar 2019, 19:24
Is it possible to get the windows 10 accent colour?
I have tried to do this using native APIs:
#include <wrl.h> #include <windows.ui.viewmanagement.h> namespace abi_vm = ABI::Windows::UI::ViewManagement; namespace wrl = Microsoft::WRL; namespace wf = Windows::Foundation; void GetAccentColor() { // Error checking has been elided for expository purposes. wrl::ComPtr<abi_vm::IUISettings3> settings; wf::ActivateInstance(wrl::Wrappers::HStringReference(RuntimeClass_Windows_UI_ViewManagement_UISettings).Get(), &settings); ABI::Windows::UI::Color color; settings->GetColorValue(abi_vm::UIColorType::UIColorType_Accent, &color); // color.A, color.R, color.G, and color.B are the color channels. }
But I keep getting unresolved symbol linker errors. I am using Visual Studio as my development environment so I do not have a .pro file. Instead of switching to Qt Creator as my IDE is there any way to just get the system accent colour? This seems like an awful lot of work to get a colour.
Thanks.
-
Is it possible to get the windows 10 accent colour?
I have tried to do this using native APIs:
#include <wrl.h> #include <windows.ui.viewmanagement.h> namespace abi_vm = ABI::Windows::UI::ViewManagement; namespace wrl = Microsoft::WRL; namespace wf = Windows::Foundation; void GetAccentColor() { // Error checking has been elided for expository purposes. wrl::ComPtr<abi_vm::IUISettings3> settings; wf::ActivateInstance(wrl::Wrappers::HStringReference(RuntimeClass_Windows_UI_ViewManagement_UISettings).Get(), &settings); ABI::Windows::UI::Color color; settings->GetColorValue(abi_vm::UIColorType::UIColorType_Accent, &color); // color.A, color.R, color.G, and color.B are the color channels. }
But I keep getting unresolved symbol linker errors. I am using Visual Studio as my development environment so I do not have a .pro file. Instead of switching to Qt Creator as my IDE is there any way to just get the system accent colour? This seems like an awful lot of work to get a colour.
Thanks.
@mourke untesolved symbol means, the Linker cannot find these functions.
You will need to link the appropriate DLLs to solve this. MSDN usually tells you which library that is for each function.
Regards
-
wrote on 3 Feb 2019, 17:29 last edited by
How do I do this without using a .pro file?
-
@mourke
Hi
Would that not be as normal in visual studio?
https://stackoverflow.com/questions/4445418/how-to-add-additional-libraries-to-visual-studio-project -
Using WRL is like eating razors. I strongly suggest you switch to the much nicer c++/winrt library included in the Windows SDK.
To get accent color with it you would do:
#include <winrt/Windows.UI.ViewManagement.h> #pragma comment(lib, "windowsapp") winrt::init_apartment(); // call this only once at the start of your app winrt::Windows::UI::ViewManagement::UISettings settings; winrt::Windows::UI::Color color = settings.GetColorValue(winrt::Windows::UI::ViewManagement::UIColorType::Accent);
Yes, the namespaces are ungodly, that's Windows runtime api for you :)
-
Using WRL is like eating razors. I strongly suggest you switch to the much nicer c++/winrt library included in the Windows SDK.
To get accent color with it you would do:
#include <winrt/Windows.UI.ViewManagement.h> #pragma comment(lib, "windowsapp") winrt::init_apartment(); // call this only once at the start of your app winrt::Windows::UI::ViewManagement::UISettings settings; winrt::Windows::UI::Color color = settings.GetColorValue(winrt::Windows::UI::ViewManagement::UIColorType::Accent);
Yes, the namespaces are ungodly, that's Windows runtime api for you :)
wrote on 3 Feb 2019, 19:21 last edited by@Chris-Kawa said in Get Windows 10 Accent Colour:
#pragma comment(lib, "windowsapp")
When I tried to include any winrt headersI got about 3 thousand build errors complaining about syntax and what not inside the library itself
-
It's a C++17 library, so make sure your project is set up to use that version of the language or later.
Go to project properties C/C++ -> Language -> C++ Language Standard and set it to either
ISO C++ 17
orISO C++ Latest Draft Standard
.It's 2019. You should be doing that for all your new projects anyway.
-
It's a C++17 library, so make sure your project is set up to use that version of the language or later.
Go to project properties C/C++ -> Language -> C++ Language Standard and set it to either
ISO C++ 17
orISO C++ Latest Draft Standard
.It's 2019. You should be doing that for all your new projects anyway.
wrote on 3 Feb 2019, 19:40 last edited by@Chris-Kawa Thanks so much it works perfectly!
winrt::init_apartment(); // call this only once at the start of your app
this line crashes the app but taking it out removes the crash and everything still works as expected. Thanks!
-
@Chris-Kawa Thanks so much it works perfectly!
winrt::init_apartment(); // call this only once at the start of your app
this line crashes the app but taking it out removes the crash and everything still works as expected. Thanks!
I'm glad I could help but
this line crashes the app
This line initializes COM. It should definitely not crash your app and it is needed for winrt stuff to even work.
but taking it out removes the crash and everything still works as expected
That's like saying taking out
main()
fixes an app. It's suspicious to say the least. I'd suggest to look into it because now you basically don't know why your app is working. Where did you put that line? Are you initializing COM in some other way?
3/9