110,533
社区成员
发帖
与我相关
我的任务
分享
//仅供参考
class TransparentListBox : ListBox
{
public TransparentListBox()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
this.Invalidate();
base.OnSelectedIndexChanged(e);
}
protected override void OnPaint(PaintEventArgs e)
{
if (this.Focused && this.SelectedItem != null)
{
Rectangle itemRect = this.GetItemRectangle(this.SelectedIndex);
e.Graphics.FillRectangle(Brushes.Green, itemRect);
}
for (int i = 0; i < Items.Count; i++)
{
e.Graphics.DrawString(this.GetItemText(this.Items[i]), this.Font,
new SolidBrush(this.ForeColor), this.GetItemRectangle(i));
}
base.OnPaint(e);
}
}