question

Beheeddine-8582 avatar image
0 Votes"
Beheeddine-8582 asked Beheeddine-8582 commented

windows runtime message handler

so i was trying to implement a code where I don't need to check for the accent color of windows like the accent color in the setting if it does changed or not
every x millisencond so I thought about making some kind of event

WM_SETTINGCHANGE

is what StackOverflow is saying me to use so is there a better idea like I'm using c++ not c#
+ do anyone has any idea if there is an alternative for this message in windows runtime

  • for just clear things out I wanna basically to run a function everytime the accent color change and thanks



windows-uwpwindows-11windows-api
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered Beheeddine-8582 commented

I made a test in C++/WinRT/Console from scratch in VS 2022, so I hope it will work with VS Code...

197552-colorvalueschanged-console.jpg

 #include <Unknwn.h>
 #include <winrt/Windows.UI.Core.h>
 #include <winrt/Windows.UI.ViewManagement.h>
 #include <winrt/Windows.UI.h>
 #include <winrt/Windows.UI.XAML.Media.h>
 #include <winrt/Windows.UI.XAML.h>
 #include <winrt/Windows.Foundation.h>
 #include <stdio.h>
    
 using namespace winrt;
 using namespace Windows::Foundation;
    
 struct CNotify 
 {
  void NotifyColors()
  {
  colorValuesChangedRevoker = uiSettings.ColorValuesChanged(winrt::auto_revoke, [this](winrt::Windows::UI::ViewManagement::UISettings const&, winrt::Windows::Foundation::IInspectable const&)
  {
  this->UpdateAccentColor();
  });
  }
 private:
  Windows::UI::ViewManagement::UISettings uiSettings;
  Windows::UI::ViewManagement::UISettings::ColorValuesChanged_revoker colorValuesChangedRevoker;
  void UpdateAccentColor();
 };
    
 void CNotify::UpdateAccentColor()
 {
  Windows::UI::Color accentColor = uiSettings.GetColorValue(Windows::UI::ViewManagement::UIColorType::Accent);
  //winrt::hstring hsCoplorName = Windows::UI::ColorHelper::ToDisplayName(accentColor);
  //printf("Accent Color : (%d, %d, %d, %d) - %ls\n", accentColor.A, accentColor.R, accentColor.G, accentColor.B, hsCoplorName.c_str());
  printf("Accent Color : (%d, %d, %d, %d)\n", accentColor.A, accentColor.R, accentColor.G, accentColor.B);
 }
    
 int main()
 {
   init_apartment();
  CNotify notify;
  notify.NotifyColors();
    
  INPUT_RECORD rec;
  HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
  DWORD nEnreg;
  printf("Waiting for color change (Escape to exit)...\n");
  for (;;)
  {
  ReadConsoleInput(hConsole, &rec, 1, &nEnreg);
  if (rec.EventType == KEY_EVENT)
  {
  if (!rec.Event.KeyEvent.bKeyDown && rec.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
  break;
  }
  }
  return(0);
 }




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

welll this is working epicly thanks a lot

0 Votes 0 ·
XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered Beheeddine-8582 commented

One way is UISettings ColorValuesChanged event as @Raymond Chen suggests. Or according to the answer, you can use SystemControlBackgroundAccentBrush to detect.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How to use that colorvalue changed i tried that but i was lost can someone just give me an exemple in c++

And thanks

0 Votes 0 ·

as i said everything is in C# you know

0 Votes 0 ·
Castorix31 avatar image
1 Vote"
Castorix31 answered Castorix31 commented

A basic test in C++/WinRT from a UWP template, where I change the Grid background color when System colors change =>

196754-colorvalueschanged.gif

.h :

 #include "MainPage.g.h"
    
 #include <winrt/Windows.UI.Core.h>
 #include <winrt/Windows.UI.ViewManagement.h>
 #include <winrt/Windows.UI.h>
 #include <winrt/Windows.UI.XAML.Media.h>
    
 namespace winrt::UWP_CPP_WinRT3::implementation
 {
     struct MainPage : MainPageT<MainPage>
     {
         MainPage();
    
         int32_t MyProperty();
         void MyProperty(int32_t value);
    
         void ClickHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
    
         void NorifyColors()
         {
             colorValuesChangedRevoker = uiSettings.ColorValuesChanged(winrt::auto_revoke, [this](auto&&, auto&&)
                 {
                     Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [strongThis = get_strong()]()
                     {
                         strongThis->UpdateAccentColor();
                     });
                 });
         }
     private:
         Windows::UI::ViewManagement::UISettings uiSettings;
         Windows::UI::ViewManagement::UISettings::ColorValuesChanged_revoker colorValuesChangedRevoker;
         void UpdateAccentColor();
     };
 }

in .cpp :

 // mainGrid is the main Grid of the main window

 void MainPage::UpdateAccentColor()
 {
     Color accentColor = uiSettings.GetColorValue(Windows::UI::ViewManagement::UIColorType::Accent);
     mainGrid().Background(Windows::UI::Xaml::Media::SolidColorBrush::SolidColorBrush(accentColor));
 }
    
 void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
 {
     myButton().Content(box_value(L"Clicked"));
     NorifyColors();
 }



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

196777-screenshot-75.png



well this is clearly in the documentation even thought you can find it in the developer tools when you search the source
so yeah thanks well thanks a lot
if you can can you give me the link to that UWP template I guess I have to figure it out well understand what every line in your code is doing before I implement it
thanks
+can you answer my comments even though I accepted your answer
thanks again @Castorix31

0 Votes 0 ·
screenshot-75.png (260.9 KiB)
Castorix31 avatar image Castorix31 Beheeddine-8582 ·

if you can can you give me the link to that UWP template

It is the code generated by this VS Template :

196863-uwp-cpp-winrt.jpg


0 Votes 0 ·
uwp-cpp-winrt.jpg (111.5 KiB)
Beheeddine-8582 avatar image
0 Votes"
Beheeddine-8582 answered Beheeddine-8582 edited

welll is there a while loop i can replicate replicate part of it from scratch but I'm stuck the function doesn't want to call itself well how I make this happens
I guess something related to
winrt::BlankApp1::implementation
but how is that
I'm trying to do all of that In a single main.cpp file since i wanna understand the basics first here's where I'm at

include <fstream>

include <iostream>

include <filesystem>

include <chrono>

include <thread>

include <winrt/Windows.UI.Core.h>

include <winrt/Windows.UI.ViewManagement.h>

include <winrt/Windows.UI.h>

include <winrt/Windows.UI.XAML.Media.h>

int wew = 55;
winrt::Windows::UI::ViewManagement::UISettings::ColorValuesChanged_revoker colorValuesChangedRevoker;
winrt::Windows::UI::ViewManagement::UISettings uiSettings;
struct mazin
{
void NorifyColors()
{
colorValuesChangedRevoker = uiSettings.ColorValuesChanged(winrt::auto_revoke, [this](auto&&, auto&&)
{
Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [strongThis = get_strong()]()
{
strongThis->UpdateAccentColor();
});
});
}
private:
void UpdateAccentColor();
};

void mazin::UpdateAccentColor(){
winrt::Windows::UI::Color accentColor = uiSettings.GetColorValue(winrt::Windows::UI::ViewManagement::UIColorType::Accent);
wew = wew +2 ;
}

int main (){
bool running = true;
if (!std::filesystem::exists("color.inc")) {
running = false;
}
running = true;
while (running)
{
std::cout << wew << std::endl;
}
}

thanks for your help thought @Castorix31

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BTW i'm using vscode if this can help

0 Votes 0 ·

You should try Visual Studio C++/WinRT template. vscode is unavailable to me.

0 Votes 0 ·

the function doesn't want to call itself

In my test, I call it on Button click (I renamed it into NotifyColors instead of NorifyColors (typo)...)
(you can see on the GIF that I click first on the Button) :

 void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
 {
     myButton().Content(box_value(L"Clicked"));
     NotifyColors();
 }
0 Votes 0 ·

The problem here then is then how to call this function automatically
Like how to put it in the main function

0 Votes 0 ·
Castorix31 avatar image Castorix31 Beheeddine-8582 ·

You can just call it at beginning inside main(), like for any function call

   NotifyColors();

(but I did not test in a Console program...)


0 Votes 0 ·
Show more comments
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered Beheeddine-8582 commented

Hi Beheeddine,

Thanks for taking time to explain the scenario to us.

If you want to obtain the user’s accent color, you can ask the UI Settings object for the current color value of UIColorType.Accent.

You can find more information about UI Settings here - https://docs.microsoft.com/en-us/uwp/api/Windows.UI.ViewManagement.UISettings?redirectedfrom=MSDN&view=winrt-22000

If you are trying to get the details using C++ app, try following code.

namespace vm = Windows::UI::ViewManagement;

void GetAccentColor()
{
auto settings = ref new vm::UISettings();
auto color = settings->GetColorValue(vm::UIColorType::Accent);
// color.A, color.R, color.G, and color.B are the color channels.
}

in C#, use following code.

using vm = Windows.UI.ViewManagement;

void GetAccentColor()
{
var settings = new vm.UISettings();
var color = settings.GetColorValue(vm.UIColorType.Accent);
// color.A, color.R, color.G, and color.B are the color channels.
}

Also you have following event to check the color type is changed or not.

public event TypedEventHandler<UISettings,object> ColorValuesChanged;

Additional info here - https://docs.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.uisettings.colorvalueschanged?view=winrt-22000

A batch file with above method can be scheduled to run under windows tasks to monitor the change event.




--If the reply is helpful, please Upvote and Accept as answer.--

Thank you!

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

the question is that how to right this

public event TypedEventHandler<UISettings,object> ColorValuesChanged;

in c++ I guess that's the final question

0 Votes 0 ·