【发布时间】:2013-07-04 04:30:47
【问题描述】:
当控件被禁用时,如何在 C# 中更改 Listview 控件的背景颜色?
文本框的颜色在禁用时可以更改,但是当列表视图被禁用时,它会变为灰色,我们无法对其应用任何颜色。那么有没有办法在禁用时更改 Listview 控件的背景颜色? ?
【问题讨论】:
标签: c# winforms listview .net-2.0
当控件被禁用时,如何在 C# 中更改 Listview 控件的背景颜色?
文本框的颜色在禁用时可以更改,但是当列表视图被禁用时,它会变为灰色,我们无法对其应用任何颜色。那么有没有办法在禁用时更改 Listview 控件的背景颜色? ?
【问题讨论】:
标签: c# winforms listview .net-2.0
我尝试过覆盖OnPaint、OnPaintBackground,但BackColor 仍然没有变化。即使WM_PAINT 可以更改它,但项目背景色与列表视图背景色不同。我之前想过这个解决方案,虽然它只是某种 hack,但它似乎是唯一可行的解决方案,整个想法是使用 Background Image 代替:
Bitmap bm = new Bitmap(listView1.ClientSize.Width, listView1.ClientSize.Height);
Graphics.FromImage(bm).Clear(listView1.BackColor);
listView1.BackgroundImage = bm;
如果你想创建自己的ListView,它支持禁用状态下的BackColor,这里是类:
public class MyListView : ListView {
public override Color BackColor {
get { return base.BackColor;}
set {
base.BackColor = value;
if(BackgroundImage == null){
Bitmap bm = new Bitmap(1,1);
bm.SetPixel(0,0,value);
BackgroundImage = bm;
BackgroundImageTiled = true;
}
}
}
public override Image BackgroundImage {
get { return base.BackgroundImage; }
set {
base.BackgroundImage = value;
if(value == null){
Bitmap bm = new Bitmap(1,1);
bm.SetPixel(0,0,BackColor);
BackgroundImage = bm;
BackgroundImageTiled = true;
}
}
}
}
如果有人有其他解决方案,我也想知道。
【讨论】: