Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListView OwnerDraw Question

ListView OwnerDraw Question

Scheduled Pinned Locked Moved C#
questionwpfgraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guinness4Strength
    wrote on last edited by
    #1

    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();
        }
    
    L 1 Reply Last reply
    0
    • G Guinness4Strength

      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();
          }
      
      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      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!

      G 1 Reply Last reply
      0
      • L LongRange Shooter

        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!

        G Offline
        G Offline
        Guinness4Strength
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups