EDN Admin
Well-known member
Hi,
I am implementing this using the Windows API in C++, using Visual Studio 2008 on Windows XP.
I am having trouble adding groups to my List View control. I can add items and columns to my List View control. I have enabled
GroupView using the:
<div style="color:Black; background-color:White
<pre>ListView_EnableGroupView(hwnd_ListView, TRUE);<br/>
[/code]
and this returns 0, which in this context according to the MSDN documentation means: "The ability to display list-view items as a group is already enabled".
(I dont know if this is helpful, but I have noticed that if I set the 2nd parameter to
FALSE it also returns 0, which cant be correct.)
I create an LVGROUP and fill the required members and Insert the group:
<div style="color:Black; background-color:White
<pre>LVGROUP lv_Group1 = {0};<br/>
<br/>
lv_Group1.cbSize = <span style="color:Blue sizeof
(LVGROUP);<br/>
lv_Group1.mask = LVGF_HEADER | LVGF_GROUPID;<br/>
lv_Group1.pszHeader = L<span style="color:#a31515 "Group1"
;<br/>
lv_Group1.iGroupId = 1;<br/>
<br/>
<span style="color:Blue if
(ListView_InsertGroup(hwnd_ListView, 0, &lv_Group1) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "InsertGroup Failed!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
[/code]
Finally, I insert an item to the group to make is visible:
<div style="color:Black; background-color:White
<pre>LVITEM lv_Item1 = {0};<br/>
<br/>
lv_Item1.mask = LVIF_TEXT | LVIF_GROUPID | LVIF_COLUMNS;<br/>
lv_Item1.iItem = 0;<br/>
lv_Item1.iSubItem = 0;<br/>
lv_Item1.pszText = <span style="color:#a31515 "Item 1"
;<br/>
lv_Item1.iGroupId = 1;<br/>
<br/>
ListView_InsertItem(hwnd_ListView, &lv_Item1);<br/>
[/code]
This results in the item being displayed, but no group is displayed. All the calls are inside the WM_CREATE message handler.
Here is the full implementation of the WM_CREATE message handler:
<div style="color:Black; background-color:White
<pre><span style="color:Blue case
WM_CREATE:<br/>
{<br/>
icex.dwSize = <span style="color:Blue sizeof
(INITCOMMONCONTROLSEX);<br/>
icex.dwICC = ICC_TAB_CLASSES;<br/>
InitCommonControlsEx(&icex);<br/>
<br/>
RECT rc;<br/>
GetClientRect(hwnd, &rc);<br/>
<br/>
hwnd_ListView = CreateWindowEx(0, WC_LISTVIEW, <span style="color:#a31515 ""
, WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT,<br/>
0, 0, rc.right - rc.left, rc.bottom - rc.top, hwnd, <br/>
(HMENU) IDC_LISTVIEW, g_hinst, NULL);<br/>
<br/>
<span style="color:Blue if
(hwnd_ListView == NULL)<br/>
{<br/>
DWORD rv_CreateListView = GetLastError();<br/>
<span style="color:Blue char
err_CreateListView[MAXLEN];<br/>
sprintf_s(err_CreateListView, MAXLEN, <span style="color:#a31515 "CreateListView Failed! Error Code: %d"
<br/>
, rv_CreateListView);<br/>
MessageBox(NULL, err_CreateListView, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<span style="color:Blue int
rv_EnGroup[3];<br/>
rv_EnGroup[0] = ListView_EnableGroupView(hwnd_ListView, FALSE);<br/>
<br/>
<span style="color:Blue if
(rv_EnGroup[1] = ListView_EnableGroupView(hwnd_ListView, TRUE) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "GroupView not Enabled!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
rv_EnGroup[2] = ListView_EnableGroupView(hwnd_ListView, TRUE);<br/>
<br/>
LVGROUP lv_Group1 = {0};<br/>
<br/>
lv_Group1.cbSize = <span style="color:Blue sizeof
(LVGROUP);<br/>
lv_Group1.mask = LVGF_HEADER|LVGF_GROUPID;<br/>
lv_Group1.pszHeader = L<span style="color:#a31515 "First group"
;<br/>
lv_Group1.iGroupId = 1;<br/>
<br/>
<span style="color:Blue if
(ListView_InsertGroup(hwnd_ListView, 0, &lv_Group1) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "InsertGroup Failed!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
HIMAGELIST hSmall;<br/>
HICON hIcon; <br/>
<br/>
hSmall = ImageList_Create(16, 16, ILC_COLOR4, 1, 0 );<br/>
<br/>
<span style="color:Blue if
(hSmall == NULL)<br/>
{<br/>
DWORD rv_CreateImageList = GetLastError(); <br/>
<br/>
<span style="color:Blue char
err_CreateImageList[MAXLEN];<br/>
sprintf_s(err_CreateImageList, MAXLEN, <span style="color:#a31515 "CreateImageList Failed! Error Code: %d"
<br/>
, rv_CreateImageList);<br/>
MessageBox(NULL, err_CreateImageList, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
hIcon = LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_ICON1));<br/>
<br/>
if(hIcon == NULL)<br/>
{<br/>
DWORD rv_LoadIcon = GetLastError(); <br/>
<br/>
<span style="color:Blue char
err_LoadIcon[MAXLEN];<br/>
sprintf_s(err_LoadIcon, MAXLEN, <span style="color:#a31515 "LoadIcon Failed! Error Code: %d"
<br/>
, rv_LoadIcon);<br/>
MessageBox(NULL, err_LoadIcon, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
<span style="color:Blue if
(ImageList_AddIcon(hSmall, hIcon) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "AddIcon Failed!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
LV_COLUMN lvC;<br/>
<br/>
lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;<br/>
lvC.fmt = LVCFMT_LEFT;<br/>
lvC.cx = 75;<br/>
lvC.pszText = <span style="color:#a31515 "Col1"
;<br/>
<br/>
ListView_InsertColumn(hwnd_ListView, 0, &lvC);<br/>
<br/>
LVITEM lv_Item1 = {0}; <br/>
lv_Item1.mask = LVIF_TEXT | LVIF_GROUPID | LVIF_COLUMNS;<br/>
lv_Item1.iItem = 0;<br/>
lv_Item1.iSubItem = 0;<br/>
lv_Item1.pszText = <span style="color:#a31515 "Item 1"
;<br/>
lv_Item1.iGroupId = 1;<br/>
<br/>
ListView_InsertItem(hwnd_ListView, &lv_Item1);<br/>
}<br/>
<span style="color:Blue break
;<br/>
[/code]
Thanks for any help you can offer me!
View the full article
I am implementing this using the Windows API in C++, using Visual Studio 2008 on Windows XP.
I am having trouble adding groups to my List View control. I can add items and columns to my List View control. I have enabled
GroupView using the:
<div style="color:Black; background-color:White
<pre>ListView_EnableGroupView(hwnd_ListView, TRUE);<br/>
[/code]
and this returns 0, which in this context according to the MSDN documentation means: "The ability to display list-view items as a group is already enabled".
(I dont know if this is helpful, but I have noticed that if I set the 2nd parameter to
FALSE it also returns 0, which cant be correct.)
I create an LVGROUP and fill the required members and Insert the group:
<div style="color:Black; background-color:White
<pre>LVGROUP lv_Group1 = {0};<br/>
<br/>
lv_Group1.cbSize = <span style="color:Blue sizeof
(LVGROUP);<br/>
lv_Group1.mask = LVGF_HEADER | LVGF_GROUPID;<br/>
lv_Group1.pszHeader = L<span style="color:#a31515 "Group1"
;<br/>
lv_Group1.iGroupId = 1;<br/>
<br/>
<span style="color:Blue if
(ListView_InsertGroup(hwnd_ListView, 0, &lv_Group1) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "InsertGroup Failed!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
[/code]
Finally, I insert an item to the group to make is visible:
<div style="color:Black; background-color:White
<pre>LVITEM lv_Item1 = {0};<br/>
<br/>
lv_Item1.mask = LVIF_TEXT | LVIF_GROUPID | LVIF_COLUMNS;<br/>
lv_Item1.iItem = 0;<br/>
lv_Item1.iSubItem = 0;<br/>
lv_Item1.pszText = <span style="color:#a31515 "Item 1"
;<br/>
lv_Item1.iGroupId = 1;<br/>
<br/>
ListView_InsertItem(hwnd_ListView, &lv_Item1);<br/>
[/code]
This results in the item being displayed, but no group is displayed. All the calls are inside the WM_CREATE message handler.
Here is the full implementation of the WM_CREATE message handler:
<div style="color:Black; background-color:White
<pre><span style="color:Blue case
WM_CREATE:<br/>
{<br/>
icex.dwSize = <span style="color:Blue sizeof
(INITCOMMONCONTROLSEX);<br/>
icex.dwICC = ICC_TAB_CLASSES;<br/>
InitCommonControlsEx(&icex);<br/>
<br/>
RECT rc;<br/>
GetClientRect(hwnd, &rc);<br/>
<br/>
hwnd_ListView = CreateWindowEx(0, WC_LISTVIEW, <span style="color:#a31515 ""
, WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT,<br/>
0, 0, rc.right - rc.left, rc.bottom - rc.top, hwnd, <br/>
(HMENU) IDC_LISTVIEW, g_hinst, NULL);<br/>
<br/>
<span style="color:Blue if
(hwnd_ListView == NULL)<br/>
{<br/>
DWORD rv_CreateListView = GetLastError();<br/>
<span style="color:Blue char
err_CreateListView[MAXLEN];<br/>
sprintf_s(err_CreateListView, MAXLEN, <span style="color:#a31515 "CreateListView Failed! Error Code: %d"
<br/>
, rv_CreateListView);<br/>
MessageBox(NULL, err_CreateListView, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<span style="color:Blue int
rv_EnGroup[3];<br/>
rv_EnGroup[0] = ListView_EnableGroupView(hwnd_ListView, FALSE);<br/>
<br/>
<span style="color:Blue if
(rv_EnGroup[1] = ListView_EnableGroupView(hwnd_ListView, TRUE) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "GroupView not Enabled!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
rv_EnGroup[2] = ListView_EnableGroupView(hwnd_ListView, TRUE);<br/>
<br/>
LVGROUP lv_Group1 = {0};<br/>
<br/>
lv_Group1.cbSize = <span style="color:Blue sizeof
(LVGROUP);<br/>
lv_Group1.mask = LVGF_HEADER|LVGF_GROUPID;<br/>
lv_Group1.pszHeader = L<span style="color:#a31515 "First group"
;<br/>
lv_Group1.iGroupId = 1;<br/>
<br/>
<span style="color:Blue if
(ListView_InsertGroup(hwnd_ListView, 0, &lv_Group1) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "InsertGroup Failed!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
HIMAGELIST hSmall;<br/>
HICON hIcon; <br/>
<br/>
hSmall = ImageList_Create(16, 16, ILC_COLOR4, 1, 0 );<br/>
<br/>
<span style="color:Blue if
(hSmall == NULL)<br/>
{<br/>
DWORD rv_CreateImageList = GetLastError(); <br/>
<br/>
<span style="color:Blue char
err_CreateImageList[MAXLEN];<br/>
sprintf_s(err_CreateImageList, MAXLEN, <span style="color:#a31515 "CreateImageList Failed! Error Code: %d"
<br/>
, rv_CreateImageList);<br/>
MessageBox(NULL, err_CreateImageList, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
hIcon = LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_ICON1));<br/>
<br/>
if(hIcon == NULL)<br/>
{<br/>
DWORD rv_LoadIcon = GetLastError(); <br/>
<br/>
<span style="color:Blue char
err_LoadIcon[MAXLEN];<br/>
sprintf_s(err_LoadIcon, MAXLEN, <span style="color:#a31515 "LoadIcon Failed! Error Code: %d"
<br/>
, rv_LoadIcon);<br/>
MessageBox(NULL, err_LoadIcon, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
<span style="color:Blue if
(ImageList_AddIcon(hSmall, hIcon) == -1)<br/>
{<br/>
MessageBox(NULL, <span style="color:#a31515 "AddIcon Failed!"
, <span style="color:#a31515 "Error!"
, MB_OK);<br/>
}<br/>
<br/>
LV_COLUMN lvC;<br/>
<br/>
lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;<br/>
lvC.fmt = LVCFMT_LEFT;<br/>
lvC.cx = 75;<br/>
lvC.pszText = <span style="color:#a31515 "Col1"
;<br/>
<br/>
ListView_InsertColumn(hwnd_ListView, 0, &lvC);<br/>
<br/>
LVITEM lv_Item1 = {0}; <br/>
lv_Item1.mask = LVIF_TEXT | LVIF_GROUPID | LVIF_COLUMNS;<br/>
lv_Item1.iItem = 0;<br/>
lv_Item1.iSubItem = 0;<br/>
lv_Item1.pszText = <span style="color:#a31515 "Item 1"
;<br/>
lv_Item1.iGroupId = 1;<br/>
<br/>
ListView_InsertItem(hwnd_ListView, &lv_Item1);<br/>
}<br/>
<span style="color:Blue break
;<br/>
[/code]
Thanks for any help you can offer me!
View the full article