ListView的图标不显示,大哥们帮我看看为什么
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include "tchar.h"
#include "commctrl.h"
#include "resource.h"
#pragma comment(lib,"comctl32.lib")
#define WINDOW_CLASS_NAME "WINCLASS"
#define IDC_LISTVIEW 1
HWND hlistview;
HMENU hmenu;
HINSTANCE instance;
HICON hIcon;
TCHAR listindex[] = _T("%d");
TCHAR listcont1[] =_T("房间%d");
TCHAR listcont2[] =_T("电器%d");
TCHAR listcont3[] =_T("汽车%d");
TCHAR listcont4[] =_T("飞机%d");
void InsertColumn()
{
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
lvc.fmt=LVCFMT_CENTER;
lvc.pszText ="序号";
lvc.cx = 100;
SendMessage(hlistview,LVM_INSERTCOLUMN,0,(LPARAM)&lvc);
lvc.pszText = "实例1";
lvc.cx = 150;
SendMessage(hlistview,LVM_INSERTCOLUMN,1,(LPARAM)&lvc);
lvc.pszText = "实例2";
SendMessage(hlistview,LVM_INSERTCOLUMN,2,(LPARAM)&lvc);
lvc.pszText = "实例3";
SendMessage(hlistview,LVM_INSERTCOLUMN,3,(LPARAM)&lvc);
lvc.pszText = "实例4";
SendMessage(hlistview,LVM_INSERTCOLUMN,4,(LPARAM)&lvc);
}
void InitListView()
{
int i=0;
int j=0;
CHAR buffer[20];
int iIconPos;
int iIconWidth = GetSystemMetrics(SM_CXICON);
int iIconHeight = GetSystemMetrics(SM_CYICON);
HIMAGELISTg_himl = ImageList_Create(iIconWidth, iIconHeight, ILC_MASK, 1, 0);
if(g_himl==NULL)
MessageBox(0,0,0,0);
iIconPos = ImageList_AddIcon(g_himl, hIcon);
if(iIconPos==-1)
MessageBox(0,0,0,0);
ListView_SetImageList(hlistview , g_himl, LVSIL_NORMAL);//将图像列表与listctrl绑定
for(i=5;i>0;i--)
{
LV_ITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.iItem = 0;
lvi.iSubItem = 0;
wsprintf(buffer,listindex,(i));
lvi.pszText =buffer;
lvi.iImage = iIconPos;
//SendMessage(hlistview,LVM_INSERTITEM,0,(LPARAM)&lvi);
ListView_InsertItem(hlistview, &lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont1,(i));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont2,(i));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont3,(i));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont4,(i));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
}
}
void AddItemOfListView(int row)
{
CHAR buffer[20];
LV_ITEM lvi;
lvi.mask = LVIF_TEXT;
lvi.iItem = row;
lvi.iSubItem = 0;
wsprintf(buffer,listindex,(row+1));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_INSERTITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont1,(row+1));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont2,(row+1));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont3,(row+1));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
lvi.iSubItem++;
wsprintf(buffer,listcont4,(row+1));
lvi.pszText =buffer;
SendMessage(hlistview,LVM_SETITEM,0,(LPARAM)&lvi);
}
LRESULT CALLBACK WindowProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hprevinstance,
PSTR szcmdLine, int icmdshow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX winclass;
InitCommonControls();
instance=hinstance;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL,IDC_ARROW);
winclass.hbrBackground= GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName= WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
hIcon=LoadIcon(hinstance,MAKEINTRESOURCE(IDI_ICON1));
if(hIcon==NULL)
MessageBox(0,0,0,0);
if(!RegisterClassEx(&winclass))
return (0);
if(!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"WinFormPrj",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
240,262,
800,500,
NULL,
NULL,
hinstance,
NULL)))
return (0);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
{
hlistview = CreateWindowEx(LVS_EX_FULLROWSELECT,_T("SysListView32"),NULL,LVS_REPORT |
WS_CHILD |WS_VISIBLE|LVS_SORTASCENDING|LVS_ICON,
0,0,200,200,hwnd,(HMENU)IDC_LISTVIEW,instance,NULL);
InsertColumn();
InitListView();
//SendMessage(hlistview,LVM_SETTEXTCOLOR,0,(LPARAM)RGB(0,0,0));
//SendMessage(hlistview,LVM_SETBKCOLOR,0,(LPARAM)RGB(0,0,0));
//SendMessage(hlistview,LVM_SETTEXTBKCOLOR,0,(LPARAM)RGB(0,0,0));
SetFocus(hlistview);
//ListView_SetItemState(hlistview,0,LVIS_SELECTED,LVIS_SELECTED);
//参数0使第一行处于选择中状态,若为-1则表示所有的行
ListView_SetExtendedListViewStyle(hlistview,LVS_EX_FULLROWSELECT); //设置整行选择风格
return (0);
} break;
case WM_SIZE:
{
MoveWindow(hlistview,0,0,LOWORD(lparam),HIWORD(lparam),TRUE);
//调用MoveWindow来改变列表视图控件的大小使得它覆盖整个的客户区
} break;
case WM_NOTIFY:
{
switch(LOWORD(wparam))
{
case IDC_LISTVIEW:
{if(((LPNMHDR)lparam)->code==NM_RCLICK)
{
//MessageBeep (-1);
int iselect=SendMessage(hlistview,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
if(iselect!=-1)
{
POINT point;
hmenu=LoadMenu(instance,MAKEINTRESOURCE(POPMENU));
hmenu=GetSubMenu(hmenu,0);
GetCursorPos (&point);
TrackPopupMenu (hmenu, TPM_LEFTALIGN, point.x, point.y, 0, hwnd, NULL);
}
}
return (0);
} break;
}
}break;
case WM_COMMAND:
{
if(LOWORD(wparam)==ID_MENU_ADD)
{
int itemc=SendMessage(hlistview,LVM_GETITEMCOUNT,0,0);
AddItemOfListView(itemc);
}
if(LOWORD(wparam)==ID_MENU_DEL)
{
int iselect=SendMessage(hlistview,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
if(iselect!=-1)
{
SendMessage(hlistview,LVM_DELETEITEM,iselect,0);
}
}
if(LOWORD(wparam)==ID_MENU_EXIT)
{
MessageBeep (-1) ;
PostQuitMessage(0);
}
return (0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return (0);
} break;
default:break;
}
return (DefWindowProc(hwnd,msg,wparam,lparam));
}
[解决办法]
HIMAGELISTg_himl = ImageList_Create(iIconWidth, iIconHeight, ILC_MASK, 1, 0);
if(g_himl==NULL)
MessageBox(0,0,0,0);
iIconPos = ImageList_AddIcon(g_himl, hIcon);
if(iIconPos==-1)
MessageBox(0,0,0,0);
ListView_SetImageList(hlistview , g_himl, LVSIL_NORMAL);//将图像列表与listctrl绑定
图标的创建绑定都成功的