////////////////////////////////////////////////////////////// LRESULT ListViewCustomDraw(HWND hwnd, LPARAM lParam) { LPNMHDR pnmh = (LPNMHDR) lParam; if (pnmh->code != NM_CUSTOMDRAW) return 0; LPNMLVCUSTOMDRAW lpNMCustomDraw = (LPNMLVCUSTOMDRAW) lParam; int nResult = CDRF_DODEFAULT; if (CDDS_PREPAINT == lpNMCustomDraw->nmcd.dwDrawStage) { nResult = CDRF_NOTIFYITEMDRAW; } else if (CDDS_ITEMPREPAINT == lpNMCustomDraw->nmcd.dwDrawStage) { nResult = CDRF_NOTIFYSUBITEMDRAW; } else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == lpNMCustomDraw->nmcd.dwDrawStage) { nResult = CDRF_SKIPDEFAULT; const DWORD dwStyle = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS; HDC hdc = lpNMCustomDraw->nmcd.hdc; SetBkMode(hdc,TRANSPARENT); int nItem = (int)lpNMCustomDraw->nmcd.dwItemSpec; int nSubItem = lpNMCustomDraw->iSubItem; BOOL bItemSelected = ListView_GetItemState(hwnd, nItem, LVIS_SELECTED); RECT subItemRect; ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_BOUNDS, &subItemRect); // HBRUSH brsh=0; if (bItemSelected) { //OutputDebugString("bItemSelected\n"); brsh=CreateSolidBrush(RGB(255, 128, 128));//yellow FillRect(hdc, &subItemRect,brsh); } else {// not Selected brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30)); FillRect(hdc, &subItemRect,brsh); } if(brsh) DeleteObject(brsh); // TCHAR szText[260]; ListView_GetItemText(hwnd, nItem, nSubItem, szText, 260); DrawText(hdc, szText, strlen(szText), &subItemRect, dwStyle); } return nResult; }
关键:
else
{// not Selected
brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30));
FillRect(hdc, &subItemRect,brsh);
}