.net 如何更改组合框背景颜色(不仅仅是下拉列表部分)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6468024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change combobox background color (not just the drop down list part)
提问by JBB
In a winform application running on windows 7 I want the change the background color of a combobox to highlight it. The comboxbox has a DropDownStyle of DropDownList.
在 Windows 7 上运行的 winform 应用程序中,我希望更改组合框的背景颜色以突出显示它。comboxbox 具有 DropDownList 的 DropDownStyle。
When I programmatically change the BackColor property to Red, only the background of the actual drop down list is changed to Red. When the drop down list is not opened, the combobox background displaying the selected value remains grey. What can I do so it becomes red too?
当我以编程方式将 BackColor 属性更改为红色时,只有实际下拉列表的背景更改为红色。未打开下拉列表时,显示所选值的组合框背景保持灰色。我该怎么做才能让它也变红?
Setting the BackColor property works fine when app is run on Windows XP
当应用程序在 Windows XP 上运行时,设置 BackColor 属性工作正常
采纳答案by Igby Largeman
This should get you started.
这应该让你开始。
Change the combobox DrawMode property to OwnerDrawFixed, and handle the DrawItem event:
将组合框 DrawMode 属性更改为 OwnerDrawFixed,并处理 DrawItem 事件:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
int index = e.Index >= 0 ? e.Index : 0;
var brush = Brushes.Black;
e.DrawBackground();
e.Graphics.DrawString(comboBox1.Items[index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
The background color will be right but the style of the box will be flat, not the usual 3D style.
背景颜色会是正确的,但框的样式将是扁平的,而不是通常的 3D 样式。
回答by bambams
Since you lose the 3D effects anyway with Igby Largeman's solution you're better off changing the FlatStyleproperty to Flat. The background color seems to be obeyed even in Windows 7 that way, and without re-implementing any low-level events.
由于无论如何使用 Igby Largeman 的解决方案都会丢失 3D 效果,因此最好将FlatStyle属性更改为Flat. 即使在 Windows 7 中,背景颜色似乎也遵循这种方式,并且无需重新实现任何低级事件。
I would consider this a bug on Microsoft's part...
我认为这是微软方面的一个错误......
回答by blind Skwirl
I played around with this for a while and didn't want to do anything too involved. Those ideas above probably work but all I did was change the flatStyle property from "standard" to "flat".
我玩了一段时间,不想做任何太复杂的事情。上面的那些想法可能有效,但我所做的只是将 flatStyle 属性从“标准”更改为“平面”。
Although not perfect, it at least changes the background that grey/disabled look to white.
虽然不完美,但它至少将灰色/禁用外观的背景更改为白色。
You can see the comparison here:
您可以在此处查看比较:
Heating Source #1 > DropdownList > flat (the final decision since dropdown was allowing users to enter bad data)
加热源 #1 > DropdownList > flat(因为下拉菜单允许用户输入错误数据,所以最终决定)
Heater Source #2 > Dropdown > Standard (the default which looks nice)
Heater Source #2 > Dropdown > Standard(默认看起来不错)
Housing Type > Dropdown > Flat
房屋类型 > 下拉菜单 > 平面
Heating Source #1 Vendor > DropdownList > Standard (the default which looks disabled grey)
热源 #1 供应商 > 下拉列表 > 标准(默认看起来禁用灰色)
回答by bob number 2
Igby Largeman's answer got me 95% of the way there. And credit to Sasha Bond for the Brush colouring for setting the HighlightText colour when selected.
Igby Largeman 的回答让我达到了 95%。并归功于 Sasha Bond 的笔刷着色,用于在选择时设置 HighlightText 颜色。
Some improvements I made to get me to 100% are adding brush colour from the ComboBox's ForeColor and handling when the index is -1 (and setting it to -1 to start with so it behaves exactly like a normal dropdownstyle ComboBox).
我为使我达到 100% 所做的一些改进是从 ComboBox 的 ForeColor 添加画笔颜色并在索引为 -1 时进行处理(并将其设置为 -1 以开始,因此它的行为与正常的下拉样式 ComboBox 完全一样)。
Best of all, when setting this back to a standard dropdown style, it still behaves normally.
最重要的是,当将此设置回标准下拉样式时,它仍然表现正常。
private void comboBox1_DrawItem ( object sender, DrawItemEventArgs e )
{
int index = e.Index >= 0 ? e.Index : -1;
Brush brush = ( ( e.State & DrawItemState.Selected ) > 0 ) ? SystemBrushes.HighlightText : new SolidBrush ( comboBox1.ForeColor );
e.DrawBackground ();
if ( index != -1 )
{
e.Graphics.DrawString ( comboBox1.Items[index].ToString (), e.Font, brush, e.Bounds, StringFormat.GenericDefault );
}
e.DrawFocusRectangle ();
}
回答by Sasha Bond
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
var cmb = (ComboBox) sender;
if (cmb == null) return;
if (e.Index % 2 == 0)
{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
e.Graphics.DrawString(cmb.Items[e.Index].ToString(), cmb.Font, SystemBrushes.GrayText, e.Bounds);
}
else
{
e.DrawBackground();
// change background color
e.Graphics.FillRectangle(Brushes.AntiqueWhite, e.Bounds);
// change foreground color
Brush brush = ((e.State & DrawItemState.Selected) > 0) ? SystemBrushes.HighlightText : SystemBrushes.ControlText;
e.Graphics.DrawString(cmb.Items[e.Index].ToString(), cmb.Font, brush, e.Bounds);
e.DrawFocusRectangle();
}
}


