读书人

listview NMCUSTOMDRAW有关问题

发布时间: 2013-01-04 10:04:18 作者: rapoo

listview NMCUSTOMDRAW问题

代码如下,WM_NOTIFY下case CDDS_ITEMPREPAINT:永远进不去,请教高手为什么,应该怎么做。。我想把listview第一行第一用不同颜色显示。。


BOOL CALLBACK AboutProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE hIn;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR lpCmdLine,int nShowCmd){
hIn=hInstance;
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize=sizeof(InitCtrlEx);
InitCtrlEx.dwICC=ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrlEx);
DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,AboutProc);
}
#define LISTID 20000
BOOL CALLBACK AboutProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
static HWND hWndListView;
LV_COLUMN lvc;
LV_ITEM lvi;
RECT rect;
int i;
switch(message)
{
case WM_INITDIALOG:
GetWindowRect(hDlg,&rect);
hWndListView = CreateWindowEx(NULL,WC_LISTVIEW,
NULL,
WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_AUTOARRANGE,
20,
60,
rect.right - rect.left-50,
rect.bottom - rect.top-160,
hDlg,
NULL,
hIn,
NULL);
SetWindowLong(hWndListView,GWL_ID,LISTID);
lvc.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_FMT;
lvc.fmt=LVCFMT_CENTER;
lvc.pszText=TEXT("CREATOR");
lvc.cx=80;
SendMessage(hWndListView,LVM_INSERTCOLUMN,0,(LPARAM)&lvc);
lvc.pszText = TEXT("NAME");
SendMessage(hWndListView,LVM_INSERTCOLUMN,1,(LPARAM)&lvc);
i=0;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem =i;
lvi.lParam =i;
lvi.iSubItem =0;
lvi.mask = LVIF_TEXT;
lvi.pszText=TEXT("1");
SendMessage(hWndListView,LVM_INSERTITEM,0,(LPARAM)&lvi);
lvi.iSubItem ++;
lvi.pszText =TEXT("2");
SendMessage(hWndListView,LVM_SETITEM,0,(LPARAM)&lvi);
break;
case WM_COMMAND:
if(LOWORD(wParam)==IDOK||LOWORD(wParam)==IDCANCEL)
{
EndDialog(hDlg,0);
break;
}
break;
case WM_NOTIFY:
{
LPNMLVCUSTOMDRAW lpDraw = (LPNMLVCUSTOMDRAW) lParam;
switch(lpDraw->nmcd.hdr.idFrom)
{
case LISTID:
RECT itemRect;
HDC dc;
switch(lpDraw->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
dc=lpDraw->nmcd.hdc;
itemRect=lpDraw->nmcd.rc;
FillRect(dc,&itemRect,CreateSolidBrush(RGB(221,255,255))); return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
dc=lpDraw->nmcd.hdc;
itemRect=lpDraw->nmcd.rc;
lpDraw->clrText=RGB(255,0,255);
lpDraw->clrTextBk=RGB(221,255,255);
if(lpDraw->iSubItem==0)
{
lpDraw->clrTextBk=RGB(221,255,255);
lpDraw->clrText=RGB(0,0,255);
DrawText(dc,TEXT("1"),-1,&itemRect,DT_LEFT|DT_VCENTER|DT_SINGLELINE);
}
return CDRF_DODEFAULT;
}

}/**/
break;
}
}
return 0;
}

[解决办法]
        case CDDS_PREPAINT: 


dc = lpDraw->nmcd.hdc;
itemRect = lpDraw->nmcd.rc;
FillRect(dc, &itemRect, CreateSolidBrush(RGB(221,255,255)));
return SetWindowLongPtr(hwnd, DWLP_MSGRESULT, CDRF_NOTIFYITEMDRAW), TRUE;


[解决办法]
关于 DialogProc 对话框消息处理函数的返回值,微软说到:

Return Value

Typically, the dialog box procedure should return TRUE if it processed the message, and FALSE if it did not. If the dialog box procedure returns FALSE, the dialog manager performs the default dialog operation in response to the message.

If the dialog box procedure processes a message that requires a specific return value, the dialog box procedure should set the desired return value by calling SetWindowLong(hwndDlg, DWL_MSGRESULT, lResult) immediately before returning TRUE. Note that you must call SetWindowLong immediately before returning TRUE; doing so earlier may result in the DWL_MSGRESULT value being overwritten by a nested dialog box message.

The following messages are exceptions to the general rules stated above. Consult the documentation for the specific message for details on the semantics of the return value.

WM_CHARTOITEM
WM_COMPAREITEM
WM_CTLCOLORBTN
WM_CTLCOLORDLG
WM_CTLCOLOREDIT
WM_CTLCOLORLISTBOX
WM_CTLCOLORSCROLLBAR
WM_CTLCOLORSTATIC
WM_INITDIALOG
WM_QUERYDRAGICON
WM_VKEYTOITEM

读书人网 >VC/MFC

热点推荐