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?
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?