ListView Custom Draw - 不同行(列)显示不同的颜色

类别:VC语言 点击:0 评论:0 推荐:

怎样使得ListView的不同行的颜色不同,或者不同列的颜色不同。在MFC中,例子比较多,但是SDK下,很少有完整的例子。
这里给出个例子,我试了挺长时间,呵呵,才开始Windows开发学习,见笑了。

碰到的困难有两个,一个是,处理完NM_CUSTOMDRAW消息后,怎样提供返回值的问题(通过SetWindowLong)。再有一个是,设置了返回值后,对话框的窗口处理函数要返回1。

#include "stdafx.h"
#include "DlgListVw.h"

typedef struct tagProxyInfo
{
    char ip[20];
    char port[10];
    char desc[20];
} PROXYINFO, *PPROXYINFO;

PROXYINFO Proxys[] = {
 {"198.26.118.46", "80", _T("美国 未知地址")},
 {"198.26.118.57", "8080", _T("美国 未知地址")},
 {"198.26.120.228", "80", _T("美国 未知地址")},
 {"66.201.48.36", "80", _T("美国 加利福尼亚州")},
 {"202.175.26.149", "80", _T("澳门 特别行政区")},
 {"168.143.113.13", "8080", _T("美国 Englewood")},
 {"24.252.80.76", "8000", _T("美国 Cox通信公司")},
 {"210.178.220.65", "8080", _T("韩国 汉城")},
 {"206.207.248.173", "80", _T("美国 WestNet")},
 {"12.129.196.86", "234", _T("美国 ATT用户")}
};

#define PROXY_COUNT (sizeof(Proxys) / sizeof(Proxys[0]))

BOOL InsertListViewItems(HWND hwndListView)
{
 //set the number of items in the list
 ListView_SetItemCount(hwndListView, PROXY_COUNT);

 LVITEM lvi;

 lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
 lvi.state = 0;
 lvi.stateMask = 0;

 for (int idx = 0; idx < PROXY_COUNT; ++idx)
 {
  lvi.iItem = idx;
  lvi.iSubItem = 0;
  lvi.lParam = (LPARAM)&Proxys[idx];
  lvi.pszText = Proxys[idx].ip;
 //lvi.pszText = LPSTR_TEXTCALLBACK;

  ListView_InsertItem(hwndListView, &lvi);
  ListView_SetItemText(hwndListView, idx, 1, Proxys[idx].port);
  ListView_SetItemText(hwndListView, idx, 2, Proxys[idx].desc);
 }

 return TRUE;
}

BOOL InitListView(HWND hwndListView)
{
 LV_COLUMN   lvColumn;
 int         i;
 TCHAR       szString[3][20] = {_T("IP"), _T("Port"), _T("描述")};

 //empty the list
 ListView_DeleteAllItems(hwndListView);

 //initialize the columns
 lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
 lvColumn.fmt = LVCFMT_LEFT;
 lvColumn.cx = 120;
 for(i = 0; i < 3; i++)
 {
  lvColumn.pszText = szString[i];
  ListView_InsertColumn(hwndListView, i, &lvColumn);
 }

 InsertListViewItems(hwndListView);

 return TRUE;
}


LRESULT CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 HWND hListView;
 //NMLVDISPINFO *plvdi;
 LPNMLVCUSTOMDRAW lplvcd;
 int nResult = 0;
 
 switch (message)
 {
 case WM_INITDIALOG:
  hListView = GetDlgItem(hDlg, IDC_LIST1);
  InitListView(hListView);

  break;
 case WM_NOTIFY:
  switch(((LPNMHDR)lParam)->code)
  {
  /*lvi.pszText = LPSTR_TEXTCALLBACK; */
 /*
  case LVN_GETDISPINFO:
   plvdi = (NMLVDISPINFO *)lParam;
   switch (plvdi->item.iSubItem)
   {
   case 0:
    plvdi->item.pszText = Proxys[plvdi->item.iItem].ip;
    break;
    
   case 1:
    plvdi->item.pszText = Proxys[plvdi->item.iItem].port;
    break;

   case 2:
    plvdi->item.pszText = Proxys[plvdi->item.iItem].desc;
    break;
   }
   break;
  */
  case  NM_CUSTOMDRAW:
   lplvcd = (LPNMLVCUSTOMDRAW)lParam;
   switch(lplvcd->nmcd.dwDrawStage)
   {
    case CDDS_PREPAINT:
     SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)(CDRF_NOTIFYITEMDRAW));
     nResult = 1;

     break;
    case CDDS_ITEMPREPAINT:
     if (lplvcd->nmcd.dwItemSpec % 2 == 0)
     {
      lplvcd->clrTextBk = RGB(192, 192, 192) ;
     }
     else
     {
      lplvcd->clrTextBk =  RGB(220, 220, 220);
     }
     
     //SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)(CDRF_NOTIFYSUBITEMDRAW));
     SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)(CDRF_DODEFAULT));

     nResult = 1; //Must return 1

     break;
    
    // Un comment the followed code, if the code avoe return CDRF_NOTIFYSUBITEMDRAW
    // Changing column color
    /*
    case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
     if(lplvcd->iSubItem %2){
      lplvcd->clrTextBk = RGB(255,0,0);
     }
     else{
      lplvcd->clrTextBk = RGB(128,128,255);
     }

     SetWindowLong(hDlg, DWL_MSGRESULT, (LONG)(CDRF_DODEFAULT));

     nResult = 1; //Muse return 1

     break;
    */
   }

   break;
  }
  break;

 case WM_COMMAND:
  EndDialog(hDlg, LOWORD(wParam));
  break;
 }

 return nResult;
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
 InitCommonControls();

 DialogBox(hInstance, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)DlgProc);

 return 0;
}

本文地址:http://com.8s8s.com/it/it325.htm