5,557,174 members and growing! (16,388 online)
Email Password   helpLost your password?
Desktop Development » List Controls » ListView controls     Intermediate

Generating missing Paint event for TreeView and ListView controls

By J Young

Article on generating missing Paint event for TreeView, ListView
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 14 Jul 2003
Updated: 14 Jul 2003
Views: 90,378
Bookmarked: 48 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 5.37 Rating: 4.13 out of 5
2 votes, 10.0%
1
1 vote, 5.0%
2
0 votes, 0.0%
3
3 votes, 15.0%
4
14 votes, 70.0%
5

Microsoft .NET forms controls like TreeView and Listview are just wrappers around the controls in ComCtl. As such they do not normally invoke the Paint event. The only suggestion I have seen posted is to set the ControlStyles.UserPaint style and do all the drawing yourself!

TreeViewWithPaint Control

To solve this problem a internal Graphics object based on a Bitmap was used. It is re-created during any Resize.

//Recreate internal graphics object

protected override void OnResize( System.EventArgs e ) {
    if( internalBitmap == null  ||
        internalBitmap.Width != Width || internalBitmap.Height != Height ) {

        if( Width != 0 && Height != 0 ) {
            DisposeInternal();
            internalBitmap = new Bitmap( Width, Height );
            internalGraphics = Graphics.FromImage( internalBitmap );
        }
    }
}

When the control receives a WM_PAINT, three steps are performed:

  1. The ComCtl is painted into the internal Graphics object via a WM_PRINTCLIENT message.
    //Draw Internal Graphics
    
    IntPtr hdc = internalGraphics.GetHdc();
    Message printClientMessage = Message.Create( Handle, 
         WM_PRINTCLIENT, hdc, IntPtr.Zero );  
    DefWndProc( ref printClientMessage );
    internalGraphics.ReleaseHdc( hdc );
  2. The OnPaint() is now invoked using PaintEventArgs constructed from the internal Graphics object.
    //Add the missing OnPaint() call
    
    OnPaint( new PaintEventArgs( internalGraphics, Rectangle.FromLTRB( 
        updateRect.left,
        updateRect.top,
        updateRect.right,
        updateRect.bottom ) ) );
  3. The Bitmap of the internal Graphics object is copied to the normal screen Graphics device.
    //Draw Screen Graphics
    
    screenGraphics.DrawImage( internalBitmap, 0, 0 );

Also the WM_ERASEBKGND was filtered out to remove flicker.

case WM_ERASEBKGND:
    //removes flicker

    return;

Also the Paint event was added to restore browsable attributes.

[
//Re-enable Attributes for the Paint Event

EditorBrowsableAttribute( EditorBrowsableState.Always ),
BrowsableAttribute(true)
]
public new event PaintEventHandler Paint {
    add   { base.Paint += value; }
    remove{ base.Paint -= value; }
}

Using the code

To use the TreeViewWithPaint control:

  1. Just add the control to the toolbox.
  2. Drag it on to your form.
  3. Attach a Paint handler to the now exposed Paint event.

To create a <AnotherComCtl>WithPaint, modify TreeViewWithPaint as follows:

  1. Use <AnotherComCtl> as the base class.
  2. Copy the <AnotherComCtl> class attributes to the <AnotherComCtl>WithPaint class.
  3. Add an <AnotherComCtl>WithPaint.bmp, which is a 16x16 bit map used for the toolbox icon.

TreeViewWithPaint Control Test Bench

I created a simple Form containing a single TreeViewWithPaint control. The Paint event can now be used.

treeViewWithPaint1.Paint += new PaintEventHandler( 
       treeViewWithPaint1_Paint );

This particular Paint handler just draws a simple white band inside the selected node.

//Add a simple yellow band inside the selected node

private void treeViewWithPaint1_Paint(object sender, PaintEventArgs e) {
    Graphics g = e.Graphics;
    TreeNode node = treeViewWithPaint1.SelectedNode;
    
    if( node != null && node.IsVisible ) {
        using( Pen pen = new Pen( Color.Yellow ) ) {
            g.DrawRectangle( pen, 
                node.Bounds.X + 1,
                node.Bounds.Y + 1,
                node.Bounds.Width  - 3,
                node.Bounds.Height - 3
                );
        }
    }
}

Points of Interest

  • The needed attributes were discovered by using the VB (not C#) object browser for VS 2002 (not VS 2003).
  • There is an undocumented feature of XP that horizontal scroll bars appear on TreeView if you add items at design time.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

J Young



Location: United States United States

Other popular List Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralRIGHT TO LEFT DISPLAY NOT CORRECTmemberbenchserv18:17 18 May '08  
Generalget internalgraphics with alpha?memberboxed4:31 19 Jul '06  
GeneralFlickerfree TreeviewmemberIvi221:17 10 Jul '06  
GeneralUpdatememberIvi223:10 6 Aug '07  
QuestionListView Pain Evetn in VB.NETmemberk^s4:50 4 May '06  
QuestionRe: ListView Pain Evetn in VB.NETmemberk^s7:10 4 May '06  
GeneralLegal info?memberFilip Fracz11:47 30 Jan '06  
Generalorganization chart (horizontal tree view)membercahnakal0:23 4 Oct '05  
GeneralRe: organization chart (horizontal tree view)membercahnakal0:38 4 Oct '05  
GeneralList View OnPaint Event?memberAlmutalibi4:37 30 Jun '05  
GeneralRe: List View OnPaint Event?memberbrutalis0:03 20 Sep '05  
GeneralJust to note how to do it in another waymemberNacho Nachev0:17 17 Jan '05  
GeneralThings to do to make it work for VB.NETmemberFlorent Geffroy4:20 17 Aug '04  
GeneralGradient backgroundmemberDieg11:16 24 Nov '03  
GeneralWhat about OwnerData?memberTrevor Magnusson14:34 1 Oct '03  
GeneralGraphic Artifacts ProblemmemberLiumas8:44 29 Aug '03  
GeneralRe: Graphic Artifacts ProblemmemberLiumas9:02 29 Aug '03  
GeneralRe: Graphic Artifacts ProblemsussTerry Henning17:15 4 Sep '03  
GeneralTreeNode Paint eventmemberMikael Wiberg15:59 16 Jul '03  
GeneralRe: TreeNode Paint eventmembernickafx23:39 4 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jul 2003
Editor: Nishant Sivakumar
Copyright 2003 by J Young
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project