CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    May 2008
    Posts
    3

    Question Adding icons to listview(LVS_ICON stylee)

    hi...

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

    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?
    Last edited by MetaG; May 26th, 2008 at 03:46 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured