开发者

Changing color of scroll bar in ListView in WinForms

开发者 https://www.devze.com 2023-01-29 10:08 出处:网络
In a Windows Forms application, is it possible to change the color of a scrollbar in for the ListView control?

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.


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 the ListView control. And, of course, the ListView 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 the TreeView) handles drawing the scroll bars itself, which should indicate that you can't simply handle its Paint event and draw them yourself like you can with many of the other WinForms controls. The Paint event corresponds to the window message WM_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 the WM_NCPAINT message is received, because I've said they are part of the ListView 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.


Changing color of scroll bar in ListView in WinForms

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

0

精彩评论

暂无评论...
验证码 换一张
取 消