Changing color of scroll bar in ListView in WinFor

2019-01-19 21:10发布

问题:

Is it possible to change the color of the scroll bar in a ListView control. It is a Windows application.

Please let me know what property or code I can use to do this.

can someone help u how to apply theme to winforms( to whole to my application) as in yahoo messenger/digby etc.

回答1:

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.