Click here to Skip to main content
Licence 
First Posted 19 Dec 2004
Views 105,771
Bookmarked 29 times

CustomDraw ListView

By | 19 Dec 2004 | Article
An article that describes how to use custom draw controls in C#.

Screen Shot

Introduction

I wanted to customize the ListView control without going for the full owner draw approach. So custom draw was the obvious solution.

Background

I have done this sort of thing in ATL/WTL before, so I knew that I needed the parent of the control to reflect the notification message (.NET does by default). I also needed to handle the reflected notification message OCM_NOTIFY to perform my custom drawing. I stumbled across an interesting article by Georgi Atanasov, Customizing the header control in a ListView. So with the help of this article, I created my own sample ListView control that basically changes the font of the item text and sets the background color to that beautiful aqua color!!

Using the code

The EventListView.cs contains the ListView derived control and implements the ICustomDraw interface. The ICustomDraw interface contains the various stages of custom drawing that can occur:

int OnPrePaint(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnPostPaint(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnPreErase(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnPostErase(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnItemPrePaint(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnItemPostPaint(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnItemPreErase(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);
int OnItemPostErase(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd);

It also contains a property Handle which is used to test that the reflect message really is for this control.

IntPtr Handel { get; }

EventListView class creates an object of type CustomDrawHandler and overrides the WndProc procedure and passes the message to this CustomDrawHandler.

protected override void WndProc(ref Message m)
{
    m_custDrawHandler.HandleReflectedCustDrawMessage(ref m);

    if (!m_custDrawHandler.MessageHandled)
        base.WndProc (ref m);
}

The HandleReflectedCustDrawMessage function checks to see if we have an OCM_NOTIFY message and that the message code is for CustomDraw. It then checks the draw stage and calls back onto the interface that was passed to its constructor, i.e., the ListView derived control. The EventListView.OnPrePaint function returns CDRF_NOTIFYITEMDRAW which causes the OnItemPrePaint function to be called. The OnItemPrePaint changes the background color of the item and draws the item text in an Arial font.

History

This is the first version of the custom draw in C#. Please feel free to email me with any comments or suggestions.

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

Brian Keating

Web Developer

Ireland Ireland

Member

Currently working for bfa Ireland.
C++/C# mainly.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalclean code Pinmemberflyingxu20:35 10 Mar '09  
GeneralImplementing Events to the EventListView parent PinmemberS Fewings17:10 7 Jan '09  
QuestionHowto determine if PREPAINT pass is item background paint or item draw paint? PinmemberJRQ8:37 14 Nov '06  
GeneralThanks Pinmemberbogdan_toda2:22 10 Jul '06  
GeneralListView Item Height Pinmemberjfulton994:48 17 May '06  
GeneralRe: ListView Item Height Pinmembermahika12:54 11 Dec '06  
GeneralRe: ListView Item Height Pinmemberjfulton9912:59 11 Dec '06  
GeneralChanging Item Bounds Pinmemberpeachfuzz1:22 27 Aug '05  
GeneralSubitem Painting PinmemberandrewPP4:38 24 Aug '05  
GeneralRe: Subitem Painting Pinmember-=777=-22:06 8 Nov '05  
QuestionItem no longer highlighted? PinmemberandrewPP7:09 23 Aug '05  
AnswerRe: Item no longer highlighted? PinmemberandrewPP4:37 24 Aug '05  
GeneralRe: Item no longer highlighted? PinmemberCodeMonkey46198216:00 25 Feb '07  
GeneralScroll bar handling PinmemberStar931:35 19 Jul '05  
Generalhandling subitems PinsussAnonymous4:36 12 May '05  
GeneralRe: handling subitems Pinmembertrip426:42 15 Jun '05  
QuestionWhy ? Pinsussphlaphi23:00 2 Jan '05  
AnswerRe: Why ? PinmemberBrian Keating22:04 3 Jan '05  
GeneralCompact FrameWork Pinmemberdandolo0:30 20 Dec '04  
GeneralRe: Compact FrameWork PinmemberBrian Keating0:42 20 Dec '04  
GeneralRe: Compact FrameWork Pinmemberdandolo1:18 24 Dec '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Dec 2004
Article Copyright 2004 by Brian Keating
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid