I want to change the words' color in a ListCtrl ,I Write the code in all Windows SDK ways, I meet the problem,when I response the WM_NOTIFY message(I am sure the NM_CUSTOMDRAW message is sent from my ListCtrl),I find that through I have return
CDRF_NOTIFYITEMDRAW
,I can't receive the next
NM_CUSTOMDRAW notification
with dwDrawStage setting to
CDDS_ITEMPREPAINT
,I receive the
NM_CUSTOMDRAW notification
only once,I don't konw why? who can help me..The following code have problem :(My list Control is already been report mode)
int nResult = CDRF_DODEFAULT;
int nResult=CDRF_DODEFAULT;
if(lpNmlvCustomDraw->nmcd.dwDrawStage==CDDS_PREPAINT)
{
nResult = CDRF_NOTIFYITEMDRAW;
}
else if(lpNmlvCustomDraw->nmcd.dwDrawStage==CDDS_ITEMPREPAINT)
{
nResult=CDRF_NOTIFYITEMDRAW;
}
the follow is my all code:
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include"ListView.h"
ListView lvMyListView;
HWND hListViewWnd;
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
hListViewWnd=GetDlgItem(hWnd,IDC_MyListView);
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
case WM_NOTIFY:
{
NMHDR* lpNmhdr=(NMHDR*)lParam;
switch(lpNmhdr->code)
{
case NM_CUSTOMDRAW:
{
if(lpNmhdr->idFrom==IDC_MyListView)
{
LPNMLVCUSTOMDRAW lpNmlvCustomDraw = reinterpret_cast<lpnmlvcustomdraw>(lParam);
int nResult = CDRF_DODEFAULT;
if(lpNmlvCustomDraw->nmcd.dwDrawStage==CDDS_PREPAINT)
{
nResult = CDRF_NOTIFYITEMDRAW;
}
else if(lpNmlvCustomDraw->nmcd.dwDrawStage==CDDS_ITEMPREPAINT)
{
nResult=CDRF_NOTIFYSUBITEMDRAW;
}
else if(lpNmlvCustomDraw->nmcd.dwDrawStage==(CDDS_ITEMPREPAINT | CDDS_SUBITEM))
{
int nItem=lpNmlvCustomDraw->nmcd.dwItemSpec;
int nSubItem=lpNmlvCustomDraw->iSubItem;
COLORREF crText;
if ( (nItem % 3) == 0 )
{
crText = RGB(255,0,0);
HDC hdc=lpNmlvCustomDraw->nmcd.hdc;
RECT subItemRect;
ListView_GetSubItemRect(hListViewWnd, nItem, nSubItem, LVIR_BOUNDS, &subItemRect);
FillRect(hdc,&subItemRect,CreateSolidBrush(RGB(100,100,100)));
}
else if ( (nItem % 3) == 1 )
crText = RGB(0,255,0);
else
crText = RGB(128,128,255);
lpNmlvCustomDraw->clrText = crText;
nResult = CDRF_NEWFONT;
}
return nResult;
}
}
break;
default:
break;
}
}
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
lvMyListView.SetMainWinhwnd(hwnd);
lvMyListView.SethList(IDC_MyListView);
lvMyListView.ListView_Init();
lvMyListView.InsertColumnItem(TEXT("文件下载进度"),500,500);
lvMyListView.InsertColumnItem(TEXT("歌曲名"),180,180);
lvMyListView.InsertColumnItem(TEXT("歌手"),151,151);
for(int index=0;index<3;index++)
{
lvMyListView.InsertRowItem(TEXT("周杰伦"));
lvMyListView.InsertRowItem(TEXT("双截棍"));
lvMyListView.InsertRowItem(TEXT("50%"));
}
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDC_OK:
{
MessageBox(hwnd,TEXT("欢迎访问如鹏网 www.RuPeng.com 大学生计算机学习社区"),TEXT("问好"),MB_OK);
}
break;
default:
break;
}
}
void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}