WIN32 ListView 实现间隔色的2种方法

第一种:创建LISTVIEW 时需要 添加 LVS_OWNERDRAWFIXED式样。然后在主窗体事件中添加以下代码。

LPDRAWITEMSTRUCT pDis = (LPDRAWITEMSTRUCT)lparam;

	switch (pDis->CtlType)
	{
	case ODT_LISTVIEW:
	{
		HBRUSH hbrBkColor = CreateSolidBrush(RGB(255, 255, 255));
		HBRUSH hbrBkColor2 = CreateSolidBrush(RGB(245, 245, 245));
		HBRUSH hbrSEL = CreateSolidBrush(RGB(155, 197, 255));

		if ((pDis->itemAction | ODA_SELECT) && (pDis->itemState & ODS_SELECTED))
		{
			FillRect(pDis->hDC, &pDis->rcItem, hbrSEL);
		}
		else
		{
			if ((pDis->itemID % 2) != 0)
				FillRect(pDis->hDC, &pDis->rcItem, hbrBkColor);
			//SetDCBrushColor(pDis->hDC, RGB(255, 255, 255));
			else
				//SetDCBrushColor(pDis->hDC, RGB(248, 248, 248));
				FillRect(pDis->hDC, &pDis->rcItem, hbrBkColor2);
		}

		int count = Header_GetItemCount(ListView_GetHeader(m_hListView));
		wchar_t buffer[256] = { 0 };

		RECT ItemRc = {};
		LVCOLUMN myLVCOLUMN = {};
		myLVCOLUMN.mask = LVCF_FMT;
		UINT Format = DT_CENTER;

		for (int i = 0; i < count; i++)
		{
			ListView_GetSubItemRect(pDis->hwndItem, pDis->itemID, i, LVIR_LABEL, &ItemRc);
			if (i == 0)
				StringCbPrintfW(buffer, 256, L"%d", pDis->itemID + 1);
			else
				ListView_GetItemText(pDis->hwndItem, pDis->itemID, i, (LPWSTR)buffer, 256);

			ListView_GetColumn(m_hListView, i, &myLVCOLUMN);



			if (myLVCOLUMN.fmt & LVCFMT_CENTER)
				Format = DT_CENTER;
			else if (myLVCOLUMN.fmt & LVCFMT_RIGHT)
				Format = DT_RIGHT;
			else
				Format = DT_LEFT;

			if (i == 0)
				Format = DT_CENTER;

			DrawText(pDis->hDC, (LPWSTR)buffer, -1, &ItemRc, DT_SINGLELINE | DT_VCENTER | Format);
		}

第二种方法:不需要在创建时添加LVS_OWNERDRAWFIXED式样,直接响应膏体的WM_NOTIFY事件,在中添加以下代码,都能实现同样的效果。

if (((LPNMHDR)lParam)->code == NM_CUSTOMDRAW)
{
LPNMLVCUSTOMDRAW lpdis = (LPNMLVCUSTOMDRAW)lParam;

	switch (lpdis->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
		return CDRF_NOTIFYITEMDRAW;
	case CDDS_ITEMPREPAINT:
		return CDRF_NOTIFYSUBITEMDRAW;

	case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
		HBRUSH hbrBkColor_1 = CreateSolidBrush(RGB(255, 255, 255));
		HBRUSH hbrBkColor_2 = CreateSolidBrush(RGB(245, 245, 245));
		HBRUSH hbrSEL = CreateSolidBrush(RGB(155, 197, 255));

		int nItem = (int)lpdis->nmcd.dwItemSpec;
		int nSubItem = (int)lpdis->iSubItem;

		BOOL bItemSelected = ListView_GetItemState(hwnd, nItem, LVIS_SELECTED);

		if (bItemSelected)
		{
			FillRect(lpdis->nmcd.hdc, &lpdis->nmcd.rc, hbrSEL);
		}
		else
			if (nItem % 2 == 0)
			{
				FillRect(lpdis->nmcd.hdc, &lpdis->nmcd.rc, hbrBkColor_2);
			}
			else
			{
				FillRect(lpdis->nmcd.hdc, &lpdis->nmcd.rc, hbrBkColor_1);
			}

		TCHAR szText[100] = { 0 };

		if (nSubItem == 0)
		{
			StringCbPrintf(szText, 100 * sizeof(TCHAR), L"%d", nItem + 1);
		}
		else
		{
			ListView_GetItemText(hwnd, nItem, nSubItem, szText, 100);
		}

		LVCOLUMN myLVCOLUMN = {};
		myLVCOLUMN.mask = LVCF_FMT;
		ListView_GetColumn(hwnd, nSubItem, &myLVCOLUMN);

		UINT Format = DT_CENTER;

		if (myLVCOLUMN.fmt & LVCFMT_CENTER)
			Format = DT_CENTER;
		else if (myLVCOLUMN.fmt & LVCFMT_RIGHT)
			Format = DT_RIGHT;
		else
			Format = DT_LEFT;

		if (nSubItem == 0)
			Format = DT_CENTER;
		DrawText(lpdis->nmcd.hdc, szText, lstrlen(szText), &lpdis->nmcd.rc, Format);

		DeleteObject(hbrBkColor_1);
		DeleteObject(hbrBkColor_2);
		DeleteObject(hbrSEL);

		return CDRF_SKIPDEFAULT;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值