You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows桌面应用:无法将MultimediaFileInfo数据显示到ListView

解决Windows ListView显示MultimediaFileInfo结构体数据问题

前提检查

确保你的ListView已正确配置LVS_REPORT样式(多列报表模式),且初始化时添加了对应列(文件名、路径、播放时间):

// 创建ListView时指定样式
hwndListView = CreateWindow(WC_LISTVIEW, _T(""), 
                            WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL,
                            10, 100, 680, 300, hwndParent, (HMENU)IDC_LISTVIEW, hInst, NULL);

// 初始化三列
LVCOLUMN lvc = {0};
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;

// 文件名列
lvc.pszText = _T("文件名");
lvc.cx = 150;
lvc.iSubItem = 0;
ListView_InsertColumn(hwndListView, 0, &lvc);

// 文件路径列
lvc.pszText = _T("文件路径");
lvc.cx = 300;
lvc.iSubItem = 1;
ListView_InsertColumn(hwndListView, 1, &lvc);

// 播放时间列
lvc.pszText = _T("播放时间");
lvc.cx = 200;
lvc.iSubItem = 2;
ListView_InsertColumn(hwndListView, 2, &lvc);

遍历vector插入ListView数据

在LoadMultimediaFile函数完成文件加载后,遍历std::vector<MultimediaFileInfo>,将结构体数据逐行插入ListView:

// 先清空ListView现有内容(避免重复添加)
ListView_DeleteAllItems(hwndListView);

int itemIdx = 0;
for (const auto& media : m_mediaFiles) {
    LVITEM lvi = {0};
    lvi.mask = LVIF_TEXT;
    lvi.iItem = itemIdx;
    lvi.iSubItem = 0;
    // 插入第一列(文件名)
    lvi.pszText = const_cast<LPTSTR>(media.fileName.c_str()); // 适配Unicode/多字节编码
    int insertedIdx = ListView_InsertItem(hwndListView, &lvi);
    
    if (insertedIdx != -1) {
        // 设置第二列(文件路径)
        ListView_SetItemText(hwndListView, insertedIdx, 1, const_cast<LPTSTR>(media.filePath.c_str()));
        
        // 格式化播放时间为字符串并设置第三列
        TCHAR timeBuf[256] = {0};
        // 假设playTime为SYSTEMTIME类型,按需调整格式化逻辑
        wsprintf(timeBuf, _T("%04d-%02d-%02d %02d:%02d:%02d"),
                 media.playTime.wYear, media.playTime.wMonth, media.playTime.wDay,
                 media.playTime.wHour, media.playTime.wMinute, media.playTime.wSecond);
        ListView_SetItemText(hwndListView, insertedIdx, 2, timeBuf);
    }
    itemIdx++;
}

关键注意事项

  • 编码匹配:如果项目是Unicode编码,结构体中的fileName、filePath需为std::wstring;多字节编码则用std::string,避免乱码。
  • 结构体数据有效性:确认LoadMultimediaFile已正确填充MultimediaFileInfo的所有字段,无空值或未初始化情况。
  • ListView句柄正确性:确保操作的是已成功初始化的ListView窗口句柄,避免空句柄调用API。

内容的提问来源于stack exchange,提问作者Ray Foo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 23:52:38