win32 sdk 列表视图控件绘制

  1. //////////////////////////////////////////////////////////////
  2. LRESULT ListViewCustomDraw(HWND hwnd, LPARAM lParam)
  3. {
  4. LPNMHDR pnmh = (LPNMHDR) lParam;
  5. if (pnmh->code != NM_CUSTOMDRAW) return 0;
  6. LPNMLVCUSTOMDRAW lpNMCustomDraw = (LPNMLVCUSTOMDRAW) lParam;
  7. int nResult = CDRF_DODEFAULT;
  8. if (CDDS_PREPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
  9. {
  10. nResult = CDRF_NOTIFYITEMDRAW;
  11. }
  12. else if (CDDS_ITEMPREPAINT == lpNMCustomDraw->nmcd.dwDrawStage)
  13. {
  14. nResult = CDRF_NOTIFYSUBITEMDRAW;
  15. }
  16. else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == lpNMCustomDraw->nmcd.dwDrawStage)
  17. {
  18. nResult = CDRF_SKIPDEFAULT;
  19. const DWORD dwStyle = DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS;
  20. HDC hdc = lpNMCustomDraw->nmcd.hdc;
  21. SetBkMode(hdc,TRANSPARENT);
  22. int nItem = (int)lpNMCustomDraw->nmcd.dwItemSpec;
  23. int nSubItem = lpNMCustomDraw->iSubItem;
  24. BOOL bItemSelected = ListView_GetItemState(hwnd, nItem, LVIS_SELECTED);
  25. RECT subItemRect;
  26. ListView_GetSubItemRect(hwnd, nItem, nSubItem, LVIR_BOUNDS, &subItemRect);
  27. //
  28. HBRUSH brsh=0;
  29. if (bItemSelected)
  30. {   //OutputDebugString("bItemSelected\n");
  31. brsh=CreateSolidBrush(RGB(255, 128, 128));//yellow
  32. FillRect(hdc, &subItemRect,brsh);
  33. }
  34. else
  35. {// not Selected
  36. brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30));
  37. FillRect(hdc, &subItemRect,brsh);
  38. }
  39. if(brsh) DeleteObject(brsh);
  40. //
  41. TCHAR szText[260];
  42. ListView_GetItemText(hwnd, nItem, nSubItem, szText, 260);
  43. DrawText(hdc, szText, strlen(szText), &subItemRect, dwStyle);
  44. }
  45. return nResult;
  46. }

关键:
else
{// not Selected
brsh=CreateSolidBrush(RGB(51+nItem*30, 153, 255-nItem*30));
FillRect(hdc, &subItemRect,brsh);
}

上一篇:WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题


下一篇:Win32 SDK:ListBox 为什么不整个 LB_SETTEXT