Click here to Skip to main content
13,147,919 members (37,391 online)
Click here to Skip to main content


33 bookmarked
Posted 26 Jun 2004

Owner Drawn ListViews In WTL

, 26 Jun 2004
An overview of handling owner drawn listview controls in WTL.
// (C) 2004 - Mark Petrik Sosa

#pragma once
#include "stdafx.h"

template <class TBase>
class COwnerDrawnListViewCtrl : public CWindowImpl<TBase, CListViewCtrl>,
								public COwnerDraw<TBase>
		CHAIN_MSG_MAP_ALT(COwnerDraw<TBase>, 1)

	// If this control was .SubclassWindowed instead of .Create(Xe)d, then it 
	// will not receive a WM_MEASUREITEM since this message occurs before 
	// WM_INITDIALOG. By moving the window we can get windows to generate
	// another one for us.
	// ---------------------------------------------------------------------
	void ForceMeasureItemMessage()
		// I just want to nudge it up a little.
		CRect window_rect;
		CPoint pt = window_rect.TopLeft();
		::ScreenToClient(GetParent(), &pt);
		window_rect.right = pt.x + window_rect.Width();
		window_rect.left = pt.x;
		window_rect.bottom = pt.y + window_rect.Height(); = pt.y + 1;

		// Alright now move it back. = pt.y;

	// We are required to have this empty DeleteItem to avoid ambiguity issues
	// with COwnderDraw::DeleteItem and CListViewCtrl::DeleteItem.
	// ---------------------------------------------------------------------
	void DeleteItem(LPDELETEITEMSTRUCT /*lpDeleteItemStruct*/) {};

	void GetCellRect(int header_column, const CRect& item_rect, CRect& cell_rect)
		CHeaderCtrl header = GetHeader();
		CRect header_rect;
		header.GetItemRect(header_column, &header_rect);

		// If we don't do this, when we scroll to the right, we will be 
		// drawing as if we weren't and your cells won't line up with the
		// columns.
		int x_offset = -GetScrollPos(SB_HORZ);

		cell_rect.left = x_offset + header_rect.left;
		cell_rect.right = x_offset + header_rect.right; =;
		cell_rect.bottom = item_rect.bottom;

	// These are the functions you can overload in your class for Owner Drawing.
	// You will need to at least handle DrawItem().
	// If your going to use MeasureItem, make sure you call 
	// ForceMeasureItemMessage if you didn't create it yourself
	// ---------------------------------------------------------------------
	//void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
	//void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
	//int CompareItem(LPCOMPAREITEMSTRUCT /*lpCompareItemStruct*/)
	//void DeleteItem(LPDELETEITEMSTRUCT /*lpDeleteItemStruct*/)

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

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