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
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,360 questions
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
783 questions
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,603 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,886 questions
{count} votes

Accepted answer
  1. Castorix31 84,871 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.