windows runtime message handler

Behe eddine 41 Reputation points
2022-04-25T21:27:55.34+00:00

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
Universal Windows Platform (UWP)
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,412 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,103 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2022-04-28T23:04:56.027+00:00

    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 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Castorix31 81,461 Reputation points
    2022-04-27T00:06:01.407+00:00

    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();  
    }  
    
    1 person found this answer helpful.

  2. Xiaopo Yang - MSFT 11,256 Reputation points Microsoft Vendor
    2022-04-26T06:52:49.09+00:00

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


  3. Behe eddine 41 Reputation points
    2022-04-27T16:44:24.157+00:00

    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


  4. Limitless Technology 39,336 Reputation points
    2022-04-28T15:54:00.617+00:00

    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://learn.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://learn.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!