简体   繁体   English

检索Windows 10任务栏的颜色

[英]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. 我发现, System.Windows.SystemParameters类中有一个静态属性,用于声明用户为其Windows整体选择的颜色。

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. 我无法在SystemParameters类中找到该键。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM