检索使用 OS 视觉样式呈现的 WinForms ListBox/ListView 的边框颜色

Retrieve border color of WinForms ListBox/ListView rendered with OS visual styles

如果我们创建一个新的 WinForms .NET 项目并在其上放置一个 ListBox 或 ListView 控件,默认情况下它们将使用 OS 视觉样式呈现:

我需要绘制一个 1 像素的边框,类似于系统在我的自定义控件中为 ListBox/ListView 提供的边框。有没有办法使用系统调用检索 ListBox/ListView 的系统边框颜色?

C#/WinForms 的快速回答:

var rend = new VisualStyleRenderer("ListBox", 0, 0);
Color borderColor = rend.GetColor(ColorProperty.BorderColor);

您还可以使用 Windows API 使用 [GetThemeColor}(https://docs.microsoft.com/en-us/windows/win32/api/uxtheme/nf-uxtheme-getthemecolor) 函数和 属性 标识符 3801:

但是,在一般情况下,边框可能包含不同的颜色值 and/or 并与父背景混合,因此检索到的颜色值可能与您期望的不完全相同。