How repaint the color of ColumnHead in ListView?
-
i want to write my own listview control that i can repaint the columnhead's color. but i dont know how to do it. i use Graphics.FillRectangle() in the OnPaint() method but did not get the correct result.
The
ListView
class encapsulates the List-View common control. Sticking to pure managed code won't accomplish what you need. You must extend theListView
control, overrideWndProc
, and handle windows messages like you would in VC++. There are plenty of articles here on CodeProject about this, and you should familiarize yourself with the windows messages and common controls by reading Windows Controls[^]. For one example, see a previous post about defining an event to fire when column widths change: http://www.codeproject.com/script/comments/forums.asp?msg=807702&forumid=1649#xx807702xx[^]. Another good article about handling notification messages for the List-View common control (because that's really what this all comes down to) is http://www.codeproject.com/cs/miscctrl/listviewfilter.asp[^]. In your case, since you're paining, you'll need to handle theNM_CUSTOMDRAW
message (for a list-view). This will, once you've handle the right sequence of windows messages, give you anHDC
(handle to a device context). You can get aGraphics
object from this by usingGraphics.FromHdc
, but simply overriding theOnPaint
method (or handlig thePaint
event, which is much slower and a bad idea when done in a derived control) will not work. You must draw to the DC that the list-view is using for the list-view column header.Microsoft MVP, Visual C# My Articles