What's the reason for this?
I override OnPaintBackground and draw a string. It doesn't show up until I call this in the constructor:
this.SetStyle ( ControlStyles.UserPaint, true );
But then I don't see the items in the listview.
Why and how to solve this?
EDIT: code
protected override void OnPaintBackground ( PaintEventArgs pevent )
{
base.OnPaintBackground ( pevent );
// Create string to draw.
String drawString = "76";
// Cr开发者_运维技巧eate font and brush.
Font drawFont = new Font ( "Arial", 36 );
SolidBrush drawBrush = new SolidBrush ( Color.Blue );
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF ( 150.0F, 150.0F );
// Draw string to screen.
pevent.Graphics.DrawString ( drawString, drawFont, drawBrush, drawPoint );
//pevent.Graphics.FillRectangle ( drawBrush, this.ClientRectangle );
}`enter code here`
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 :)
精彩评论