gpt4 book ai didi

c - ListView 控件中的项和子项

转载 作者:行者123 更新时间:2023-12-02 08:31:30 24 4
gpt4 key购买 nike

我想使用 List-View 控件在“网格”中显示 LDAP 搜索的结果。我已经编写了一些测试代码来查看它是如何工作的,但它并没有按我想要的方式显示。据我理解,每个Item相当于一个“行”(使用LVS_REPORT风格),而Subitem相当于一个“列” "(例如,对于每个项目,我可以显示多个子项目,每个子项目位于同一行的单独列中)。

这是我的测试代码,目前设置为创建四列,其中有一个 Item 和四个 Subitems(对应于四列)。两个函数:一个创建列,另一个插入项目。

int CreateColumns(HWND *hwndlistbox)
{
wchar_t *cnames[100];
LVCOLUMN lvc;
int i;

cnames[0] = L"column1";
cnames[1] = L"column2";
cnames[2] = L"column3";
cnames[3] = L"column4";
cnames[4] = NULL;

lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

for (i = 0; cnames[i] != NULL; i++)
{
lvc.iSubItem = i;
lvc.pszText = cnames[i];
lvc.cx = 100;
lvc.fmt = LVCFMT_LEFT;

ListView_InsertColumn(*hwndlistbox, i, &lvc);
}

return i;
}

void InsertItems(HWND *hwndlistbox, int *columncount)
{
LVITEM lvi;
wchar_t *items[100];
int i, j;

items[0] = L"text1";
items[1] = L"text2";
items[2] = L"text3";
items[3] = L"text4";
items[4] = NULL;

lvi.mask = LVIF_TEXT;
lvi.iItem = 0;

for (i = 0; i < *columncount; i++)
{
lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_InsertItem(*hwndlistbox, &lvi);
}
}

我希望这会生成一行 (lvi.iItem = 0;),每列下面都有一个文本字符串 (lvi.iSubItem = i;)。这是它显示的内容:

enter image description here

lvi.iSubItem = i 更改为 lvi.iSubItem = 0 会导致每个文本字符串在第一列中显示为新行:

enter image description here

我玩过它,对 iItemiSubItem 上的数字进行硬编码,将两者都更改为 i,但我不能不要让它在第一列以外的任何地方显示文本。我做错了什么?

最佳答案

首先,您的cnamesitems 数组被声明为指针数组,但您没有为它们分配内存;您需要将它们声明为字符串数组,例如 wchar_t cnames[100][40];

其次,您需要使用 ListView_InsertItem 插入一个项目并设置第一列的值,然后使用 ListView_SetItem添加额外的列,例如

lvi.pszText = items[0];
lvi.iSubItem = 0;
ListView_InsertItem(*hwndlistbox, &lvi);
for (i = 1; i < *columncount; i++)
{ lvi.pszText = items[i];
lvi.iSubItem = i;
ListView_SetItem(*hwndlistbox, &lvi);
}

关于c - ListView 控件中的项和子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26323468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com