Changing color of scroll bar in ListView in WinForms
Asked Answered
V

2

11

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.

Vagal answered 9/12, 2010 at 10:10 Comment(2)
see this #4087718Sandblind
I have seen this code github.com/notepad-plus-plus/notepad-plus-plus/blob/…Lignite
C
21

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.

Czar answered 9/12, 2010 at 10:50 Comment(4)
Cody Gray says it's not possible, it must not be possible.Owings
As a computer programmer and after 19 years of field experience, I totally agree to the part that a developer should respect the user's own preferences, even for the core functionalties of your application, along with the theming options. However Windows (even 10) comes with a very solid and (arguably) ugly (at least non-modern) theme and a very little percantage of users change (even know how to change) them. So I guess the best workaround would be giving a chance to your users when they want to change them. I did it for my listview column headers but now I'm suffering about the scroll bar...Daysidayspring
Generally agree, except when you are working on medical or imaging software, then different rules apply. You exclusively know the hardware, monitor, gfx, etc. and the output is calibrated for that device. For example not to distract user from the image he is working on, and not to change monitor brightness or contrast during that process, such sw tends to have darker interface (of course leaving user to choose from several others if he does like the default). PS: I think new OS UI developers are suffering from major chromosome brain damage, it perfectly works in their brains, but no in ours.Kephart
@Kephart In that case, you still modify the environment to appear as you want it to, not the individual application software under development.Czar
S
0

enter image description here

My workaround for this issue is to create a custom ScrollBar control https://mcmap.net/q/1015377/-change-the-background-color-of-a-vscrollbar 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://mcmap.net/q/1015378/-how-to-change-the-back-color-of-the-scrollbar-of-a-panel

Spiritualty answered 5/9, 2022 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.