本文介绍了关于重绘ListCtrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ListCtrl中更改单词'color,我用所有Windows SDK方式编写代码,我遇到问题,当我响应WM_NOTIFY消息时(我确定NM_CUSTOMDRAW消息是从我的ListCtrl发送的),我发现通过我已经返回

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

,我无法收到下一个

NM_CUSTOMDRAW notification

将dwDrawStage设置为

with dwDrawStage setting to

CDDS_ITEMPREPAINT

,我只收到一次

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;
// First thing - check the draw stage. If it's the control's prepaint stage, then tell Windows we want messages for every item.
                        int nResult=CDRF_DODEFAULT;
                        if(lpNmlvCustomDraw->nmcd.dwDrawStage==CDDS_PREPAINT)//the value of lpNmlvCustomDraw->nmcd.dwDrawStage will always be CDDS_PREPAINT, I don't know why, who can help me?
                        {
                            nResult = CDRF_NOTIFYITEMDRAW;
                        }
                        else if(lpNmlvCustomDraw->nmcd.dwDrawStage==CDDS_ITEMPREPAINT)
                        {
//This is the notification message for an item.  We'll request notifications before each subitem's prepaint stage.
//CDRF_NOTIFYSUBITEMDRAW:Version 4.71. The control will notify the parent when a list view subitem is being drawn
//CDDS_SUBITEM Version 4.71. Flag combined with CDDS_ITEMPREPAINT or CDDS_ITEMPOSTPAINT if a subitem is being drawn. This will only be set if CDRF_NOTIFYITEMDRAW is returned from CDDS_PREPAINT.
                            //nResult = CDRF_NOTIFYSUBITEMDRAW;
                            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-&gt;code)
			{
			case NM_CUSTOMDRAW:
				{
					if(lpNmhdr-&gt;idFrom==IDC_MyListView)//如果是ListCtr控件发送的。
					{

						 LPNMLVCUSTOMDRAW lpNmlvCustomDraw = reinterpret_cast<lpnmlvcustomdraw>(lParam);

						 // Take the default processing unless we set this to something else below.
						int nResult = CDRF_DODEFAULT; // 默认由系统绘制, 除非下面进行了自定义绘制
// First thing - check the draw stage. If it's the control's prepaint stage, then tell Windows we want messages for every item.
						if(lpNmlvCustomDraw-&gt;nmcd.dwDrawStage==CDDS_PREPAINT)//那么需要指定重绘的具体操作。
						{
							nResult = CDRF_NOTIFYITEMDRAW;
						}
						else if(lpNmlvCustomDraw-&gt;nmcd.dwDrawStage==CDDS_ITEMPREPAINT)//重绘需要的具体操作。
						{
							nResult=CDRF_NOTIFYSUBITEMDRAW;
						}
						else if(lpNmlvCustomDraw-&gt;nmcd.dwDrawStage==(CDDS_ITEMPREPAINT | CDDS_SUBITEM))//当是要对子项进行重绘的时候。
						{
                                                        int nItem=lpNmlvCustomDraw-&gt;nmcd.dwItemSpec;
							int nSubItem=lpNmlvCustomDraw-&gt;iSubItem;//取得需要重绘的列号。//Version 4.71. Index of the subitem that is being drawn. If the main item is being drawn, this member will be zero.

							COLORREF crText;

							if ( (nItem % 3) == 0 )
							{
								crText = RGB(255,0,0);
								// 获得列绘制区域
								HDC hdc=lpNmlvCustomDraw-&gt;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);

							// Store the color back in the NMLVCUSTOMDRAW struct.
							lpNmlvCustomDraw-&gt;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&lt;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);
}

推荐答案


这篇关于关于重绘ListCtrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:18