我想更改CheckedListBox
上的蓝色选择颜色。我已经尝试创建Custom
CheckedListBox
,但由于出现在x64
上,因此出现错误。我在互联网上搜索了2天,却找不到任何东西。还有其他人遇到同样的问题吗?
如果您要投反对票,请解释原因,谢谢:)
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (DrawItemState.None != e.State)
e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, DrawItemState.NoFocusRect, this.ForeColor, Color.FromArgb(255, 90, 0));
base.OnDrawItem(e);
}
我得到的错误->无法加载工具箱项'CustomCheckeListBox',它将从工具箱中删除。
这在我的代码中有效:
protected override void OnDrawItem(DrawItemEventArgs e)
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State ^ DrawItemState.Selected,
e.ForeColor,
Color.Red);//Choose the color
System.Drawing.Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal);
int dx = (e.Bounds.Height - checkSize.Width) / 2;
e.DrawBackground();
bool isChecked = GetItemChecked(e.Index);//For some reason e.State doesn't work so we have to do this instead.
if (Enabled)
{
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx), isChecked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
}
else
{
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx), isChecked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled);
}
using (StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center })
{
using (Brush brush = new SolidBrush(isChecked ? CheckedItemColor : (Enabled ? ForeColor : SystemColors.GrayText)))
{
e.Graphics.DrawString(Items[e.Index].ToString(), Font, brush, new Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height), sf);
}
}
base.OnDrawItem(e);
}