【问题标题】:Change background selection color of ListView?更改 ListView 的背景选择颜色?
【发布时间】:2010-03-27 18:05:34
【问题描述】:

如何更改 ListView 上的选择颜色?默认情况下,当用户选择一个项目时,它会显示蓝色背景。我想把它改成深灰色,或者什么... 感谢您的帮助!

【问题讨论】:

    标签: c# winforms listview colors selection


    【解决方案1】:

    如果您希望您的 ListView 具有 Windows 资源管理器 ListView 的风格(包括 Win7/Vista 中带有圆边的漂亮外观),您可以使用一点 P/Invoke 来实现:

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    internal static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);
    
    // You can subclass ListView and override this method
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        SetWindowTheme(this.Handle, "explorer", null);
    }
    

    【讨论】:

      【解决方案2】:

      对于 WinForms,您必须将 OwnerDraw 属性设置为 true,然后使用 DrawItemDrawSubItem 事件手动绘制项目。

      See here 为例。

      【讨论】:

        【解决方案3】:

        ObjectListView -- WinForm ListView 的包装器 -- 具有让您控制所选行的背景和前景色的属性。它使用了 Obalix 建议的技术,但它已经为您完成了艰苦的工作。

        因此,只需稍加努力,您就可以制作出这样的东西:

        “Feel Good Inc”行显示自定义前景和背景供选择。

        【讨论】:

        • ObjectListView 不是ListView 的直接替代品。使用它可能需要对现有代码进行一些重构。
        • 我分析了这个项目。但是我觉得学起来太难了。
        猜你喜欢
        • 1970-01-01
        • 2018-03-04
        • 2012-10-01
        • 2021-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        • 2016-01-11
        相关资源
        最近更新 更多