c# - 更改 ListView 标题和网格线颜色

标签 c# winforms

我有一个 Windows 窗体应用程序,我在其中添加了几个 listView 以便为用户保存一些数据,它看起来像这样

enter image description here

如您所见,我的表单背景色是黑色的,因此 ListView 的网格线和标题的白色形成了令人讨厌的对比,所以在搜索了一个小时但没有成功后,我决定在这里提问。

[问题]:如何编辑 ListView 的标题和网格线的颜色以满足我的需要?

最佳答案

看起来没有人对自定义 ListView 以支持 Grid Line Color 感兴趣。我试过这个,想在这里分享。当您滚动 ListView 项目时,有点闪烁(不是很多)并不是很好。不过是可以接受的。我想我在这里缺乏一些win32的知识,让它更完美:

public class CustomListView : ListView {
        bool scrollDown;
        int lastScroll;
        public Color GridLinesColor {get;set;}
        [DllImport("user32")]
        private static extern int GetScrollPos(IntPtr hwnd, int nBar);
        public CustomListView(){
           GridLinesColor = Color.Red;
           DoubleBuffered = true;
           base.GridLines = false;//We should prevent the default drawing of gridlines.
        }
        public new bool GridLines {get;set;}
        protected override void WndProc(ref Message m)
        {                
            if (m.Msg == 0x20a){//WM_MOUSEWHEEL = 0x20a
                scrollDown = (m.WParam.ToInt64() >> 16) < 0;
            }
            if (m.Msg == 0x115){//WM_VSCROLL = 0x115
                int n = (m.WParam.ToInt32() >> 16);
                scrollDown = n > lastScroll;
                lastScroll = n;
            }                
            base.WndProc(ref m);
            if (m.Msg == 0xf && GridLines && Items.Count > 0&&View==View.Details)//WM_PAINT = 0xf
            {                    
                using (Graphics g = CreateGraphics())
                {
                    using(Pen p = new Pen(GridLinesColor)){
                      int w = -GetScrollPos(Handle, 0);
                      for (int i = 0; i < Columns.Count; i++)
                      {
                        w += Columns[i].Width;
                        g.DrawLine(p, new Point(w, 0), new Point(w, ClientSize.Height));
                      }
                      int a = Items[0].Bounds.Bottom - 1;
                      int b = Height - Items[0].Bounds.Y;
                      int c = Items[0].Bounds.Height;
                      for (int i = scrollDown ? a + (b/c) * c : a ; scrollDown ? i >= a : i < b ; i += scrollDown ? -c : c)
                      {
                        g.DrawLine(p, new Point(0, i), new Point(ClientSize.Width, i));
                      }                                      
                    }          
                }                 
            }

        }
}

更新:感谢 Cody Gray 的建议,我添加了代码来处理水平滚动。为简单起见,我使用 GetScrollPos,因为根据 MSDN 文档页面的建议,我们应该改用 GetScrollInfo

enter image description here

关于c# - 更改 ListView 标题和网格线颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18283781/

相关文章:

c# - 如何填充 C# Windows 窗体组合框?

c# - 添加新项目时防止 ListView 自动水平滚动

c# - 将用户控件添加到 Windows 窗体 C# 时出错

c# - WPF 上下文菜单位置不正确

c# - 在数据库中存储数组的最佳方式

java - 等同于 Java 中的方法 RSACryptoServiceProvider signHash

c# - 如何在 Windows 窗体 C# 中打印可滚动面板

c# - 加载 .html,带有 css 引用,文件嵌入在资源中

c# - 使用 System.Speech.Recognition 打开 Windows 语音识别

c# - 通过单独的程序集充分利用 ASP.NET MVC DataAnnotations