Introduction

Having recently spent sometime working with WTL, I relise their is a distinct lack of
documentation. In my my opinion WTL is the best solution so far to wrapping up Win32, and the
more documentation we get on the web, the better it'll be for all of us.

This article tackles the approach used to create custom drawn controls using WTL.

I've choosen to use a listView to link up with the article
Neat Stuff to do with List Controls using CustomDraw,
which demonstrates the same concept using MFC. I choose to keep this simple and would refer you to that document for more
examples on what you can do to improve the look and feel of the list view.

MyListControl

The WTL makes creating custom drawn controls very easy. The trick is to know which bits of code you need in
your project to make it happen. We begin by defining our own class for a CListViewCtrl, so we can keep
all the custom drawing in one place.

We have to inherit from CWindowImpl, otherwise we're not going to get any window messages. The most
important thing to note here is we also inherit from CCustomDraw. This along with CHAIN_MSG_MAP(CCustomDraw<MyListView>)
macro gives us the ability to neatly over ride the process of drawing the CListViewContol.

The CHAIN_MSG_MAP() macro routes the message to the default message map in the base class.

If you take a look inside at the code inside AtlCtrls.h at the class CCustomCtrl<T> you'll see it defines
the marco NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW, OnCustomDraw) which handles the custom draw notification. It splits
the message into it component part and calls a function for each. These are the functions you can overload to deal with the drawing
of your control.

Creating the Control

Most examples you see in MSDN and in other articles assume the control is created as a resource and
linked into you code. To be different I'm going to create this list box directly onto the main window.

Below is all the code we're going to add the wizard generated CMainFrame to create our custom drawn control.
Look out for the CHAIN_MSG_MAP_MEMBER(m_listView) macro. Miss it out and your class won't get the
NM_CUSTOMDRAW message. This macro routes the message to the default message map in the data member, which is what we want to do.

As a side note, we could have handled the NM_CUSTOMDRAW message at this level. Checked to see what the control id was and process
the painting as appropriate. This maybe useful in some cases, but its much neater to do what we have done. Keeping our code
clean and clear.

In the OnCreate() we just call create on the listView data member and fill in some demo data. Mission complete ...

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.