Moving items up and down in a ListView control

本文介绍了一种在Windows编程中实现ListView控件项手动交换的方法,包括如何通过自定义函数SwapLVItems来交换两个列表项的所有数据,以及如何通过MoveLVSelectedItemsUp和MoveLVSelectedItemsDown函数来实现列表项的上移和下移操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In some occasions, you might want to allow the user the change the order of items in the ListView, by moving the selected items up and down. In the API of ListView control, there is no support for swapping between 2 items. The only way to do that, is by manually swapping all data of the items, including the lParam value and all columns !
The following code snippet demonstrate how to do that. The SwapLVItems function swap between 2 items (iItem1 and iItem2 are the index of the items in the ListView).
The MoveLVSelectedItemsUp and MoveLVSelectedItemsDown functions move the selected items up and down.

Return back to C/C++ code index

UINT GetLVItemState(HWND hwnd, int i, UINT mask)
{
	return ListView_GetItemState(hwnd, i, mask);
}

void GetLVItemText(HWND hwnd, int iItem, int iSubItem, LPSTR pszText, int cchTextMax)
{
	ListView_GetItemText(hwnd, iItem, iSubItem, pszText, cchTextMax);
}

void SetLVItemText(HWND hwnd, int i, int iSubItem, LPSTR pszText)
{
	ListView_SetItemText(hwnd, i, iSubItem, pszText);
}


BOOL GetLVItem(HWND hListView, UINT mask, int iItem, int iSubItem, 
		LPLVITEM pitem, UINT stateMask)
{
	pitem->mask = mask;
	pitem->stateMask = stateMask;
	pitem->iItem = iItem;
	pitem->iSubItem = iSubItem;
	return ListView_GetItem(hListView, pitem);
}


int GetHeaderItemCount(HWND hwndHD)
{
	return Header_GetItemCount(hwndHD);
}

HWND GetLVHeaderControl(HWND hListView)
{
	return ListView_GetHeader(hListView);
}

int GetLVColumnsCount(HWND hListView)
{
	return (GetHeaderItemCount(GetLVHeaderControl(hListView)));
}


void SwapLVItems(HWND hListView, int iItem1, int iItem2)
{
	//I assume that 4K buffer is really enough for storing the content of a column
	const LOCAL_BUFFER_SIZE = 4096;
	LVITEM lvi1, lvi2;
	UINT uMask = LVIF_TEXT | LVIF_IMAGE | LVIF_INDENT | LVIF_PARAM  | LVIF_STATE;
	char szBuffer1[LOCAL_BUFFER_SIZE + 1], szBuffer2[LOCAL_BUFFER_SIZE + 1];
	lvi1.pszText = szBuffer1;
	lvi2.pszText = szBuffer2;
	lvi1.cchTextMax  = sizeof(szBuffer1);
	lvi2.cchTextMax  = sizeof(szBuffer2);

	BOOL bResult1 = GetLVItem(hListView, uMask, iItem1, 0, &lvi1, (UINT)-1);
	BOOL bResult2 = GetLVItem(hListView, uMask, iItem2, 0, &lvi2, (UINT)-1);

	if (bResult1 && bResult2)
	{
		lvi1.iItem = iItem2;
		lvi2.iItem = iItem1;
		lvi1.mask = uMask;
		lvi2.mask = uMask;
		lvi1.stateMask = (UINT)-1;
		lvi2.stateMask = (UINT)-1;
		//swap the items
		ListView_SetItem(hListView, &lvi1);
		ListView_SetItem(hListView, &lvi2);

		int iColCount = GetLVColumnsCount(hListView);
		//Loop for swapping each column in the items.
		for (int iIndex = 1; iIndex < iColCount; iIndex++)
		{
			szBuffer1[0] = '/0';
			szBuffer2[0] = '/0';
			GetLVItemText(hListView, iItem1, iIndex, 
					szBuffer1, LOCAL_BUFFER_SIZE);
			GetLVItemText(hListView, iItem2, iIndex, 
					szBuffer2, LOCAL_BUFFER_SIZE);
			SetLVItemText(hListView, iItem2, iIndex, szBuffer1);
			SetLVItemText(hListView, iItem1, iIndex, szBuffer2);
		}
	}
}

//Move up the selected items
void MoveLVSelectedItemsUp(HWND hListView)
{
	int iCount = ListView_GetItemCount(hListView);

	for (int iIndex = 1; iIndex < iCount; iIndex++)
		if (GetLVItemState(hListView, iIndex, LVIS_SELECTED) != 0)
			SwapLVItems(hListView, iIndex, iIndex - 1);

}

//Move down the selected items
void MoveLVSelectedItemsDown(HWND hListView)
{
	int iCount = ListView_GetItemCount(hListView);

	for (int iIndex = iCount - 1; iIndex >= 0; iIndex--)
		if (GetLVItemState(hListView, iIndex, LVIS_SELECTED) != 0)
			SwapLVItems(hListView, iIndex, iIndex + 1);

}

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值