Search code examples
winapiwndproc

WM_SETTINGSCHANGE is received twice when changing preference to dark mode


I am catching the WM_SETTINGSCHANGE in WndProc so that I can update my app for dark/light mode changes. I run the app and catch the messages when I change my user preference in Personalization / Colors / Choose Your Mode (light, dark, custom) on Windows 11.

I notice that changing my preference generates TWO of the WM_SETTINGSCHANGE messages in WndProc. They do have different numbers in the LParam, but I can't figure out how to print the LParam string in C# (if that makes a difference).

The values of the two messages are as follows, where 0=user policy change. I don't know what the values of the lparams are - the doc says they are the addresses of strings (possibly to null strings?) provided by the sender of the messages:

wparam=0, lparam=800528915624
wparam=0, lparam=800528912280

Does this mean I have to update all my app settings twice whenever I get a message? (The Microsoft doc for WM_SETTINGSCHANGE recommends updating all app system settings each time.) Does anyone know why my single preference change triggers TWO settings change messages? Thank you.


Solution

  • As I mentioned in my answer to your other question:

    In C#, you can use PtrToStringAnsi(), PtrToStringUni(), or PtrToStringAuto() function (depending on how you are creating your window) to access the string value from the lParam of WM_SETTINGCHANGE.

    But, as @IInspectable noted, you are likely to get multiple messages with the same string value. So yes, you will have to reload your app settings each time. To avoid doing that unnecessarily if you receive multiple messages in a short period of time, you can throttle the reload by (re)starting a short timer whenever a new setting-changing message is received, and then reload your app settings only when that timer has elapsed.