As I said in the last thread about this subject, OnPaint()
and UserPaint
don't work with ListView. The painting is handled by the underlying control and cannot be intercepted in that fashion. This is different to other Controls
So, when ControlStyles.UserPaint
is true
, the underlying control is not told to redraw itself. Instead, all drawing is routed to the OnPaintBackground()
and OnPaint()
methods, which -- as you have found -- do nothing.
There are two ways to do what you asked (the second is better than the first):
First way: Intercept the WM_PAINT
, do the base processing, and then draw onto the listview. Something like this:
public class MyListView : ListView
{
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x0F: // WM_PAINT
this.HandlePaint(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected virtual void HandlePaint(ref Message m) {
base.WndProc(ref m);
using (Graphics g = this.CreateGraphics()) {
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
g.DrawString("Some text", new Font("Tahoma", 13),
SystemBrushes.ControlDark, this.ClientRectangle, sf);
}
}
}
But this gives repaint problems when what you draw is outside the area that the listview thinks holds the control's contents -- it doesn't trigger paint events.
Second manner: Intercept the CustomDraw notification (this is not the same as OwnerDraw), and listen for the CDDS_POSTPAINT
stage. In that stage you can safely draw onto the list view. You can look at the code of ObjectListView to see how it is done.
You could also just save yourself a lot of bother and use an ObjectListView directly :)
Best Answer
I have created a zip file:
Than I have downloded that zip file: