问题描述
我想在网格,以使用列表视图
控件来显示LDAP搜索结果。我已经写了一些测试code,看看它是如何工作的,但它没有被显示为我想要的。据我了解,每个项目
相当于一个行(使用 LVS_REPORT
风格)和子项目
相当于一个列(例如每个项目我可以显示多个子项,在每一个在同一行上一个单独的列)。
I'm want to use a List-View
control to display results of an LDAP search in a "grid". I've written some test code to see how it works, but it's not being displayed as I want. As I understand it, each Item
is equivalent to a "row" (using LVS_REPORT
style), and the Subitem
is equivalent to a "column" (e.g. for each item I can display a number of subitems, each in a separate column on the same row).
下面是我的测试code,当前设置为创建四列,用一个单一的项目和四个子项(对应于四列)。两个功能:一个创建列,其他插入项目
Here's my test code, currently set to create four columns, with a single Item and four Subitems (corresponding to the four columns). Two functions: one to create the columns, the other to insert items.
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 expect this to generate a single row (lvi.iItem = 0;
) with a text string under each column (lvi.iSubItem = i;
). This is what it displays instead:
修改 lvi.iSubItem = I
到 lvi.iSubItem = 0
中的每个文本字符串被显示为在第一列中的新行:
Changing lvi.iSubItem = i
to lvi.iSubItem = 0
results in each text string being displayed as a new row in the first column:
我和它玩耍了,硬编码两个数字的iItem
和 iSubItem
,改变既 I
,但我不能让它显示文本以外的任何地方的第一列等。我在做什么错了?
I've played around with it, hardcoding the numbers on both iItem
and iSubItem
, changing both to i
, but I can't get it to display the text anywhere other than the first column. What am I doing wrong?
推荐答案
首先,你的的CNAME
和项目
阵列被声明为指针数组,但你不分配它们的内存;你需要将它们声明为一个字符串数组,如 wchar_t的CNAME记录[100] [40];
First of all, your cnames
and items
arrays are declared as array of pointers, but you are not allocating memory for them; you would need to declare them as an array of strings, like wchar_t cnames[100][40];
.
其次,你需要使用 ListView_InsertItem
插入一个项目,为第一列设置的值,然后使用的来添加更多的列,像
Secondly, you need to use ListView_InsertItem
to insert an item and set the value for the first column, then use ListView_SetItem to add additional columns, like
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);
}
这篇关于项和子项的列表视图控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!