Taskbar Color Reverts When Changing Color Using SetWindowCompositionAttribute in WinUI3

Jacob Mordon 225 Reputation points
2024-09-10T06:38:06.4466667+00:00

Hi, I am encountering an issue with the taskbar color on Windows. When I change the taskbar color using the SetWindowCompositionAttribute method, the color initially changes as expected. However, as soon as I click the 'Start' button, the taskbar color reverts to the default color specified by the Windows settings.

Steps to Reproduce:

  1. Change the taskbar color using SetWindowCompositionAttribute.
  2. Observe the taskbar color change.
  3. Click the 'Start' button.
  4. Notice the taskbar color reverting to the Windows default color.

Expected Behavior: The taskbar color should remain as set by SetWindowCompositionAttribute, even after interacting with the 'Start' button.

Actual Behavior: The taskbar color reverts to the default Windows settings when the 'Start' button is clicked.

Code Sample:

public static void SetTaskbarColor(Color color)
{
    var accentPolicy = default(User32Interop.AccentPolicy);

    accentPolicy.Color = color.ToAbgr();
    accentPolicy.AccentState = 2;
    accentPolicy.Flags = 2;

    var data = default(User32Interop.Windowcompositionattribdata);

    data.Attribute = User32Interop.WindowCompositionAttribute.WcaAccentPolicy;
    data.SizeOfData = Marshal.SizeOf(typeof(User32Interop.AccentPolicy));
    data.Data = Marshal.AllocHGlobal(data.SizeOfData);

    Marshal.StructureToPtr(accentPolicy, data.Data, false);

    var taskbarHandle = User32Interop.FindWindow("Shell_TrayWnd", null);

    var secondaryTaskbarHandle = User32Interop.FindWindow("Shell_SecondaryTrayWnd", null);

    if (taskbarHandle != nint.Zero)
    {
        User32Interop.SetWindowCompositionAttribute(taskbarHandle, ref data);
    }

    if (secondaryTaskbarHandle != nint.Zero)
    {
        User32Interop.SetWindowCompositionAttribute(secondaryTaskbarHandle, ref data);
    }

    Marshal.FreeHGlobal(data.Data);
}
Windows development | Windows App SDK
Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2024-09-10T08:06:23.7933333+00:00
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.