Click here to Skip to main content
14,981,532 members
Articles / Desktop Programming / WTL
Posted 17 Nov 2003


23 bookmarked

Sort List Control

Rate me:
Please Sign up or sign in to vote.
4.82/5 (20 votes)
17 Nov 20033 min read
A list control with sorting indicator.

Sample Image - sortlistviewctrl.jpg


First thing, I would like to apologize for any grammar/spelling mistakes in the article. Since English isn't my native language, I tend to do a lot of mistakes while typing.

CListViewCtrl is a nice control, used mainly in report mode. You can see it filled with files when using Windows Explorer. The control also supports sorting mechanism using the SortItems function. The main problem is that there is no sorting indicator.


When looking at some articles at Code Project, you can find two solutions. The first solution is to provide two additional resources of bitmaps - indicating the arrows for the header control. The second solution is an owner drawn header control which is capable of drawing the appropriate arrow.

Why bother? How does Microsoft draw its arrows? How does Window Explorer draw itself?

Loading Bitmaps

After looking at some files, I found out the solution. There are two bitmaps, located in shell32.dll. The up arrow is 133, while the down arrow is 134.

To load the appropriate bitmap, we use the LoadImage function. Since most of the applications link themselves to shell32.dll, we can use the GetModuleHandle function to retrieve the module handle. If our application doesn't link itself to shell32.dll, we can use LoadLibrary instead.

After loading the bitmaps, we can draw them on the header control just as we draw other bitmaps.


To sort using the list control, we need to define a callback function. To avoid doing it manually, I've created a wrapper for a structure, which my list control can use.

Using the code

Since the report mode is some kind of a structure, we need to create our class to hold the relevant information. The class must inherit from CSortListViewCtrlItem, and specify its class name as the template parameter. We will use a file structure which represents a single file:

class CFileStruct : public CSortListViewCtrlItem<CFileStruct>
    enum Field
        Filename = 0,

    CFileStruct(LPCTSTR szFilename,ULONGLONG nSize,FILETIME ftTime);
    int Compare(CFileStruct* pItem,int nField);
    CString Format(int nField);

    CString m_szFilename;
    ULONGLONG m_nSize;
    FILETIME m_ftTime;

The constructor initializes the structure, while the Format function simply formats a specific field into a string.

The important function is Compare. It compares the current structure with another one, depending on the specified field (you can check the implementation in maindlg.cpp).

Second, we need to create the list control object. The CSortListViewCtrl is a template class which accepts the structure type as its parameter.

CSortListViewCtrl<CFileStruct> m_List;

To subclass the control, we can call SubclassWindow or simply use the operator = which wraps the subclassing method.


To make the control work, we need to create a new structure dynamically (using the new operator), and attach the structure pointer to the list item using the SetItemData function. We don't need to delete the structure, since the control will delete it automatically when handling the LVN_DELETEITEM command.

The control should handle all sorting by itself. If you want to sort it by yourself, you can call the Sort function.

To implement the control, you must follow these steps:

  • Derive your own class from CSortListViewCtrlItem and implement the Compare function.
  • Create the control object with your own structure as its template parameter, and subclass it to the original list window.
  • Reflect notifications from the control's parent using the REFLECT_NOTIFICATIONS macro.

Sample application

The sample application creates a list control, and fills it with files from your Windows folder. You can sort the files by their name, size or time stamp. There is also a tooltip support which displays the entire information regarding a single file.

Final notes

The source code is provided as-is. You can use it freely and modify it as you want. If you release any application which uses it, I will appreciate getting an email from you (just for my ego :) ).


November 18, 2003 - First public release.


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

Gilad Novik
Web Developer
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.

Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.

Check my Homepage for additional resources.

Quote: "There's always one more bug"

Comments and Discussions

QuestionIn what situation we can not locate Shell32.dll in memory? Pin
Hunt Chang1-May-08 6:46
MemberHunt Chang1-May-08 6:46 
Generalerror C3203, VC8(/msvs2005 pro,sp1) Pin
Harly8-Jun-07 4:07
MemberHarly8-Jun-07 4:07 
GeneralRe: error C3203, VC8(/msvs2005 pro,sp1) Pin
Harly8-Jun-07 4:27
MemberHarly8-Jun-07 4:27 
GeneralRe: error C3203, VC8(/msvs2005 pro,sp1) Pin
hpking22-Aug-10 18:02
Memberhpking22-Aug-10 18:02 
QuestionCListViewCtrl Pin
Carlos_Lopez24726-Jan-06 3:40
MemberCarlos_Lopez24726-Jan-06 3:40 
GeneralSubclassWindow =(HWND hwnd) Pin
Skandal31-Jul-04 8:14
MemberSkandal31-Jul-04 8:14 
GeneralProblem Solved Pin
Skandal31-Jul-04 11:14
MemberSkandal31-Jul-04 11:14 
GeneralBetter look with Windows XP Pin
Michal Mecinski19-Nov-03 1:51
MemberMichal Mecinski19-Nov-03 1:51 
GeneralHandy Pin
WREY18-Nov-03 20:09
MemberWREY18-Nov-03 20:09 
GeneralDemo Doesn't Work Pin
Mike O'Neill18-Nov-03 10:15
MemberMike O'Neill18-Nov-03 10:15 
GeneralRe: Demo Doesn't Work Pin
Gilad Novik18-Nov-03 21:21
MemberGilad Novik18-Nov-03 21:21 
GeneralRe: Demo Doesn't Work Pin
Gilad Novik18-Nov-03 21:33
MemberGilad Novik18-Nov-03 21:33 
GeneralRe: Demo Doesn't Work Pin
Greh2-Feb-05 20:48
MemberGreh2-Feb-05 20:48 
GeneralNice Bio! Pin
Jim Crafton18-Nov-03 8:08
MemberJim Crafton18-Nov-03 8:08 

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.