gpt4 book ai didi

c# - 如何在 Windows 窗体中更改 CheckedListBox 中选定项的颜色?

转载 作者:太空狗 更新时间:2023-10-30 00:17:24 54 4
gpt4 key购买 nike

我想更改在 C# WindowsForms 的 CheckedListBox 中选中的项目的颜色。

谁能帮我解决这个问题!

最佳答案

这应该可以帮助您入门。我将 CheckedListBox 子类化并重写了绘图事件。结果是列表中所有选中的项目都用红色背景绘制。

从玩这个开始,如果您希望复选框后面的区域也有不同的颜色,请在调用 base.OnDrawItem 之前使用 e.Graphics.FillRectangle .

class ColouredCheckedListBox : CheckedListBox
{
protected override void OnDrawItem(DrawItemEventArgs e)
{
DrawItemEventArgs e2 =
new DrawItemEventArgs
(
e.Graphics,
e.Font,
new Rectangle(e.Bounds.Location, e.Bounds.Size),
e.Index,
e.State,
e.ForeColor,
this.CheckedIndices.Contains(e.Index) ? Color.Red : SystemColors.Window
);

base.OnDrawItem(e2);
}
}

关于c# - 如何在 Windows 窗体中更改 CheckedListBox 中选定项的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2130934/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com