Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve color of windows 10 taskbar

I found out, that there is a static property in the System.Windows.SystemParameters class that declares the color the user chose for his Windows overall.

But there is a second possibility for the user that enables him to enable or disable, whether the taskbar/windows bar should use that same color.

I was unable to find a key for that in the SystemParameters-class.

like image 464
Jens Langenbach Avatar asked Jan 23 '17 11:01

Jens Langenbach


1 Answers

I believe there is a registry value to find the colour and it is probably inside:

HKEY_CURRENT_USER\Control Panel\Colors

However on my system I have colours on the taskbar disabled and that colour value doesn't seem to appear in this key.

A work around would be to combine the answers to the following two questions:

  1. TaskBar Location
  2. How to Read the Colour of a Screen Pixel

You need the following imports:

[DllImport("shell32.dll")]
private static extern IntPtr SHAppBarMessage(int msg, ref APPBARDATA data);

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);

The following structs:

private struct APPBARDATA
{
    public int cbSize;
    public IntPtr hWnd;
    public int uCallbackMessage;
    public int uEdge;
    public RECT rc;
    public IntPtr lParam;
}

private struct RECT
{
    public int left, top, right, bottom;
}

And the following constant:

private const int ABM_GETTASKBARPOS = 5;

Then you can call the following two methods:

public static Rectangle GetTaskbarPosition()
{
    APPBARDATA data = new APPBARDATA();
    data.cbSize = Marshal.SizeOf(data);

    IntPtr retval = SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
    if (retval == IntPtr.Zero)
    {
        throw new Win32Exception("Please re-install Windows");
    }

    return new Rectangle(data.rc.left, data.rc.top, data.rc.right - data.rc.left, data.rc.bottom - data.rc.top);
}

public static Color GetColourAt(Point location)
{
    using (Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
    using (Graphics gdest = Graphics.FromImage(screenPixel))
    {
        using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
        {
            IntPtr hSrcDC = gsrc.GetHdc();
            IntPtr hDC = gdest.GetHdc();
            int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
            gdest.ReleaseHdc();
            gsrc.ReleaseHdc();
        }

        return screenPixel.GetPixel(0, 0);
    }
}

Like this:

Color taskBarColour = GetColourAt(GetTaskbarPosition().Location);
like image 126
TheLethalCoder Avatar answered Oct 20 '22 02:10

TheLethalCoder