2个回答
展开全部
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
首先将DrawMode为DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才触发该事件(DrawItem事件)
//在构造函数中写上触发的事件:
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush myBrush = Brushes.Black;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
myBrush = new SolidBrush(Color.FromArgb(0, 0, 255));//选中时背景颜色
}
else//没有选中时的背景颜色
{
myBrush = new SolidBrush(Color.White);
}
e.Graphics.FillRectangle(myBrush, e.Bounds);//填满矩形背景颜色
e.Graphics.DrawRectangle(new Pen(new SolidBrush(e.ForeColor)), e.Bounds);
e.DrawFocusRectangle();//焦点框
StringFormat stringformat = StringFormat.GenericDefault;
stringformat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, stringformat);
}
首先将DrawMode为DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才触发该事件(DrawItem事件)
//在构造函数中写上触发的事件:
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush myBrush = Brushes.Black;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
myBrush = new SolidBrush(Color.FromArgb(0, 0, 255));//选中时背景颜色
}
else//没有选中时的背景颜色
{
myBrush = new SolidBrush(Color.White);
}
e.Graphics.FillRectangle(myBrush, e.Bounds);//填满矩形背景颜色
e.Graphics.DrawRectangle(new Pen(new SolidBrush(e.ForeColor)), e.Bounds);
e.DrawFocusRectangle();//焦点框
StringFormat stringformat = StringFormat.GenericDefault;
stringformat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, stringformat);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重绘 ListItem样式
追问
有代码吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询