Click here to Skip to main content
15,891,704 members
Articles / Programming Languages / C#
Article

CustomDraw ListView

Rate me:
Please Sign up or sign in to vote.
3.57/5 (8 votes)
19 Dec 20042 min read 150.5K   2.1K   33   21
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:

C#
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.

C#
IntPtr Handel { get; }

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

C#
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


Written By
Web Developer
Ireland Ireland
Currently working for bfa Ireland.
C++/C# mainly.

Comments and Discussions

 
Generalclean code Pin
flyingxu10-Mar-09 20:35
flyingxu10-Mar-09 20:35 
GeneralImplementing Events to the EventListView parent Pin
S Fewings7-Jan-09 17:10
S Fewings7-Jan-09 17:10 
QuestionHowto determine if PREPAINT pass is item background paint or item draw paint? Pin
JRQ14-Nov-06 8:37
JRQ14-Nov-06 8:37 
GeneralThanks Pin
bogdan_toda10-Jul-06 2:22
bogdan_toda10-Jul-06 2:22 
GeneralListView Item Height Pin
jfulton9917-May-06 4:48
jfulton9917-May-06 4:48 
GeneralRe: ListView Item Height Pin
jqritual11-Dec-06 12:54
jqritual11-Dec-06 12:54 
GeneralRe: ListView Item Height Pin
jfulton9911-Dec-06 12:59
jfulton9911-Dec-06 12:59 
Mahika,

Thanks for your response. I will try this.

-John

John F.

GeneralChanging Item Bounds Pin
peachfuzz27-Aug-05 1:22
peachfuzz27-Aug-05 1:22 
GeneralSubitem Painting Pin
andrew|24-Aug-05 4:38
andrew|24-Aug-05 4:38 
GeneralRe: Subitem Painting Pin
Roman Lerman8-Nov-05 22:06
Roman Lerman8-Nov-05 22:06 
QuestionItem no longer highlighted? Pin
andrew|23-Aug-05 7:09
andrew|23-Aug-05 7:09 
AnswerRe: Item no longer highlighted? Pin
andrew|24-Aug-05 4:37
andrew|24-Aug-05 4:37 
GeneralRe: Item no longer highlighted? Pin
CodeMonkey46198225-Feb-07 16:00
CodeMonkey46198225-Feb-07 16:00 
GeneralScroll bar handling Pin
93Current19-Jul-05 1:35
93Current19-Jul-05 1:35 
Generalhandling subitems Pin
Anonymous12-May-05 4:36
Anonymous12-May-05 4:36 
GeneralRe: handling subitems Pin
Member 36299615-Jun-05 6:42
Member 36299615-Jun-05 6:42 
QuestionWhy ? Pin
phlaphi2-Jan-05 23:00
sussphlaphi2-Jan-05 23:00 
AnswerRe: Why ? Pin
Brian Keating3-Jan-05 22:04
Brian Keating3-Jan-05 22:04 
GeneralCompact FrameWork Pin
dandolo20-Dec-04 0:30
dandolo20-Dec-04 0:30 
GeneralRe: Compact FrameWork Pin
Brian Keating20-Dec-04 0:42
Brian Keating20-Dec-04 0:42 
GeneralRe: Compact FrameWork Pin
dandolo24-Dec-04 1:18
dandolo24-Dec-04 1:18 

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.