在ListView(WIN 32)中插入图标 [英] Insert icons in ListView(WIN 32)

查看:93
本文介绍了在ListView(WIN 32)中插入图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我在列表视图中插入图标​​时遇到问题.
当我按下按钮时,我想在列表视图中插入一些图标.
我已经注意到,当我运行代码时,捕获了某个地方(必须有一个可能的图标),但没有显示图标.
请帮我解决这个问题!

这是我的简单代码:

Hi guys
I have a problem with inserting icons in my listview.
When I push the button I''d like to insert some icons in the listview.
I have noticed when I run my code some place is captured(where a possible icon must be)?BUT icons aren''t displayed.
Please help me with that !!!

Here is my simple code:

//-----------------------------------
//Resource file:
#define IDI_ICON1       111
#define IDI_ICON2       112
#define IDI_ICON3       113
#define IDI_ICON4       114
#define IDI_ICON5       115
#define IDI_ICON6       116

//There is my listview:

hFileListView=CreateWindow(WC_LISTVIEW,
    NULL,
    WS_CHILD | WS_VISIBLE | LVS_REPORT|WS_HSCROLL|WS_BORDER|WS_VSCROLL |
    ES_AUTOHSCROLL | ES_AUTOVSCROLL,
    150,
    70,
    630,
    220,
    hWnd,
    (HMENU) 500,
    hInst,
    NULL);
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH  | LVCF_FMT;
lvc.fmt  = LVCFMT_LEFT;
//Name
lvc.iSubItem = 0;
lvc.cx       = 280;
lvc.pszText  = TEXT("Name");
ListView_InsertColumn(hFileListView, 0, &lvc);
//Data Modified
lvc.iSubItem = 1;
lvc.cx       = 150;
lvc.pszText  = TEXT("Data Modified");
ListView_InsertColumn(hFileListView, 1, &lvc);
//Type
lvc.iSubItem = 2;
lvc.cx       = 100;
lvc.pszText  = TEXT("Type");
ListView_InsertColumn(hFileListView, 2, &lvc);
//Size
lvc.iSubItem = 3;
lvc.cx       = 100;
lvc.pszText  = TEXT("Size");
ListView_InsertColumn(hFileListView, 3, &lvc);

//----------------------------------------------------------------
typedef struct tagAPPLINFO
{
    char szAppName[40];
    char szIconName[20];
}APPLINFO;

APPLINFO rgApplInfo[]=
{
    {"Folder",            "book1.ico"},
    {"Exe",               "exec.ico"},
    {"Music",             "music.ico"},
    {"Text",		"text.ico"},
    {"Undefined",		"unkn.ico"},
    {"Picture",		"picture.ico"},
    {"Dll",		"Dll.ico"}  
};
VOID InsertInFileList(HWND hWnd,HWND hFileListView,LVITEM lv,HIMAGELIST himl,HINSTANCE hInst){
    HICON hIcon;
    himl=ImageList_Create(
    GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
    for(int i = IDI_ICON1; i <= IDI_ICON6; i++)
    {
        hIcon = LoadIcon(hInst,MAKEINTRESOURCE(i));
        ImageList_AddIcon(himl, hIcon);
    }
    ListView_SetImageList(hFileListView, himl, LVSIL_SMALL);
    for(int i=0; i<6; i++)
    {
        lv.iItem=0;
        lv.iSubItem = 0;
        lv.cchTextMax = 40;
        lv.lParam = (LPARAM)&rgApplInfo[1];
        lv.iImage = i;
        ListView_InsertItem(hFileListView, &lv);
        ListView_SetItemText(hFileListView, 0, 0, TEXT("1"));
    }
}

推荐答案

LVITEM mask成员在传递给ListView_InsertItem时未设置:
The LVITEM mask member is not set when passing it to ListView_InsertItem:
lv.iItem=0; // this will insert at top!
lv.iSubItem = 0;
lv.cchTextMax = 40;
lv.lParam = (LPARAM)&rgApplInfo[1];
lv.iImage = i;
// This is missing!
lv.mask = LVIF_PARAM | LVIF_IMAGE;
// To pass also text, use
lv.pszText = TEXT("1");
lv.mask = LVIF_PARAM | LVIF_IMAGE | LVIF_TEXT;


我不确定会带来多大的不同,但建议使用
I am not sure how much difference it will make but it is recommended to use LoadImage()[^] rather than LoadIcon(). You should also check all return values from system calls to see if any have not succeeded.


您可能希望将图像列表的大小扩展为6或10,以
开头
You might want to expand the size of your imagelist to 6 or 10 to start with
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR32, 6, 1);


和DestroyIcon


And DestroyIcon

for(int i = IDI_ICON1; i <= IDI_ICON6; i++)
    {
        hIcon = LoadIcon(hInst,MAKEINTRESOURCE(i));
        ImageList_AddIcon(himl, hIcon);
        DestroyIcon( hIcon );
    }


您的图标支持多种尺寸吗?同一图标文件中的16x16、32x32、256x256?


Does you icons support multiple sizes? 16x16, 32x32, 256x256, in the same icon file?

ListView_SetView( hFileListView, LV_VIEW_DETAILS );


确保设置项目


Make sure you set item

ListView_SetItem( hFileListView, &lv);
ListView_SetItem( hFileListView, (const LV_ITEM *)&lv);


这篇关于在ListView(WIN 32)中插入图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆