Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / MFC

Graphic and Text Ownerdraw Listbox

Rate me:
Please Sign up or sign in to vote.
4.14/5 (24 votes)
1 Sep 2008CPOL3 min read 94.8K   4.5K   48   9
Already made MFC ownerdraw listbox that holds text and icons
Sample Image - MFC_OwnerDraw_Listbox.jpg

Introduction

Hi, everyone. This is my first article on CodeProject and I hope you find it useful. My article is about an ownerdraw MFC listbox that I developed that accepts text and icons in it. The last thing is that I would like you to rate the article so I can know your opinion. Thanks a lot.

Listbox Features

As we all know, the regular listbox that you drag and drop from the control toolbar in the Visual Studio 6 just accepts text. But I made three extra features in my listbox which are:

  • Accept graphic icons (.ico) files beside the text.
  • You can determine the text and the icon positions in the listbox (left or right).
  • You can drag and drop any (.ico) icon files in the listbox and they will be automatically added.

Things To Do Before You Begin

There are some procedures in the design that should be taken into consideration before using the COwnerDrawListBox class. First open the resource tab in Visual Studio and select the dialog that you will put your listbox in and drag a listbox from the control bar and adjust it on the dialog like this:

Project Settings

Then open the properties dialog of the listbox and select the styles tab and change the properties as changed in the picture like this:

Listbox Styles Settings

You have to change the owner draw style to variable, because our listbox contains text and icons to draw, then uncheck the sort and check the vertical scroll as shown above. After that, select the next tab (Extended styles) and check accept files as shown:

Listbox Extended Settings

One last thing, after you had done all that you have to make a variable and attach it to the listbox control so you can use and add items in it, but be sure that you made the variable from our class COwnerDrawListBox, not CListBox.

Using the Code

First of all, you must add the OwnerDrawListBox .cpp and .h files to the project. Let us suppose that you are working on a Dialog based MFC project as the demo project, if the project name is OwnerDraw, then in the COwnerDrawDlg class, you should include the OwnerDrawListBox files.

C++
#include "OwnerDrawListBox.h"

Then suppose the place that we are going to add the items in the list box is in the OnInitDialog() function and our object from the COwnerDrawListBox class is called m_OwnerListBox, so, just before the line:

C++
return TRUE;

call the member function Add() like this:

C++
m_OwnerListBox.Add("Cafe", AfxGetApp()->LoadIcon(IDI_CAFE));

Where the macro IDI_CAFE is the icon added and named to the project resources. Then there are two other helping functions that I made to help you set the icon and the text position SetIconPosition and SetTextPosition respectively. If you wanted to set the position of one, then you have to set the position of the other else the one which its position was set is the one that will be drawn and the other won't be drawn. You can use the two functions like this:

C++
m_OwnerListBox.SetTextPosition(COwnerDrawListBox::ITEM_LEFT);
m_OwnerListBox.SetIconPosition(COwnerDrawListBox::ITEM_RIGHT);

where the parameter COwnerDrawListBox::ITEM_RIGHT or COwnerDrawListBox::ITEM_LEFT that is passed to the function SetIconPosition or SetTextPosition is the value where you want to set the text or the icon in the listbox. If you didn't set either positions of the icon and the text, they will be set to the default position where the text will be on the right and the icon will be on the left.

After you have finished, you must call function Destroy() to delete all the pointers used in the COwnerDrawListBox class and not to allow any memory leaks to happen like this:

C++
m_OwnerDrawListBox.Destroy();

About Me

My Name is Mina Nabil, I'm working as a Software Engineer in Pyramids Systems Development (PSD) and this my first article on CodeProect as I said before, so I would like to hear your comments, reviews and if anyone wants to ask anything, just mail me at mina.nsami@gmail.com or leave me a message, I promise that I'll reply. Thanks a lot.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDefect Pin
hbbrandnew6-May-11 21:16
professionalhbbrandnew6-May-11 21:16 
Generalproblem Pin
srikanthmulka26-Apr-11 2:43
srikanthmulka26-Apr-11 2:43 
QuestionCDC detach problem in DrawItem method Pin
maqmakmac14-Nov-08 18:58
maqmakmac14-Nov-08 18:58 
Generalgreat job Pin
Jermeen24-Jul-07 23:12
Jermeen24-Jul-07 23:12 
GeneralI like this one ! Pin
2pers26-Jan-07 9:05
2pers26-Jan-07 9:05 
GeneralRe: I like this one ! Pin
2pers26-Jan-07 9:25
2pers26-Jan-07 9:25 
Hey, I answer to my problem Smile | :) (it was not very hard in fact Wink | ;-) )
For those who want to support the native size, just use DrawIconEx method

DrawIconEx(lpDIS->hDC, rectIcon.left+2,rectIcon.top+2,pItem->m_hIcon,
m_nativesize.cx, m_nativesize.cx, 0, NULL, DI_NORMAL );

Regards,

2pers

GeneralProblem with GetText Pin
rockaden24-Oct-06 7:37
rockaden24-Oct-06 7:37 
GeneralRe: Problem with GetText Pin
>>MonMon<<27-Oct-06 1:06
>>MonMon<<27-Oct-06 1:06 
GeneralRe: Problem with GetText Pin
ev_Genius12-Aug-07 23:03
ev_Genius12-Aug-07 23:03 

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.