如何更改列表框选择背景颜色? [英] How to change ListBox selection background color?

查看:322
本文介绍了如何更改列表框选择背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来使用默认的颜色从Windows设置,默认情况下是蓝色的。
比方说,我想将它永久更改为红色。我使用的WinForms。

It seems to use default color from Windows settings which is blue by default. Let's say I want to change it to red permanently. I'm using Winforms.

先谢谢了。

推荐答案

您必须覆盖<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.drawitem.aspx\"><$c$c>Drawitem事件并设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.drawmode.aspx\"><$c$c>DrawMode属性<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.drawmode.aspx\"><$c$c>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(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

这篇关于如何更改列表框选择背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆