How to make the colors of different lines of ListView, or different colors of colors. In the MFC, the example is more, but the SDK is rarely an example. Here, I have tried it for a long time, huh, I started to develop learning, laughing. There are two difficulties encountered, one is, how to provide the return value after processing the NM_CUSTOMDRAW message (through SetWindowlong). Then there is a return value after setting the return value, the window processing function of the dialog box is to return 1. #include "stdafx.h" #include "dlglistvw.h" typef struct tagproxyInfo {char ip [20]; char port [10]; char desc [20];} proxyInfo, * pproxyInfo;
ProxyInfo Proxys [] = {{"198.26.118.46", "80", _T ("American Unknown Address")}, {"198.26.118.57", "8080", _T ("American Unknown Address")}, {" 198.26.120.228 "," 80 ", _T (" American Unknown Address ")}, {" 66.201.48.36 "," 80 ", _T (" United States California ")}, {" 202.175.26.149 "," 80 " , _T ("Macau Special Administrative Region")}, {"168.143.113.13", "8080", _T ("US Englewood")}, {"24.252.80.76", "8000", _T ("US Cox Communication Company" )} {"210.178.220.65", "8080", _t ("Korea Seoul")}, {"206.207.248.173", "80", _T ("US WESTNET")}, {"12.129.196.86", "234", _T ("US ATT User")}}
#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 ListView_InsertItem (hwndListView, & lvi); ListView_SetItemText (hwndListView, idx, 1, Proxys [idx] .port); ListView_SetItemText (hwndListView, idx, 2, Proxys [idx] .desc);} return TRUE;} Bool INTLISTVIEW (HWND HWNDLISTVIEW) {LV_COLUMN LVCCOLUMN; INT i; tchar szstring [3] [20] = {_T ("ip"), _t ("port"), _t ("description")}; // 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; lParam lplvcd = (LPNMLVCUSTOMDRAW); switch (lplvcd-> nmcd.dwDrawStage) {case: case 2: * / case NM_CUSTOMDRAW; plvdi-> item.pszText = Proxys [plvdi-> item.iItem] .desc; break;} break CDDS_PREPAINT: SETWINDOWLONG (HDLG, DWL_MSGRESULT, (LONG) (CDRF_NOTIITEMDRAW); 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, (cdrf_notifyspace); setWindowlong (HDLG, DWL_MSGRESULT, (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) {INTCOMMONCONTROLS (); DialogBox (Hinstance, (LPCTSTR) IDD_DIALOG1, NULL, (DLGPROC) DLGPROC); Return 0;}