Tech Off Post

Single Post Permalink

View Thread: Drawing on Controls
  • User profile image
    JParrish

    Well.. I couldn't stand this so when I got home I spent a little time working with it and realized that the best approach in my case is to track which item was last targetted and invalidate the region that I had painted over. It now works well and is much less intensive on resources. Something like this:

          Graphics g = this.CreateGraphics();
          Point currentpoint = this.PointToClient(new Point(e.X,e.Y));

          foreach(ListViewItem item in this.Items)
          {
            if(currentpoint.Y <= item.Bounds.Bottom && currentpoint.Y >= item.Bounds.Top)
            {
              if(item.Index != this.lastindextargetted)
              {
                g.DrawLine(new Pen(Brushes.Black,2),0,item.Bounds.Bottom,this.Width,item.Bounds.Bottom);
                if(this.lastindextargetted >= 0)
                {
                  this.Invalidate(new Rectangle(0,this.Items[this.lastindextargetted].Bounds.Bottom-2,this.Width,4));
                }

                this.lastindextargetted = item.Index;
              }
            }
          }