fLISTVIEW


Quick Data

Header file:listview.h
Object name:fLISTVIEW
Object index:1 of 1 Object

General Description

The fLISTVIEW is a component with a lot of everything. It is a component which could be quite useful. This is the component that you see as your desktop - displaying icons with text. It's also the component that you see whenever you browse a directory - a set of icons with text, that can be viewed many different ways. You can add icons with captions to one of these listviews (or no icons, if you so wish), and then display these as icons, or icons with extra details in seperate columns, or just list the icons as small icons. There are many different things that you can do with this component.

To allow the use of icons, this class uses the fIMAGELIST class as well. However, if you don't want any icons at all, then you do not need to include this class. The fIMAGELIST class is automatically included when you include this class; if you don't want that to happen, then #define F_LISTVIEW_H_NO_IMAGELISTS before you include this class.

Before we get too much further, there is some terminology that you must be familiar with. The following is a screenshot from Windows Explorer, and I've added labels to show what each part is called. An understanding of this will be very useful as you read this document.

Listview Components

Another thing that will be useful to understand will be the different modes of a Listview, which is described by the image below.

Listview Modes

This component is derived from _GUIBASE, and inherits the properties from that.


Primary Options


Name Description Value

There are no Primary properties for this component. Just use lvNormal (which creates a sunken border). If you do not want a border, then use 0 (Zero) for this value.

Secondary Options


Name Description Value

lvUsual This is the usual style for a Listview. It basically sets up the Listview to show Large Icons with labels (just like the desktop). Also, this style makes sure that the listview does not destroy any imagelists that you assign to it. AvWind | LVS_ICON | LVS_SHAREIMAGELISTS

The following options are a little different. Whereas the above options are a mixture of styles to create one type of object, the following styles are by themselves. You can mix them up to make more customised objects. You mix the styles with the OR (|) operator. You can even mix them with the above styles to enhance them further. Mix and match away!
lvIconAlignLeft This style makes the icons align to the left on the window - which is what the desktop does most of the time. LVS_ALIGNLEFT
lvIconAlignTop This style aligns the icons at the top of the window instead - which is how the icons are aligned if you were looking at a directory listing. LVS_ALIGNTOP
lvIconAlignAuto This style makes the icons automatically arrange themselves. LVS_AUTOARRANGE
lvEditableLabel This style allows the user to edit the labels of items in the control. There are extra events which deal with this, and you should check them out to see how this works. LVS_EDITLABELS
lvNoWrapLabel This style forces the Listview not to wrap the labels of the items if they are too long. LV_NOLABELWRAP
lvNoDestroyImls Ordinarily, once you have assigned an imagelist to a Listview, the Listview will destroy it when it is destroyed. In the case of a fIMAGELIST class, the list contained in it will be destroyed. Specifying this style will prevent that from happening. LVS_SHAREIMAGELISTS
lvShowSelection This style deals with what happens to the selection box when the control loses focus. With this style, whenever the Listview loses focus, the current selection will still be hilighted, albiet with a grey highlight instead of a blue one. However, if this style is not included, and the Listview loses focus, the selection box will be removed, and the items that are selected will look like any other item that is not selected. A good demonstration would be the desktop. Minimize some windows or resize a window so that you can see part of the desktop with an icon. Focus that icon by selecting it. Then focus another window, but don't obscure that icon that you just selected. As you select the other window, the selection box around the icon will vanish completely. Thus, the desktop's Listview does not have this style. LVS_SHOWSELALWAYS
lvSingleSelect This style specifies that the Listview should work in single select mode. By default the Listview allows the user to select multiple items. However, if you don't want this to happen, then apply this style, and the user will only be able to select one item at a time. LVS_SINGLESEL
lvSortAscending This style makes the Listview sort the icons ascendingly. By default the Listview will not sort the entries. LVS_SORTASCENDING
lvSortDescending This style makes the Listview sort the entries descendingly. By default the Listview does not sort the items. LVS_SORTDESCENDING
lvIconView This style makes the Listview display its items in Icon mode - with large icons and captions directly underneath. This style is the same style as is used for the desktop. See the general description at the top of this document for an image demonstrating what each view looks like. LVS_ICON
lvSmallIconView This style makes the Listview display its entries as items with small icons and the caption to the right of that icon. The icons are then aligned towards the top - in that they fill up the area starting from the top left hand corner and move right, then wrapping around to start from the left again. See the general description at the top of this document for an image demonstrating what each view looks like. LVS_SMALLICON
lvListView This style is similar to the lvSmallIconView, except that instead of aligning the icons to the top, they will be aligned to the right. The icons fill the area starting from the top left hand corner, moving downwards, and moving back up to the top each time a new column is needed. See the general description at the top of this document for an image demonstrating what each view looks like. LVS_LIST
lvReportView This style makes the Listview display small icons with text to the right of these icons. As well as that, the Listview displays columns with various data about that item next to that. This style is used when you specify the "Details" view for a directory listing in Explorer- there are multiple columns, each with a header that describes that column. You can click the headers of each column, which sorts that column. See the general description at the top of this document for an image demonstrating what each view looks like. LVS_REPORT
lvNoColHeaders This style makes the Listview not display column headers when in report mode. LVS_NOCOLUMNHEADER
lvNoClickHeaders This style makes the column headers in report mode not able to be clicked. Ordinarily you can click these headers, which usually means to sort that column. However, you may not wish to have sorting capabilities, and thus this style can be quite handy. LVS_NOSORTHEADER

Methods

Extended styles

Some styles for Listviews can not be set with primary or secondary options, and must be done manually via these functions.

To save stating the obvious over and over, if you pass TRUE for Set, it will set the style, and if you pass FALSE for Set, then it will unset the style.

Prototype Description

void MakeFullRowSelect(bool Set); This style is only relevant to the Report mode. If the user selects an item in report mode, and this style is set, then the whole row will get selected - not just the icon and the caption for that item.
void MakeHotTrack(bool Set); This style makes the item's captions light up with an underline and will change the colour of the caption when the mouse comes over that item. Also, the mouse pointer will change to a hand - just like Internet Exploder.
void MakeColdTrack(bool Set); This style forces all of the item's captions to be underlined by default, and when the mouse pointer moves over an item, its underlining will dissapear, and the mouse pointer will change into a hand.
void MakeOneClick(bool Set); This style makes the item's captions just like hyperlinks in Internet Explorer - one click will be equivilent of two clicks, and the item will not only be selected, but 'executed'.
void MakeTwoClick(bool Set); This style makes the Listview work in the usual two click style - one click to select the item, and another click to execute the item.
void MakeCheckBoxes(bool Set); This style makes the Listview display checkboxes next to each item when the list is in Report mode. I suppose there is someway of returning their state?
void MakeFlatScrollbars(bool Set); This style makes the Listview's scrollbars be of the flat scrollbar type, which seems to be pretty popular these days.
void MakeGridLines(bool Set); This style makes the Listview display gridlines between each item and each column in Report mode.
void MakeHeaderDragAndDrop(bool Set); This style allows the user to drag and drop the headers in Report mode, to arrange the columns as they wish. Don't worry about having to manage this; Windows does all the work for you - all you need to do is place the data in the Listview like you normally would.
void MakeSubItemImages(bool Set); When in Report mode, each column's item is called a subitem. It is possible, after you set this style, to add an icon to that subitem as well, which could be rather handy.

And now we continue with the ordinary functions...

However, the functions have been broken up into subcategories, as there are many, many functions that make up this class.

Functions to add and edit items and subitems


Prototype Description

int AddItem(char* Caption, int Icon); This function adds an item to the Listview. You will need to specify a caption for the item, and if you wish, an icon. Icon is an integer number, which specifies the index of the icon that you want in the imagelist. In other words, if you want the 5th icon in the imagelist to be displayed as the icon for this item, then you would specify 4 for Icon (as Icon is zero-based). If you don't wish to have an icon, then you can pass anything that you want for Icon - although I'd recommend just passing 0 (Zero). Also, the value returned is the index of the new item, which you will need if you are going to add subitems. If you have a sorting Listview, and you wish to add subitems, you will need to remember this number and use it to assign subitems, because otherwise your program will assign the subitem to the wrong item! If you want to see how this works, check out the examples at the end of this document.
int AddItem(char* Caption, int Icon, int ID); This function is exactly the same as the above function, except that it takes an extra parameter - ID. ID is any number that you want to associate with the item, for use later. For example, I once used it in combination with an array. In ID I stored the index of that item in the array - which means that I could sort the Listview, but still instantly know which entry in the array a particular item corresponded to. You can use the GetItemID() function listed later on to retrieve this number.
void AddSubItem(int Item, int SubIndex, char* Caption); This function adds a sub item to an already created item. This data will be displayed in a seperate column. Item specifies the index of the item in the listview to add the sub item to. SubIndex specifies which column to add the item to (the columns are assigned numbers (subindexes) by yourself when you create them). Caption is the caption of the item.
void AddSubItem(int Item, int SubIndex, char* Caption, int Icon); This function is exactly like the above item, except that it accepts another parameter - Icon. Icon specifies what icon to use as the icon for that subitem. If you don't want an icon with that subitem, then please use the above function. Icon is the index of the icon in the imagelist that you wish to use.
void ChangeText(int Item, char* Text); This function changes the caption of an item. Item specifies the item to change, and Text specifies what to change the caption to.
void ChangeIcon(int Item, int Index); This function changes the icon of the specified item. Item specifies the item to change, and Index specifies the index of the icon in the imagelist that you wish to change it to.
void ChangeSubItemText(int Item, int SubItem, char* Text); This function changes the caption of a subitem. Item specifies the item to change, SubItem specifies the subitem to change, and Text specifies what the caption should be.
void ChangeSubItemIcon(int Item, int SubItem, int Index); This function changes the icon of the specified subitem of the specified item. Index is the index of the icon in the imagelist that you wish to change the icon to.

Functions to manage columns in the Listview


Prototype Description

void AddColumn(int IndexColumn, char* Caption, int Width, int SubItem); This function adds a new column to the Listview. The column and any information in it will only be displayed if the Listview is in report mode. IndexColumn specifies the initial index of the column, Caption specifies the caption of the column, Width specifies the Width of the column, and SubItem specifies the number that specifies what subitems are associated with that column. For example if you create a column with SubItem as 5, you can then add an item, and then add a subitem, specifing the SubItem as 5. If the user re-arranges the column headers, the subitem index remains the same. If you are going to use report mode, then you will have to add at least one column, and specify SubIndex of 0 for that. In this first column, the actual items that you add will be displayed.
void SetColumnCaption(int ColumnIndex, char* Caption); This function sets the caption of the specified column.
void SetColumnWidth(int ColIndex, int Width); This function sets the width of the specified column.
void SetColumnSubItem(int ColumnIndex, int SubItem); This function sets the subitem number associated with a particular column. ColumnIndex is a Zero based number specifying columns starting from the left and moving towards the right.
void RemoveColumn(int ColumnIndex); This function removes a column from the Listview. ColumnIndex is 0 (Zero) based and starts from the left.
char* GetCaption(int ColumnIndex); This function returns the caption of the specified column. ColumnIndex is 0 (Zero) based and starts from the left.
int GetAssociatedSubItem(int ColumnIndex); This function returns the subitem index associated with the column at ColumnIndex. ColumnIndex is 0 (Zero) based and starts from the left. If you allow the user to rearrange the columns, it might be nice to record that arrangement for next time, and you can do that with this function.
int GetColumnWidth(int ColumnIndex); This function returns the width of the specified column. ColumnIndex is 0 (Zero) based, and starts from the left. The user may well be able to change the width of the columns, and occasionally it is nice to record the widths for next time, which you can do with this function.

Functions to set options for the Listview (and other functions that don't quite fit in elsewhere)


Prototype Description

void SetImageLists(fIMAGELIST* BigIcons, fIMAGELIST* SmallIcons); This function sets which imagelists to gain the icons from to be able to use as icons for items in the Listview. You can pass along the pointers to two fIMAGELISTs for this. BigIcons is only required if you will have a standard icon Imagelist with large icon items. For all other styles, SmallIcons is required. If you don't want an imagelist to be assigned, then simply pass NULL for that particular list.
void Update(int Item); This function forces the Listview to update a particular item. Item is the index of the item that you wish to update. Also, if your Listview is of the auto arranging variety, this function will re-arrange the icons.
void Redraw(int Start, int Stop); This function forces the Listview to redraw all items in the range Start to Stop. If you want to redraw all of the items, just specify 0 (Zero) for Start, and -1 for Stop.
void DeleteItem(int Index); This function deletes the item specified by Index.
void DeleteAll(void); This function deletes all items from the Listview.
void SelectItem(int Item); This function selects the specified item.
void FocusItem(int Item); This function focuses the specified item.
void EditLabelInPlace(int Index); This function starts the in-place label editor. The items caption then turns into an edit control, and allows the user to type in a new name for this item. There are extra associated events that you can use to determine what the result of this operation is.
void EnsureItemVisible(int Index, bool Part); This function forces the Listview to scroll to such a position as to where it will show the item specified by Index. Passing TRUE for Part will allow the Listview to show only part of that item.
void ScrollToPos(int X, int Y) This function is a little different to what you think it is. The best way to describe it would be to use the descriptions in the Win32 SDK Reference Manual (and that's where these excerpts are from):
For X: "If the view type of the list view control is icon view, small icon view, or report view, this value specifies the number of pixels to scroll. If the view type of the list view control is list view, this value specifies the number of columns to scroll."
For Y: "If the view type of the list view control is icon view, small icon view, or list view, this value specifies the number of pixels to scroll. If the view type of the list view control is report view, this value specifies the number of lines to scroll."
int FindItem(int Start, char* Text, bool PartialMatch); Thus function searches for an item, after Start, that matches Text. The only thing that this function does is return the index of the item if it should find a matching item. If a matching item is not found, -1 is returned. Passing TRUE for PartialMatch will allow the search to match the first part of an items caption as well as the whole of the caption.
COLORREF GetBackgroundColour(void); This function returns the background colour of the Listview.
COLORREF GetTextBackgroundColour(void); This function returns the text background colour of the Listview.
COLORREF GetTextColour(void); This function returns the text colour of the Listview.
void SetBackgroundColour(COLORREF Colour); This function sets the background colour of the Listview.
void SetTextBackgroundColour(COLORREF Colour); This function sets the text background colour of the Listview.
void SetTextColour(COLORREF Colour); This function sets the text colour of the Listview.

Functions to get data from items and to get information about the items, and about the Listview's display


Prototype Description

char* GetItemText(int Index); This function returns the caption of the specified item.
char* GetSubItemIndex(int Index, int SubItem); This function returns the caption of the specified subitem. Index sepecifies the item, and SubItem specifies the subitem to obtain the caption of.
int GetItemID(int Index); This fucntion returns the ID number of the item specified by Index. This number is any number that you assigned to the item, which can be used as you wish. Consider it a 'Tag' for each item.
void GetIconIndex(int Index); This function returns the index of the icon in the imagelist for the item specified by Index.
int GetSubItemIconIndex(int Index, int SubItem); This function returns the index of the icon in the imagelist specified by the item at Index and the subitem at SubItem.
bool IsSelected(int Item); This function determines whether or not the specified item is selected. If the item is selected, TRUE is returned, and if the item is not selected, FALSE is returned. If you want to determine which item(s) are selected, just loop through all of the items in the Listview, and call this function for each.
bool IsFocused(int Item); This function determines whether or not an item is focused. If an item is focused, it will have a dotted-line box around its caption. The item may also be selected if it is focused. To determine which item(s) are focused, loop through all of the item in the Listview, and call this function for each item.
int GetNumberSelected(void); This function returns the number of items that are selected in the Listview.
int GetSelected(void); If your Listview is of the single-select variety, then there must be an easier way of determining one selected item. Well, there is. That is what this function does for you - cycle through all the items and determines which one is selected. Once this item is found, its index is returned. If your Listview is actually of the multiselect variety, then only the first selected item is returned by this function. If there are no items selected, -1 is returned.
int GetNumberItemsPerPage(void); This function returns the number of items per page - that is, the number of items that will fit in one screenful of information.
int GetNumberItems(void); This function returns the number of items in the Listview.
int GetTopMostVisibleItem(void); This function returns the index of the item that is the topmost visible item - the item that is currently at the top of the screen.
int GetItemPosX(int Item); This function returns the X co-ordinate of the position of the specified item.
int GetItemPosY(int Item); This function returns the Y co-ordinate of the position of the specified item.
void SetItemPosition(int Item, int X, int Y); This function sets the location of the specified item.
RECT* GetItemArea(int Item); This function returns a pointer to a RECT structure. The details of this RECT structure are filled out with the details pertaining to the outside boundaries of the item that you specified.
RECT* GetAllItemsArea(void); This function returns a pointer to a RECT structure, which will be filled out with the bounding rectangle of all the items in the Listview. This function only applies to those Listviews in the Icon or Small Icon modes.
int GetItemSpacing(bool SmallView); This function returns the spacing between icons. This function can only return a value if the Listview is in Icon or Small Icon mode. If you wish to return the spacing for the Small Icon view, pass TRUE for SmallView, or if you wish to get the spacing for Icon view, then pass FALSE for SmallView.

Events

Please note that the events listed here are done in pairs; one as a property, and the other as a function. The property is assigned a function name, ie. Object.OnClick = &FunctionName, whilst the function is called to verify that an event has occured. Please see the chapter on event handling for information on how to use these correctly.

One more note: all of the events for the Listview are sent under the WM_NOTIFY message - so if you are going to manually intercept these messages, don't forget! Attempting to intecept these events from the WM_COMMAND message will result in an access violation.


Prototype Description Sent under

bool CheckClick(wParam, lParam);
void (*OnClick)(fLISTVIEW* Sender);
This event occurs when the user clicks the Listview; be that on an item or an empty space. WM_NOTIFY!
bool CheckDoubleClick(wParam, lParam);
void (*OnDoubleClick)(fLISTVIEW* Sender);
This event occurs when the user double clicks in the Listview; they may have double clicked an item or an empty area of the Listview. WM_NOTIFY!
bool CheckRightClick(wParam, lParam);
void (*OnRightClick)(fLISTVIEW* Sender);
This event occurs when the user right clicks in the Listview. This may be a right click on an item, or it might be a right click in an empty area. WM_NOTIFY!
bool CheckRightDoubleClick(wParam, lParam);
void (*OnDoubleRightClick)(fLISTVIEW* Sender);
This event occurs when the user double-right-clicks in the Listview. This might have been on an item, or it might have been in an empty area. WM_NOTIFY!
bool CheckReturn(wParam, lParam);
void (*OnReturn)(fLISTVIEW* Sender);
This event occurs when the Listview has focus and the user presses the Enter or Return key. There most likely was an item selected when the enter key was pressed, but please check first. WM_NOTIFY!
bool CheckBeginDrag(wParam, lParam);
void (*OnDrag)(fLISTVIEW* Sender, int Item, POINT* Point);
This event occurs when the user begins dragging an icon. Item is the item being dragged, and Point is the point at which this occured. WM_NOTIFY!
bool CheckBeginRightDrag(wParam, lParam);
void (*OnRightDrag)(fLISTVIEW* Sender, int Item, POINT* Point);
This event occurs when the user begins to drag an item with the right mouse button. Item is the item being dragged, and Point is the point at which this occured. WM_NOTIFY!
bool CheckBeginLabelEdit(wParam, lParam);
bool (*OnLabelEdit)(fLISTVIEW* Sender, int Item);
This event occurs when the user begins to edit a label of an item. The item that is about to be modified is specified by Item. If you are using the GUI controller version, you can return TRUE to allow the label edit, or FALSE to prevent it. WM_NOTIFY!
bool CheckEndLabelEdit(wParam, lParam);
void (*OnLabelEdited)(fLISTVIEW* Sender, int Item, char* Text);
This event occurs when the user specifies that they are finished editing the label which they began to edit. Item specifies the item that was changed, and Text specifies the text that the item was changed to. WM_NOTIFY!
bool CheckColumnClick(wParam, lParam);
void (*OnColumnClick)(fLISTVIEW* Sender, int Column);
This event occurs when the user clicked a column label. Usually this is used to mean that the user wants that column sorted. Column specifies which column was clicked. Please note that Column is not the index of the column, it is the SubItem number that you gave it when you created the column. WM_NOTIFY!
bool CheckDeleteAllItems(wParam, lParam);
void (*OnDeleteAllItems)(fLISTVIEW* Sender);
This event occurs when all the items in the Listview are deleted. I would have thought that you knew when you deleted all the items, but then again, maybe not... WM_NOTIFY!
bool CheckDeleteItem(wParam, lParam);
void (*OnDeleteItem)(fLISTVIEW* Sender, int Item);
This event occurs when an item is deleted. The item that was deleted is specified by Item. WM_NOTIFY!
bool CheckInsertItem(wParam, lParam);
void (*OnInsertItem)(fLISTVIEW* Sender, int Item);
This event occurs when an item has been inserted. The item that has been inserted is specified by Item. WM_NOTIFY!
bool CheckItemChanging(wParam, lParam);
bool (*OnItemChanging)(fLISTVIEW* Sender, int Item);
This event occurs when the user does something to change an item. The item that is being changed is specified by Item. If you are using the GUI controller thingy, then you can pass TRUE to allow the change to occur, or FALSE to prevent it. WM_NOTIFY!
bool CheckItemChanged(wParam, lParam);
void (*OnItemChanged)(fLISTVIEW* Sender, int Item);
This event occurs when an item has changed. Item specifies the item that changed. WM_NOTIFY!

Sample Code

The following snippet gives a basic idea of how to use this component.


//Create an instance of the Listview...
fLISTVIEW ListView;

//Create some associated fIMAGELISTS...
fIMAGELIST ImageListBig;
fIMAGELIST ImageListSmall;

//In this example, we are going to load a DLL, and display
//all the icon in it in this Listview. Also, we shall create some
//columns to display extra information, should you wish to
//use report mode. If you have even the remote possibility of using
//report mode, then just add the columns anyway.

//Firstly, we create the window...
ListView.CreateWin(Window.GetHandle(), "Any caption here", 123456, lvNormal, AvWind | lvIconView | lvIconAlignAuto);

//Set some options for the ListView...
ListView.MakeFullRowSelect(TRUE);
ListView.MakeHeaderDragAndDrop(TRUE);

//Add some columns to the ListView...
ListView.AddColumn(0, "Icon", 200, 0);
ListView.AddColumn(1, "Icon Index", 100, 1);
//For Add column: AddColumn(Column Index, "Caption", Width, Sub Item ID)

//Now we set up the ImageLists and add all the icons from SHELL32.DLL
ImageListBig.Create(32,32, bdDeviceDependant, 1,1);
ImageListSmall.Create(16,16, bdDeviceDependant, 1,1);

//Find the number of icons...
int TempNo = ImageListBig.GetNumberIcons("SHELL32.DLL");

//We are just assuming that there are icons in the file. If the above
//function returns 0 (Zero), then there are no icons, and we should
//not try to get icons from this file.

//Now add the icons to the ImageLists...
int Temper;
for (Temper = 0; Temper < TempNo; Temper++)
    {
    ImageListBig.LoadIconFromDLL("SHELL32.DLL", Temper);
    ImageListSmall.LoadIconFromDLL("SHELL32.DLL", Temper);
    }

//Apply the imagelists...
ListView.SetImageLists(&ImageListBig, &ImageListSmall);

//Add the actual items and also the extra column information...
bstring Numbering;   //This is used to convert numbers to a string.
Temper = (TempNo-1); //Start from the end
int NewItem;         //Used to make sure we add the subitem to the correct item.
for ( ; Temper > -1; Temper--) //Start at the end, move forwards.
    {
    Numbering.tostr("Icon number %d", Temper); //Convert to a string.
    NewItem = ListView.AddItem(Numbering.data, Temper); //Add the item.
    //The above line also remembers the new items index, which you must
    //do if you have a sorting ListView.
    Numbering.tostr("%d", Temper); //Convert to a string
    ListView.AddSubItem(NewItem, 1, Numbering.data); //Add SubItem.
    //For adding a subitem: AddSubItem(Item to add to, SubItemID, Caption)
    }

//Now here's an unrelated example: how to determine what items
//are selected, if multiple selections are allowed. Here's how.
//Basically you just cycle through each item, and test each one.
//Set up a for loop...
for (int Cycle = 0; Cycle < ListView.GetNumberitems(); Cycle++)
    {
    //Now test the item...
    if (ListView.IsSelected(Cycle))
       {
       //Ok, this item is selected.
       } else {
       //Nope, this item is not selected.
       }
    }

Back to indexThe FreeFoote Foundation Classes Documentation