Click here to Skip to main content
11,706,979 members (51,920 online)
Click here to Skip to main content
Add your own
alternative version

Owner Drawn ListViews In WTL

, 26 Jun 2004 56.4K 3.8K 32
An overview of handling owner drawn listview controls in WTL.
#include "stdafx.h"
#include ".\demoownerdrawnlistviewctrl.h"

void CDemoOwnerDrawnListViewCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
	HDC dc = lpDrawItemStruct->hDC;
	CRect item_rect = lpDrawItemStruct->rcItem;
	int item_id = lpDrawItemStruct->itemID;

	bool checkered_toggle = (item_id % 2) != 0;
	int cols = GetHeader().GetItemCount();
	for(int n = 0; n < cols; n++)
		CRect cell_rect;
		GetCellRect(n, item_rect, cell_rect);

		COLORREF square_color;

		if(lpDrawItemStruct->itemState & ODS_SELECTED)
			square_color = GetSysColor(COLOR_HIGHLIGHT);
			::SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
			square_color = 
				checkered_toggle ? RGB(230, 244, 255) : RGB(145, 200, 255);	
			::SetTextColor(dc, GetSysColor(COLOR_BTNTEXT));

		HPEN square_pen = CreatePen(PS_SOLID, 1, square_color);
		HBRUSH squares_brush = CreateSolidBrush(square_color);
		SelectObject(dc, square_pen);
		SelectObject(dc, squares_brush);
		Rectangle(dc, cell_rect.left,, 
					cell_rect.right, cell_rect.bottom);
		std::stringstream ss;
		ss << item_id << _T(":") << n;

		DrawText(dc, ss.str().c_str(), ss.str().size(), cell_rect, 

		checkered_toggle = !checkered_toggle;

void CDemoOwnerDrawnListViewCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
	CClientDC dc(m_hWnd);


	lpMeasureItemStruct->itemHeight = 
		(tm.tmHeight + tm.tmExternalLeading) * 2;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

Mark Petrik
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 27 Jun 2004
Article Copyright 2004 by Mark Petrik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid