【发布时间】:2016-04-26 08:16:22
【问题描述】:
我创建了一个自定义复选框,因此我可以更改某些项目的前景色,但是当我启用 MutiColumn 为 true 时,这些值会相互重叠,而不是在多个列中工作..
public sealed class CustomCheckedListBox : CheckedListBox
{
public CustomCheckedListBox()
{
DoubleBuffered = true;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
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.
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx),
isChecked
? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
: System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
using (StringFormat sf = new StringFormat {LineAlignment = StringAlignment.Center})
{
using (Brush brush = new SolidBrush(isChecked ? CheckedItemColor : BackColor))
{
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);
}
}
}
private Color _checkedItemColor = Color.Blue;
public Color CheckedItemColor
{
get { return _checkedItemColor; }
set
{
_checkedItemColor = value;
Invalidate();
}
}
}
任何人都可以提出任何需要进行的更改以防止这种情况发生吗?
【问题讨论】:
-
您是否尝试对 DrawSubItem 进行编码?还。错误结果的图像通常会有所帮助..
-
非常感谢,我去看看..
-
同时查看属性:
ListVIewItem.UseItemStyleForSubItems- 您可能需要将其设置为 false..
标签: c# winforms checklistbox