我想更改 C# WindowsForms 中 CheckedListBox 中检查的项目的颜色。
谁能帮我解决这个问题!
这应该让你开始。我将 a 子类CheckedListBox
化并覆盖了绘图事件。结果是列表中所有选中的项目都以红色背景绘制。
通过玩这个,如果您希望复选框后面的区域也具有不同的颜色,请e.Graphics.FillRectangle
在调用之前使用base.OnDrawItem
.
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);
}
}
感谢 Jon 让我走上了正确的道路,因为我有同样的愿望:让项目的文本颜色对于复选框的 3 种状态中的每一种都不同。
我想出了 CheckedListBox 的这个子类。它更改项目文本,而不是背景颜色。它允许用户在设计时或在代码中设置 3 种颜色。
它还解决了我在查看设计器中的控件时遇到错误的问题。我还必须克服一个我认为在您的解决方案中会发生的问题,如果选择该项目,则 base.OnDrawItem 方法会消除在覆盖的 OnDrawItem 方法中设置的颜色选择。我这样做的代价是所选项目不再具有彩色背景,方法是删除 e.State 中表示它已被选中的部分,以便在 base.OnDrawItem 中它不会成为所选项目的外观和感觉。虽然我认为这没关系,因为用户仍然会看到焦点矩形,它指示哪个被选中。
希望这对其他人有用。在网上查看时,我没有找到很多有凝聚力的解决方案(甚至只是一个完整的 OnDrawItem 方法)。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace MyNameSpace
{
/// <summary>
/// This is a CheckedListBox that allows the item's text color to be different for each of the 3 states of the corresponding checkbox's value.
/// Like the base CheckedListBox control, you must handle setting of the indeterminate checkbox state yourself.
/// Note also that this control doesn't allow highlighting of the selected item since that obscures the item's special text color which has the special meaning. But
/// the selected item is still known to the user by the focus rectangle it will have surrounding it, like usual.
/// </summary>
class ColorCodedCheckedListBox : CheckedListBox
{
public Color UncheckedColor { get; set; }
public Color CheckedColor { get; set; }
public Color IndeterminateColor { get; set; }
/// <summary>
/// Parameterless Constructor
/// </summary>
public ColorCodedCheckedListBox()
{
UncheckedColor = Color.Green;
CheckedColor = Color.Red;
IndeterminateColor = Color.Orange;
}
/// <summary>
/// Constructor that allows setting of item colors when checkbox has one of 3 states.
/// </summary>
/// <param name="uncheckedColor">The text color of the items that are unchecked.</param>
/// <param name="checkedColor">The text color of the items that are checked.</param>
/// <param name="indeterminateColor">The text color of the items that are indeterminate.</param>
public ColorCodedCheckedListBox(Color uncheckedColor, Color checkedColor, Color indeterminateColor)
{
UncheckedColor = uncheckedColor;
CheckedColor = checkedColor;
IndeterminateColor = indeterminateColor;
}
/// <summary>
/// Overriden draw method that doesn't allow highlighting of the selected item since that obscures the item's text color which has desired meaning. But the
/// selected item is still known to the user by the focus rectangle being displayed.
/// </summary>
/// <param name="e"></param>
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (this.DesignMode)
{
base.OnDrawItem(e);
}
else
{
Color textColor = this.GetItemCheckState(e.Index) == CheckState.Unchecked ? UncheckedColor : (this.GetItemCheckState(e.Index) == CheckState.Checked ? CheckedColor : IndeterminateColor);
DrawItemEventArgs e2 = new DrawItemEventArgs
(e.Graphics,
e.Font,
new Rectangle(e.Bounds.Location, e.Bounds.Size),
e.Index,
(e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None, /* Remove 'selected' state so that the base.OnDrawItem doesn't obliterate the work we are doing here. */
textColor,
this.BackColor);
base.OnDrawItem(e2);
}
}
}
}