list-report

一、处理绘画前段


1. 绘制行
void MainWindow::notify(Message &message )
{
 if (message.wparam == _pListView->getCtrlId()) {
  NMHDR *pNmhder = (NMHDR *)message.lparam;
  if (pNmhder->code == NM_CUSTOMDRAW) {
   NMCUSTOMDRAW *pCustomDraw = (NMCUSTOMDRAW *)message.lparam;
   customDraw(message, pCustomDraw);
  }
 }
}


void MainWindow::customDraw(Message &message, NMCUSTOMDRAW *pCustomDraw)
{
 // 绘制开始前, 通知父窗口任何与该项相关联的操作
 if (CDDS_PREPAINT == pCustomDraw->dwDrawStage) {
  message.result = CDRF_NOTIFYITEMDRAW;
 }

 // 行绘制开始前
 if (CDDS_ITEMPREPAINT == pCustomDraw->dwDrawStage) {
  // 获取当前所在行
  int row = (int)pCustomDraw->dwItemSpec;

  // 获取list view 的CustomDraw
  NMLVCUSTOMDRAW *pLVCustomDraw = (NMLVCUSTOMDRAW *)pCustomDraw;

  // 奇数行字体为红色, 偶数行为绿色
  if (!(row % 2)) {
   pLVCustomDraw->clrText = RGB(255, 0, 0);
   pLVCustomDraw->clrTextBk = RGB(35, 34, 30);
  } else {
   pLVCustomDraw->clrText = RGB(0, 255, 0);
   pLVCustomDraw->clrTextBk = RGB(205, 180, 145);
  }
  
  // 告诉windows其它的, 由它来绘制
  message.result = CDRF_DODEFAULT;
 }
}


2. 绘制每一列
void MainWindow::notify(Message &message )
{
 if (message.wparam == _pListView->getCtrlId()) {
  NMHDR *pNmhder = (NMHDR *)message.lparam;
  if (pNmhder->code == NM_CUSTOMDRAW) {
   NMCUSTOMDRAW *pCustomDraw = (NMCUSTOMDRAW *)message.lparam;
   customDraw(message, pCustomDraw);
  }
 }
}


void MainWindow::customDraw(Message &message, NMCUSTOMDRAW *pCustomDraw)
{
 // 绘制开始前, 通知父窗口任何与该项相关联的操作
 if (CDDS_PREPAINT == pCustomDraw->dwDrawStage) {
  message.result = CDRF_NOTIFYITEMDRAW;
 }

 // 行绘制开始前, 告诉windows我们要绘制每一列
 if (CDDS_ITEMPREPAINT == pCustomDraw->dwDrawStage) {
  message.result = CDRF_NOTIFYSUBITEMDRAW;
 }

 // 绘制列
 if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pCustomDraw->dwDrawStage) {
  int row = pCustomDraw->dwItemSpec;

  NMLVCUSTOMDRAW *pLVCustomDraw = (NMLVCUSTOMDRAW *)pCustomDraw;
  int col = pLVCustomDraw->iSubItem;
 
  // 奇数行的背景色为白色, 偶数行的背景色为灰色
  if (!(row % 2)) {
   pLVCustomDraw->clrTextBk = RGB(255, 255, 255);
  } else {
   pLVCustomDraw->clrTextBk = RGB(181, 181, 181);
  }

  // 奇数列的字体为绿色, 偶数列的字体为蓝色
  if (!(col % 2)) {
   pLVCustomDraw->clrText = RGB(0, 255, 0);
  } else {
   pLVCustomDraw->clrText = RGB(0, 0, 255);
  }


  // 告诉windows其它的, 由它来绘制
  message.result = CDRF_DODEFAULT;
 }
}


二、处理绘画后段

你可能感兴趣的:(list-report)