要实现这个效果很简单,只需自定义一个类继承ListBox,然后重写OnDrawItem事件就可以了,下面看代码
class CListbox:ListBox
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
protected override void OnDrawItem(DrawItemEventArgs e)
{
Graphics g = e.Graphics;
if (e.Index % 2 == 0)
{
g.FillRectangle(new SolidBrush(Color.WhiteSmoke), e.Bounds);
g.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Blue), e.Bounds);
}
else
{