【问题标题】:Changing background color of listview c# when disabled禁用时更改listview c#的背景颜色
【发布时间】:2013-07-04 04:30:47
【问题描述】:

当控件被禁用时,如何在 C# 中更改 Listview 控件的背景颜色?

文本框的颜色在禁用时可以更改,但是当列表视图被禁用时,它会变为灰色,我们无法对其应用任何颜色。那么有没有办法在禁用时更改 Listview 控件的背景颜色? ?

【问题讨论】:

    标签: c# winforms listview .net-2.0


    【解决方案1】:

    我尝试过覆盖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;  
              }
          }
       }
    }
    

    如果有人有其他解决方案,我也想知道。

    【讨论】:

    • 我采取了不同的方法。我只是禁用了列表视图上的事件(拖放,单击事件),设置了一个布尔属性,声明禁用它。所以列表视图不会变灰,而且拖放、点击等功能全部禁用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2012-03-10
    • 2016-01-11
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多