Taskbar (Shell_TrayWnd) Alignment of items using Win32 API

Jacob Mordon 225 Reputation points
2024-06-14T12:30:53.7566667+00:00

Currently, I have a problem with changing the alignment of items such as Start Button, Search, Pinned items.

I wrote code, which works in a Console Application, but it won't work in a WinUI 3 or UWP Packaged Desktop application because of restrictions which will not give me a permission to update the registry.

Here's sample of code:

public static void SetTaskbarAlignment(bool isCentered)
{
    using var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", true);

    if (key is not null)
    {
        var value = isCentered ? 1 : 0;

        key.SetValue("TaskbarAl", value, RegistryValueKind.DWord);
    }
}

Is there other way to change Taskbar Alignment using Win32 API and not changing registry in a process?

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,490 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,649 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.
11,006 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 54,806 Reputation points
    2024-06-14T14:18:27.43+00:00

    The setting you're trying to change is a setting that controls the behavior of the shell for the current user. If a sandbox app, like your UWP app, was able to do that then it wouldn't be much of a sandbox. The whole purpose of running an app in a sandbox in UWP is so it cannot make changes that the user doesn't authorize. Changing the taskbar alignment would be such a change.

    If you need to make a shell change for the user then you're going to need to request permission for your app to do it via the registry. I'm not aware of a Win32 API to do it but even if there were then your UWP app would either not have access (because it is a sandbox) or you'd have to ask for permissions to whatever rights it is tied to.

    1 person found this answer helpful.

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.