You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现WinForms ListView深色滚动条?VS2022示例参考

怎么给WinForms ListView弄出深色滚动条

你看到VS2022工具箱项窗口里的ListView深色滚动条,其实可以通过几种方式在标准WinForms里实现:

方式一:跟着系统深色主题走

WinForms控件可以适配系统的深色视觉样式,操作很简单:

  • 首先确保项目里已经调用了Application.EnableVisualStyles()(一般新建项目默认就有,放在Program.cs的Main方法里,要在初始化窗体之前调用)。
  • 然后修改应用的manifest文件,让程序支持深色主题:
    1. 项目里没有app.manifest的话,先添加一个(右键项目→添加→新建项→选“应用程序清单文件”)。
    2. 找到<application>节点,替换成下面的内容:
      <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings>
          <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
          <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
          <themeAware xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">true</themeAware>
        </windowsSettings>
      </application>
      
    3. 保存后,当系统切换到深色主题时,ListView的滚动条就会自动变成深色的了。

方式二:强制显示深色滚动条(不管系统主题)

如果不想受系统主题影响,硬要让滚动条是深色的,可以用P/Invoke改控件的窗口属性:

  • 先写个工具类,把需要的系统调用封装好:
    using System.Runtime.InteropServices;
    
    public static class DarkScrollbarHelper
    {
        private const int GWL_EXSTYLE = -20;
        private const int WS_EX_NOINHERITLAYOUT = 0x100000;
        private const int WM_SETTINGCHANGE = 0x001A;
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
        public static void TurnOnDarkScrollbar(IntPtr controlHandle)
        {
            // 修改控件扩展样式
            int exStyle = GetWindowLong(controlHandle, GWL_EXSTYLE);
            exStyle |= WS_EX_NOINHERITLAYOUT;
            SetWindowLong(controlHandle, GWL_EXSTYLE, exStyle);
    
            // 通知控件更新样式
            SendMessage(controlHandle, WM_SETTINGCHANGE, IntPtr.Zero, IntPtr.Zero);
        }
    }
    
  • 然后在ListView的HandleCreated事件里调用这个方法:
    private void listView1_HandleCreated(object sender, EventArgs e)
    {
        DarkScrollbarHelper.TurnOnDarkScrollbar(listView1.Handle);
    }
    
    提示:如果加了WS_EX_LAYOUTRTL导致控件布局乱了,直接删掉这个常量就行,只保留WS_EX_NOINHERITLAYOUT也能生效。

方式三:用现成的第三方控件

嫌自己写代码麻烦的话,直接用第三方WinForms控件库,比如DevExpress WinForms、Telerik UI for WinForms,这些库的ListView控件自带深色主题支持,直接拖过来用就行。


内容的提问来源于stack exchange,提问作者totalZero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:17:30