Click to See Complete Forum and Search --> : Adding icons to listview(LVS_ICON stylee)


MetaG
May 26th, 2008, 03:41 PM
hi...

i am trying to add icons to a listview control(i have been looking at http://msdn.microsoft.com/en-us/library/bb774737(VS.85).aspx) but not having much luck.
heres my source code


#include <windows.h>
#include <commctrl.h>
#pragma comment(lib,"comctl32.lib")

#include "res.h"

HWND hListView = NULL;
LVITEM lvItem;

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)
{
// does usual defwindowproc etc
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
{
MSG msg;
HWND hwnd;
RECT Desktop;
WNDCLASS wc;
HIMAGELIST hImageList = 0;
INITCOMMONCONTROLSEX iccx;

ZeroMemory(&wc,sizeof(WNDCLASS));

wc.hbrBackground = CreateSolidBrush(RGB(0,0,0));
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT("LVTest");
wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));

if(!RegisterClass(&wc))
{
MessageBox(NULL,TEXT("Error: Unable to register window class."),TEXT("Error!"),MB_OK|MB_ICONWARNING);
return -1;
}

GetClientRect(GetDesktopWindow(),&Desktop);

hwnd = CreateWindow(TEXT("LVTest"),TEXT("ListView Test"),WS_SYSMENU|WS_CAPTION,Desktop.right / 2 - 640 / 2,Desktop.bottom / 2 - 480 / 2,640,480,NULL,NULL,hInstance,NULL);

if(hwnd == NULL)
{
MessageBox(NULL,TEXT("Error: Unable to create basic window."),TEXT("Error!"),MB_OK|MB_ICONWARNING);
return -2;
}

ZeroMemory(&iccx,sizeof(INITCOMMONCONTROLSEX));

iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC = ICC_LISTVIEW_CLASSES;

if(!InitCommonControlsEx(&iccx))
{
MessageBox(NULL,TEXT("Error: Unable to initialize common controls."),TEXT("Error!"),MB_ICONWARNING|MB_OK);
return -3;
}

hListView = CreateWindow(WC_LISTVIEW,TEXT(""),WS_CHILD|LVS_ICON|WS_VISIBLE,180,100,680,480,hwnd,NULL,hInstance,NULL);

if(hListView == NULL)
{
MessageBox(NULL,TEXT("Error: Unable to create listview control."),TEXT("Error!"),MB_ICONWARNING|MB_OK);
return -4;
}

hImageList = ImageList_Create(32,32,ILC_MASK,1,1);

if(hImageList == NULL)
{
MessageBox(NULL,TEXT("Error: Unable to create image list."),TEXT("Error!"),MB_OK);
return -5;
}

ImageList_AddIcon(hImageList,LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1)));

ZeroMemory(&lvItem,sizeof(LVITEM));

lvItem.mask = LVIF_TEXT|LVIF_IMAGE;
lvItem.pszText = TEXT("IconTest1");
lvItem.iImage = 0;

if(ListView_SetImageList(hListView,hImageList,LVSIL_NORMAL) == NULL)
{
SendMessage(hListView,LVM_SETIMAGELIST,(WPARAM)(int)LVSIL_NORMAL,(LPARAM)(HIMAGELIST)hImageList);
}

SendMessage(hListView,LVM_INSERTITEM,0,(LPARAM)(LPLVITEM)&lvItem);

lvItem.pszText = TEXT("IconTest2");
SendMessage(hListView,LVM_INSERTITEM,0,(LPARAM)(LPLVITEM)&lvItem);

ShowWindow(hListView,SW_SHOW);
UpdateWindow(hListView);

ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}


i can see TestIcon1 and 2 but i cannot set the icon. can someone point me in the right direction please.

thanks in advance!

P.S. it seems ImageList_AddIcon returns -1 so the icon isnt being set. is there a SendMessage or similar function to set the icon?

Marc G
May 27th, 2008, 07:21 AM
Try:
hImageList = ImageList_Create(32,32,ILC_COLOR32|ILC_MASK,1,1);
or any of the other ILC_COLOR* flags in combination with ILC_MASK.
Also check that LoadIcon is working properly.

MetaG
May 27th, 2008, 12:36 PM
thanks for trying but loadicon(...) is working fine and i have set the color and mast bit flags but sadly no luck.
i have found out that its down to imagelist_addicon returning -1 and therefore not adding the icon to the image list and thats why im not getting icon displayed.

on another note can someone point me in the direction in using the debugger if there is one for vc++ 2k5 express edition because i doesnt seem to wanna stop at break points and display information about variables etc like vc++6 did(ive only just changed enviroments).