December 11, 2024, 09:29:46 PM

News:

IWBasic runs in Windows 11!


Grouping items in listview

Started by sapero, August 29, 2008, 07:35:44 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

August 29, 2008, 07:35:44 PM Last Edit: August 29, 2008, 07:42:42 PM by sapero
This example shows how to create a listview which looks like My Computer folder in Windows XP.
Requirements: Windows XP, visual styles manifest.
CApp::OnInitDialog(),int
{
HWND hwndList = GetDlgItem(m_hwnd, 1000);

LVGROUP lvgr;
LVITEM  lvi;

// A group cannot be inserted into an empty ListView control
lvi.iItem    = 0;
lvi.iSubItem = 0;
lvi.mask     = LVIF_TEXT;
lvi.pszText  = NULL;
SendMessage(hwndList, LVM_INSERTITEM, 0, &lvi);
// you can later call LVM_MOVEITEMTOGROUP(item, group) to move this item to a group

SendMessage(hwndList, LVM_ENABLEGROUPVIEW, true, 0);
lvi.mask     = LVIF_TEXT | LVIF_GROUPID;

lvgr.cbSize  = sizeof(lvgr);
lvgr.mask    = LVGF_ALIGN | LVGF_HEADER | LVGF_GROUPID;
lvgr.uAlign  = LVGA_HEADER_LEFT;

// create first group
lvgr.iGroupId  = 0;
lvgr.pszHeader = L"my header 1";
SendMessage(hwndList, LVM_INSERTGROUP, -1, &lvgr);

// create second group
lvgr.iGroupId  = 1;
lvgr.pszHeader = L"my header 2";
SendMessage(hwndList, LVM_INSERTGROUP, -1, &lvgr);

// insert items to first group
lvi.iGroupId = 0;
lvi.pszText  = "item in first group";
SendMessage(hwndList, LVM_INSERTITEM, 0, &lvi);
lvi.pszText  = "one more";
SendMessage(hwndList, LVM_INSERTITEM, 0, &lvi);

// insert items to second group
lvi.iGroupId = 1;
lvi.pszText  = "item in second group";
SendMessage(hwndList, LVM_INSERTITEM, 0, &lvi);
lvi.pszText  = "one more";
SendMessage(hwndList, LVM_INSERTITEM, 0, &lvi);
}


Attached complete project for experimenting ;)
Note: if you modify strings before the first unicode string, the gropus may fail to create, because our current parser does not guarantee WORD align for unicode strings. If you stick with this problem, just add or remove one character (like space) to one of strings before unicode stuff.

pistol350

That's quite interesting.
Thank you for sharing it.  8)
Regards,

Peter B.