更改 WinForms 中 ListView 中滚动条的颜色
在 Windows 窗体应用程序中,是否可以更改 ListView
控件的滚动条颜色?
请让我知道实现此目标需要什么属性或代码,我找不到,也无法开发类似的东西。
我的目的是将一个主题应用于我的整个 WinForms 应用程序。
In a Windows Forms application, is it possible to change the color of a scrollbar in for the ListView
control?
Please let me know what property or code I need for achieving this, I could not find and also was not able to develop anything like that.
My intention is to apply a theme to my whole WinForms application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短回答:不,这是不可能的。
更长(更准确)的答案:
不幸的是,这远不像设置属性那么简单。原因是
ListView
控件使用的滚动条不是实际的滚动条控件。它们没有自己的窗口句柄,并且它们的绘制完全由ListView
控件在内部管理。当然,WinForms 中的ListView
控件只是 Win32 API 提供的同一控件的薄包装。它们都没有公开任何更改滚动条颜色的工具。但情节变得更加复杂。我说过
ListView
控件(请注意,这也适用于TreeView
)处理滚动条本身的绘制,这应该表明您不能简单地处理它的Paint
事件并自己绘制它们,就像使用许多其他 WinForms 控件一样。Paint
事件对应于窗口消息WM_PAINT
,但还有另一个消息 (WM_NCPAINT
) 在非客户端时发送到窗口需要涂漆的区域。按理说,当收到WM_NCPAINT
消息时,就会绘制滚动条,因为我已经说过它们是ListView
控件框架的一部分。但他们不是。还有一个毫无意义的过度定制的问题。我强烈主张应用程序尊重用户当前的主题设置。如果我将 Windows 配置为使用高对比度主题(或者如果我无法忍受蓝色,因此我有一个绿色主题),我希望计算机上的应用程序使用该主题绘制其 UI 元素。无论您将滚动条设计成什么样子,无论它在您的计算机上看起来多么出色,它在某人的计算机上肯定看起来像垃圾。如果那个人是您的用户之一,哎呀。少数使用自定义主题的应用程序进行了大量测试,但这仍然无法阻止它们遇到问题。
您可能能够以某种方式挂钩滚动条本身,但这很可能是不可能的,因为它们部分是在内核模式下实现的。事实上,我听说 WindowBlinds 的作者声称挂钩滚动条是最困难的他们必须做的事情。他们当然不会分享他们的任何技巧。
唯一看起来真正有希望的是 James Brown 的 自定义滚动条库,但它是这样写的您自己将其移植到 C# 中。像往常一样,这种事情不会没有问题。看了评论,发现很多人都有各种各样的问题。如果您不太了解 Win32 API,您可能无法修复这可能引入到您的应用程序中的任何错误。
谷歌搜索这个主题几乎完全发现了未解答的问题,这是有原因的。无论如何,这在 WPF 中要容易得多。
The short answer: No, it's not possible.
The longer (and more accurate) answer:
Unfortunately, this is nowhere near as simple as setting a property. The reason is that the scroll bars used by the
ListView
control are not actual scroll bar controls. They don't have their own window handles, and their drawing is managed completely internally by theListView
control. And, of course, theListView
control in WinForms is just a thin wrapper around that same control provided by the Win32 API. Neither of them expose any facility for changing the scroll bar's color.But the plot thickens. I said that the
ListView
control (note that this also applies to theTreeView
) handles drawing the scroll bars itself, which should indicate that you can't simply handle itsPaint
event and draw them yourself like you can with many of the other WinForms controls. ThePaint
event corresponds to the window messageWM_PAINT
, but there is another message (WM_NCPAINT
) that is sent to a window when it's non-client area needs to be painted. It stands to reason that the scrollbars are painted when theWM_NCPAINT
message is received, because I've said they are part of theListView
control's frame. But they're not.Then there's the issue of pointlessly excessive customization. I'm a strong advocate for applications that respect the user's current theme settings. If I have Windows configured to use a high contrast theme (or if I just can't stand the color blue, so I have a green theme instead), I expect the applications on my computer to draw their UI elements using that theme. Whatever you design your scroll bar to look like, however awesome it looks on your computer, it's guaranteed to look like garbage on someone's computer. If that someone is one of your users, oops. The handful of applications that use custom themes do a lot of testing and that still doesn't keep them from experiencing problems.
You might be able to hook the scroll bars themselves somehow, but that's more than likely out of the question, as they are implemented partially in kernel mode. In fact, I've heard that the authors of WindowBlinds claimed hooking scroll bars was the most difficult thing they had to do. And they're certainly not sharing any of their tricks.
The only thing that really seems hopeful is James Brown's Custom Scrollbar Library, but it's written in C. You're on your own porting that to C#. And as usual with this kind of thing, it's not going to be without it's problems. Reading the comments shows quite a few people with various issues. If you don't know the Win32 API pretty well, you're probably not going to be able to fix any bugs this potentially introduces into your application.
There's a reason a Google search on this subject almost exclusively uncovers unanswered questions. For what it's worth, this is way easier in WPF.
我解决此问题的方法是创建自定义 ScrollBar 控件 https://stackoverflow.com/a/ 73613059/5514131支持自定义颜色和主题,然后在我们的Control内部创建自定义ScrollBars,并借助Control属性和事件,将自定义ScrollBars绑定到Control并将主题放置在默认之上控制滚动条完全覆盖它们。
我知道这不是最好的方法,但它应该可以防止默认的丑陋滚动条破坏我的黑暗模式。
请参阅我关于面板控件的完整答案,但是,此解决方法可以应用于其他控件。 https://stackoverflow.com/a/73613569/5514131
My workaround for this issue is to create a custom ScrollBar control https://stackoverflow.com/a/73613059/5514131 that supports custom colors and themes, then in our Control create the custom ScrollBars internally, and with the help of the Control properties and events, bind the custom ScrollBars to the Control and place theme on top of the default Control ScrollBars to completely cover them.
I know this isn't the best approach, but it should work to prevent the default ugly scrollbars from ruining my Dark Mode.
See my complete answer regarding the Panel control, however, this workaround can be applied to other controls. https://stackoverflow.com/a/73613569/5514131