110,534
社区成员
发帖
与我相关
我的任务
分享
public class MyList : ListBox
{
public MyList()
{
DrawMode = DrawMode.OwnerDrawFixed;
//this.BackColor = Color.Black;
this.BorderStyle = BorderStyle.None;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs pe)
{
pe.DrawBackground(); //画背景
pe.DrawFocusRectangle(); //画边框
Rectangle bounds = pe.Bounds;
Color ForColor = Color.CadetBlue;//文字颜色
// Check whether the index is valid
if (pe.Index % 2 != 0)
{
}
if (pe.Index >= 0 && pe.Index < Items.Count)
{
object item = this.Items[pe.Index]; //取得需要绘制项的引用
int iOffset = 0;
iOffset += 5;
if (pe.State !=DrawItemState .None&&pe .State !=DrawItemState.Focus )
{
pe.Graphics.DrawRectangle(Pens.YellowGreen, bounds);//绘制边框
using (Brush b = new SolidBrush(System.Drawing.Color .Blue ))
{
pe.Graphics.FillRectangle(b, pe.Bounds);//填充矩形
}
GraphicsPath path = new GraphicsPath();
int x = bounds.X;
int y = bounds.Y;
Point[] Points ={ new Point(0+x, 0+y), new Point(5+x, 5+y), new Point(0+x, 10+y), new Point(0+x, 0+y) };
path.AddLines(Points);
pe.Graphics.DrawPath(Pens.Blue, path);//绘制三角
pe.Graphics.FillPath(Brushes.Green , path);//填充三角
ForColor = System.Drawing.SystemColors.HighlightText;//文字突出显示颜色
}
pe.Graphics.DrawString(item.ToString (), pe.Font, new SolidBrush(ForColor ), bounds.Left + iOffset, bounds.Top); //根据项的颜色绘制文本
}
base.OnDrawItem(pe);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace mylistbox
{
public class ListBoxEx : ListBox
{
public int[] PlayIndex;
public bool ShowTriangle;
public bool ShowNumber;
public ListBoxEx()
{
DrawMode = DrawMode.OwnerDrawFixed;
SetStyle(ControlStyles.DoubleBuffer, true);
this.BackColor = Color.Black;
this.BorderStyle = BorderStyle.None;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs pe)
{
// pe.DrawFocusRectangle(); //画边框
if (pe.Index >= 0 && pe.Index < Items.Count)
{
pe.DrawBackground(); //画背景
Rectangle bounds = pe.Bounds;
Color ForColor = Color.FromArgb(0, 130, 255); //文字颜色
Color NumColor = Color.FromArgb(0, 130, 0);//数字颜色
Color SelectedColor = SystemColors.HighlightText;//选中时的颜色
Color TriangleColor = Color.Lime;//三角的颜色
Pen p = Pens.Black;
object item = this.Items[pe.Index]; //取得需要绘制项的引用
int iOffset = 0;
if (pe.Index % 2 != 0)
{
using (Brush b = new SolidBrush(Color.FromArgb(32, 32, 32)))
{
pe.Graphics.FillRectangle(b, bounds);//绘制背景颜色;
}
}
if ((pe.State & DrawItemState.Selected) == DrawItemState.Selected)
{
using (LinearGradientBrush b = new LinearGradientBrush(bounds, Color.FromArgb(49, 105, 206), Color.FromArgb(0, 8, 0), LinearGradientMode.Vertical))
{
pe.Graphics.FillRectangle(b, pe.Bounds);//填充矩形
}
ForColor = SelectedColor;
NumColor = SelectedColor;
TriangleColor = SelectedColor;
p = Pens.White;
// pe.Graphics.DrawRectangle(p, bounds);
}
pe.Graphics.DrawRectangle(p , bounds);//绘制边框
if (Array .IndexOf ( this.PlayIndex,pe.Index)>=0)
{
if (ShowTriangle)
{
GraphicsPath path = new GraphicsPath();
int x = bounds.X;
int y = bounds.Y;
Point[] Points ={ new Point(0 + x, 2 + y), new Point(4 + x, 5 + y), new Point(0 + x, 8 + y), new Point(0 + x, 2 + y) };
path.AddLines(Points);
// pe.Graphics.DrawPath(Pens.Blue, path);//绘制三角
using (Brush b = new SolidBrush(TriangleColor))
{
pe.Graphics.FillPath(b, path);//填充三角
}
}
ForColor = TriangleColor;//文字突出显示颜色
NumColor = TriangleColor;
}
if (ShowNumber)
{
using (Brush b = new SolidBrush(NumColor))
{
pe.Graphics.DrawString((pe.Index + 1).ToString().PadLeft(3) + ".", pe.Font, b, bounds.Left + iOffset, bounds.Top);
}
iOffset += 20;
}
using (Brush b = new SolidBrush(ForColor))
{
pe.Graphics.DrawString(item.ToString(), pe.Font, b, bounds.Left + iOffset + 3, bounds.Top); //根据项的颜色绘制文本
}
}
base.OnDrawItem(pe);
}
}
}
public class MyList : ListBox
{
public MyList()
{
DrawMode = DrawMode.OwnerDrawFixed;
//this.BackColor = Color.Black;
this.BorderStyle = BorderStyle.None;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs pe)
{
pe.DrawBackground(); //画背景
pe.DrawFocusRectangle(); //画边框
Rectangle bounds = pe.Bounds;
Color ForColor = Color.CadetBlue;//文字颜色
// Check whether the index is valid
if (pe.Index % 2 != 0)
{
}
if (pe.Index >= 0 && pe.Index < Items.Count)
{
object item = this.Items[pe.Index]; //取得需要绘制项的引用
int iOffset = 0;
iOffset += 5;
if (pe.State !=DrawItemState .None&&pe .State !=DrawItemState.Focus )
{
Rectangle r = bounds;
r.Inflate(0, -1);//--------------------------------------------> 注意:这儿做了修改
pe.Graphics.DrawRectangle(Pens.YellowGreen, r);//绘制边框
using (Brush b = new SolidBrush(System.Drawing.Color .Blue ))
{
pe.Graphics.FillRectangle(b, r);//填充矩形
}
GraphicsPath path = new GraphicsPath();
int x = bounds.X;
int y = bounds.Y;
Point[] Points ={ new Point(0+x, 0+y), new Point(5+x, 5+y), new Point(0+x, 10+y), new Point(0+x, 0+y) };
path.AddLines(Points);
pe.Graphics.DrawPath(Pens.Blue, path);//绘制三角
pe.Graphics.FillPath(Brushes.Green , path);//填充三角
ForColor = System.Drawing.SystemColors.HighlightText;//文字突出显示颜色
}
pe.Graphics.DrawString(item.ToString (), pe.Font, new SolidBrush(ForColor ), bounds.Left + iOffset, bounds.Top); //根据项的颜色绘制文本
}
base.OnDrawItem(pe);
}
}