I have a ListView where I wish to tweak the drawing of items (for example highlighting certain strings in list view itmes), however I don\'t want to radically alter the way
I haven't got the time now to write up a complete answer so instead I'll put down some quick notes and come back to it later.
As LarsTech said, owner drawing a ListView
control is a pain - the .Net ListView
class is a wrapper around the underlying Win32 List View Control and the ability to "Owner draw" is provided by the NM_CUSTOMDRAW notification code. As such there is no "default .Net implementation" - the default is to use the underlying Win32 control.
To make life even more difficult there are a number of extra considerations to make:
DrawItem
and DrawSubItem
you may well be drawing the contents of the first cell twice.DrawItem
event will occur without corresponding DrawSubItem
events, meaning that if you draw a background in the DrawItem
event and then draw the text in the DrawSubItem
event your item text will disappear when you mouse over.DrawItem
event occurs first, anything you draw in the DrawItem
handler (e.g. the selection effect) may well be overlayed by things you do in the DrawSubItem
handler (e.g. having certain cells with a different background color).All in all handling owner drawing is a fairly involved affair - I found it best to handle all drawing inside the DrawSubItem
event, its also best to perform your own double-buffering by using the BufferedGraphics class.
I also found looking at the source code for ObjectListView very handy.
Finally, all of this is just to handle the details mode of the list view (the only mode I am using), if you want the other modes to work too then I believe that there are extra things to take account of.
When I get a chance I'll try and post my working example code.