ListView OwnerDraw Question
-
I've derived my own GradientListView class from the ListView control to try and draw a gradient background. I'm able to draw the gradient background just fine by overriding the OnPaintBackground event, but the ListView items no longer appear in the ListView. Is it an all or nothing deal?....Do I need to handle all the painting events if I override one of them? If I comment out the SetStyle command in the constructor the ListView shows the items correctly, but obviously without the gradient background.
public GradientListView() { //set styles SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true); } protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) { PaintRectangle(this.ClientRectangle, this.BackColor, Color.Black, pevent.Graphics); } protected void PaintRectangle(Rectangle Rect, Color RectColor, Color RectBorderColor, Graphics g) { //draw rectangle Pen LinePen = new Pen(RectBorderColor, 10); g.DrawRectangle(LinePen, new Rectangle(Rect.X, Rect.Y, Rect.Width - 1, Rect.Height - 1)); LinePen.Dispose(); Rect = new Rectangle(Rect.X + 1, Rect.Y + 1, Rect.Width - 2, Rect.Height - 2); SolidBrush bgBrush = new SolidBrush(RectColor); g.FillRectangle(bgBrush, Rect); bgBrush.Dispose(); LinearGradientBrush brush; Rectangle BottomRect; BottomRect = new Rectangle(Rect.X, Rect.Height-((int)(Rect.Height / 3)), Rect.Width, Rect.Height / 3); brush = new LinearGradientBrush( new Point(BottomRect.Width / 2, BottomRect.Top - 1), new Point(BottomRect.Width / 2, BottomRect.Bottom), RectColor, ControlPaint.Dark(RectColor)); g.FillRectangle(brush, BottomRect); brush.Dispose(); }
-
I've derived my own GradientListView class from the ListView control to try and draw a gradient background. I'm able to draw the gradient background just fine by overriding the OnPaintBackground event, but the ListView items no longer appear in the ListView. Is it an all or nothing deal?....Do I need to handle all the painting events if I override one of them? If I comment out the SetStyle command in the constructor the ListView shows the items correctly, but obviously without the gradient background.
public GradientListView() { //set styles SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true); } protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) { PaintRectangle(this.ClientRectangle, this.BackColor, Color.Black, pevent.Graphics); } protected void PaintRectangle(Rectangle Rect, Color RectColor, Color RectBorderColor, Graphics g) { //draw rectangle Pen LinePen = new Pen(RectBorderColor, 10); g.DrawRectangle(LinePen, new Rectangle(Rect.X, Rect.Y, Rect.Width - 1, Rect.Height - 1)); LinePen.Dispose(); Rect = new Rectangle(Rect.X + 1, Rect.Y + 1, Rect.Width - 2, Rect.Height - 2); SolidBrush bgBrush = new SolidBrush(RectColor); g.FillRectangle(bgBrush, Rect); bgBrush.Dispose(); LinearGradientBrush brush; Rectangle BottomRect; BottomRect = new Rectangle(Rect.X, Rect.Height-((int)(Rect.Height / 3)), Rect.Width, Rect.Height / 3); brush = new LinearGradientBrush( new Point(BottomRect.Width / 2, BottomRect.Top - 1), new Point(BottomRect.Width / 2, BottomRect.Bottom), RectColor, ControlPaint.Dark(RectColor)); g.FillRectangle(brush, BottomRect); brush.Dispose(); }
If you have the control set to OwnerDraw then it means you will draw everything. Have no fear, though, since you can override Paint and then just call base.Paint(). See if that corrects the drawing problem. As to your user name.....BRILLIANT!
-
If you have the control set to OwnerDraw then it means you will draw everything. Have no fear, though, since you can override Paint and then just call base.Paint(). See if that corrects the drawing problem. As to your user name.....BRILLIANT!
Thanks for the response, unfortunately the drawing problem still persists even after overriding the OnPaint as you suggested as well as the Draw Events....see below:
class GradientListView:System.Windows.Forms.ListView { public GradientListView() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); } protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) { base.OnDrawColumnHeader(e); } protected override void OnDrawItem(DrawListViewItemEventArgs e) { base.OnDrawItem(e); } protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { base.OnDrawSubItem(e); } protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) { PaintRectangle(this.ClientRectangle, this.BackColor, Color.Black, pevent.Graphics); } protected void PaintRectangle(Rectangle Rect, Color RectColor, Color RectBorderColor, Graphics g) { LinearGradientBrush brush; Rectangle BottomRect; BottomRect = new Rectangle(Rect.X, Rect.Height-((int)(Rect.Height / 2)), Rect.Width, Rect.Height / 2); brush = new LinearGradientBrush( Rect, RectColor, Color.White, 45); g.FillRectangle(brush, Rect); brush.Dispose(); } }
Any other thoughts ? -- modified at 11:43 Monday 23rd October, 2006