微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何更改ListBox选择背景颜色?

认情况下,使用认颜色的 Windows设置是蓝色的.
假设我想把它永久改成红色.我使用Winforms.

提前致谢.

解决方法

您必须覆盖 Drawitem事件并将 DrawMode属性设置为 DrawMode.OwnerDrawFixed

检查这个样本

private void listBox1_DrawItem(object sender,DrawItemEventArgs e)
{
    if (e.Index<0) return;
    //if the item state is selected them change the back color 
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        e = new DrawItemEventArgs(e.Graphics,e.Font,e.Bounds,e.Index,e.State ^ DrawItemState.Selected,e.ForeColor,Color.Yellow);//Choose the color

    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Draw the current item text
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),Brushes.Black,StringFormat.GenericDefault);
    // If the ListBox has focus,draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

原文地址:https://www.jb51.cc/csharp/94823.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐