简体   繁体   English

禁用时更改ListView C#的背景颜色

[英]Changing background color of listview c# when disabled

How to change the background color of a Listview Control in C# when the Control is disabled?? 禁用控件后,如何在C#中更改Listview控件的背景色?

The color of a textbox can be changed when it is disabled but when a listview is disabled it goes grey and we can't apply any color to it.So is there a way to change background color of Listview control when disabled?? 禁用时,可以更改文本框的颜色,但是禁用列表视图时,它会变成灰色,我们无法对其应用任何颜色。因此,有没有办法在禁用时更改列表视图控件的背景颜色?

I've tried with overriding OnPaint , OnPaintBackground but the BackColor is still no change. 我尝试覆盖OnPaintOnPaintBackground但是BackColor仍然没有变化。 Even WM_PAINT can change it but the Item backcolor is not identical to the listview BackColor. 甚至WM_PAINT都可以更改它,但是Item背景色与列表视图BackColor不相同。 I had thought of this solution before although it is just some kind of hack but it seems to be the only working solution, the whole idea is to use a Background Image instead: 我之前曾想过这个解决方案,尽管它只是某种骇客,但它似乎是唯一可行的解​​决方案,整个想法是使用Background Image代替:

    Bitmap bm = new Bitmap(listView1.ClientSize.Width, listView1.ClientSize.Height);
    Graphics.FromImage(bm).Clear(listView1.BackColor);
    listView1.BackgroundImage = bm;

If you want to create your own ListView which supports BackColor in disabled state, here is the class: 如果要创建自己的支持禁用状态下的BackColor的ListView ,请参见以下类:

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;  
          }
      }
   }
}

If someone has another solution, I would like to know also. 如果有人有其他解决方案,我也想知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM