c# - 检索Windows 10任务栏的颜色

标签 c# windows-10 customization

我发现,在 System.Windows.SystemParameters 类中有一个静态属性声明了用户为其整个 Windows 选择的颜色。

但是对于用户来说,还有第二种可能性,使他能够启用或禁用任务栏/窗口栏是否应该使用相同的颜色。

我无法在 SystemParameters 类中找到它的键。

最佳答案

我相信有一个注册表值可以找到颜色,它可能在里面:

HKEY_CURRENT_USER\Control Panel\Colors

但是在我的系统上,我禁用了任务栏上的颜色,并且该颜色值似乎没有出现在该键中。

解决方法是结合以下两个问题的答案:

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

您需要以下导入:

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

以下结构:

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

和以下常量:

private const int ABM_GETTASKBARPOS = 5;

然后可以调用下面两个方法:

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

像这样:

Color taskBarColour = GetColourAt(GetTaskbarPosition().Location);

关于c# - 检索Windows 10任务栏的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805080/

相关文章:

c# - 如何将每个句子的第一个字符大写

c# - 替换字符串中的 {x} 个标记

json - Openresty自定义json访问日志

C# 方法 where 约束 T :V

c# - 'split' 基于条件的通用列表的最(性能)最有效和可读的方法是什么?

windows-10 - “docusaurus-start”未被识别为内部或外部命令

python-2.7 - Scipy安装cygwin64 Windows10后期失败

c++ - 使用 __int16(或 int16_t)优于 int 的优点/缺点

c# - 使用 URI 创建客户特定的外观

ios - 自定义 UISwitch?