Click here to Skip to main content
11,920,377 members (49,856 online)
Click here to Skip to main content
Add your own
alternative version


66 bookmarked

Generating missing Paint event for TreeView and ListView controls

, 14 Jul 2003
Rate this:
Please Sign up or sign in to vote.
Article on generating missing Paint event for TreeView, ListView

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 ) {
            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.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.

    //removes flicker

Also the Paint event was added to restore browsable attributes.

//Re-enable Attributes for the Paint Event
EditorBrowsableAttribute( EditorBrowsableState.Always ),
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.


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
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralCustom DrawNod Pin
DblK29-Jun-09 11:43
memberDblK29-Jun-09 11:43 
benchserv18-May-08 18:17
memberbenchserv18-May-08 18:17 
Questionget internalgraphics with alpha? Pin
boxed19-Jul-06 4:31
memberboxed19-Jul-06 4:31 
GeneralFlickerfree Treeview Pin
Ivi2210-Jul-06 1:17
memberIvi2210-Jul-06 1:17 
GeneralUpdate Pin
Ivi226-Aug-07 3:10
memberIvi226-Aug-07 3:10 
QuestionListView Pain Evetn in VB.NET Pin
k^s4-May-06 4:50
memberk^s4-May-06 4:50 
QuestionRe: ListView Pain Evetn in VB.NET Pin
k^s4-May-06 7:10
memberk^s4-May-06 7:10 
AnswerRe: ListView Pain Evetn in VB.NET Pin
Graznok29-Sep-11 10:42
memberGraznok29-Sep-11 10:42 
QuestionLegal info? Pin
Filip Fracz30-Jan-06 11:47
memberFilip Fracz30-Jan-06 11:47 
Generalorganization chart (horizontal tree view) Pin
cahnakal4-Oct-05 0:23
membercahnakal4-Oct-05 0:23 
GeneralRe: organization chart (horizontal tree view) Pin
cahnakal4-Oct-05 0:38
membercahnakal4-Oct-05 0:38 
QuestionList View OnPaint Event? Pin
Almutalibi30-Jun-05 4:37
memberAlmutalibi30-Jun-05 4:37 
AnswerRe: List View OnPaint Event? Pin
brutalis20-Sep-05 0:03
memberbrutalis20-Sep-05 0:03 
GeneralJust to note how to do it in another way Pin
Nacho Nachev17-Jan-05 0:17
memberNacho Nachev17-Jan-05 0:17 
GeneralThings to do to make it work for VB.NET Pin
Florent Geffroy17-Aug-04 4:20
memberFlorent Geffroy17-Aug-04 4:20 
GeneralGradient background Pin
Dieg24-Nov-03 11:16
memberDieg24-Nov-03 11:16 
QuestionWhat about OwnerData? Pin
Trevor Magnusson1-Oct-03 14:34
memberTrevor Magnusson1-Oct-03 14:34 
GeneralGraphic Artifacts Problem Pin
Liumas29-Aug-03 8:44
memberLiumas29-Aug-03 8:44 
GeneralRe: Graphic Artifacts Problem Pin
Liumas29-Aug-03 9:02
memberLiumas29-Aug-03 9:02 
GeneralRe: Graphic Artifacts Problem Pin
Terry Henning4-Sep-03 17:15
sussTerry Henning4-Sep-03 17:15 
GeneralTreeNode Paint event Pin
Mikael Wiberg16-Jul-03 15:59
memberMikael Wiberg16-Jul-03 15:59 
GeneralRe: TreeNode Paint event Pin
nickafx4-Jan-05 23:39
membernickafx4-Jan-05 23:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151120.1 | Last Updated 15 Jul 2003
Article Copyright 2003 by J Young
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid