CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  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.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Adding icons to listview(LVS_ICON stylee)

    Try:
    Code:
    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2008
    Posts
    3

    Question Re: Adding icons to listview(LVS_ICON stylee)

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

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