100分求助,关于List-View

jlu3389 2008-02-29 11:29:29
我准备做一个简单的图片浏览器。
创建MFC选择的是对话框,在Foram中放了一个ListCtrl,设置为ICON的style,并添加了NM_CUSTOMDRAW这个事件的响应函数。

可我在ListCtrl中添加一个Item后,无法在NM_CUSTOMDRAW这个函数中的LPNMCUSTOMDRAW结构体中获得这个item的rect。
用GetItemRect得到也不对。

不知道那位大侠做过类似这样的东东,指导小弟一下。谢谢。
解决马上给分。
...全文
231 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
analysefirst 2008-02-29
  • 打赏
  • 举报
回复
2. Adding items to the list control

Adding items to the list control is quite simple if we are not writing a time-critical application. If there is a great number of thumbnails to load, the user will see just a moving scroll bar flashing on the screen while the images are being loaded from disk. Instead we can use a simple thread mechanism to add the list items while another thread just loads the images, and all this while the user continues to interact with the application.

The member function that loads items into the list control will be something like:
Collapse

// structure used to pass parameters to a image adder thread

struct threadParamImage
{
CString folder; // the folder to be scanned for thumbnails

CThumbnailView* pView; // the view that accomodates them

HANDLE readPipe; // the pipe used to pass thumbnail filenames to the

// JPEG loader thread

};

// structure used to pass parameters to a JPEG image loader thread

struct threadParam
{
CThumbnailView* pView; // the view that shows thumbnails

HANDLE readPipe; // the pipe used to pass thumbnail filenames to the

// JPEG loader thread

};

HANDLE hThread = NULL; // handle to the JPEG image loader thread

HANDLE readPipe = NULL; // read and write ends of the communication pipe

HANDLE writePipe = NULL;
HANDLE skipImages = NULL; // handle to the semaphore that signals the pipe

//does no longer hold consistent data

HANDLE imageFiller = NULL; // handle to the thumbnail adder thread

HANDLE imageFillerSemaphore = NULL; // thread termination flag (when this semaphore

// goes signaled, the thread must exit)

HANDLE imageFillerCR = NULL; // thumbnail adder thread critical section semaphore

HANDLE imageFillerWait = NULL; // second thumbnail adder thread critical section semaphore


// Fill in list control with thumbails from a specified folder

BOOL CThumbnailView::FillInImages(CString folder)
{
// create semaphores the first time only

if (!imageFillerSemaphore)
{
imageFillerSemaphore=CreateSemaphore(NULL, 0,1, NULL);
imageFillerCR=CreateSemaphore(NULL, 1,1, NULL);
imageFillerWait=CreateSemaphore(NULL, 1,1, NULL);
}

// critical region starts here

WaitForSingleObject(imageFillerCR, 0);

// create thread parameters

threadParamImage* pParam=new threadParamImage;
pParam->folder=folder;
pParam->pView=this;

// and the thread

DWORD dummy;
imageFiller=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ImageFillerThread,
pParam, 0, &dummy);

return TRUE;
}

While the image filler thread is:
Collapse

// thumbnail adder thread

DWORD ImageFillerThread(DWORD param)
{
// get thread parameters

threadParamImage* pParam=(threadParamImage*)param;
CString folder=pParam->folder;
CThumbnailView* pView=pParam->pView;
HANDLE readPipe=pParam->readPipe;
// cleanup

delete pParam;

// wait for previous copies to stop

WaitForSingleObject(imageFillerWait, INFINITE);

// clear previous images from list control

pView->GetListCtrl().DeleteAllItems();

// start scanning designated folder for thumbnails

WIN32_FIND_DATA fd;
HANDLE find;
BOOL ok=TRUE;
fd.dwFileAttributes=FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_COMPRESSED|
FILE_ATTRIBUTE_READONLY;
find=FindFirstFile(folder+"\\_thumbs\\*.*", &fd);

// return if operation failed

if (find==INVALID_HANDLE_VALUE)
{
ReleaseSemaphore(imageFillerWait, 1, NULL);
ExitThread(0);
return 0;
}

// critical section ends here

ReleaseSemaphore(imageFillerCR, 1, NULL);

// start adding items to the list control

do
{
if (WaitForSingleObject(imageFillerSemaphore, 0)==WAIT_OBJECT_0)
{
// thread is signaled to stop

// signal skip to JPEG file loader

int skip=-1;
DWORD dummy;
WriteFile(writePipe, &skip, sizeof(int), &dummy, NULL);
ReleaseSemaphore(skipImages, 1, NULL);
break;
}

ok=FindNextFile(find, &fd);
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)continue;

if (ok)
{
int item=pView->GetListCtrl().InsertItem(pView->GetListCtrl().GetItemCount(),
fd.cFileName, 0);
pView->GetListCtrl().SetItemPosition(item, CPoint(105*item, 5));
pView->AddImage(CString(folder+"\\_thumbs\\")+fd.cFileName, item);
}
}
while (find&&ok);

// done adding items
FindClose(find);
ReleaseSemaphore(imageFillerWait, 1, NULL);

ExitThread(0);
return 0;
}

The last but not the least is the JPEG image loader thread:
Collapse

// JPEG image loader thread

DWORD ImageLoaderThread(DWORD param)
{
// wait to get a filename, then build the image, add it to the image list

// then update list control

CThumbnailView* pView=(CThumbnailView*)param;
DWORD dummy;
char buffer[1024];

while(1)
{
int itemIndex;
int size;

if (WaitForSingleObject(skipImages, 0)==WAIT_OBJECT_0)
{
// skip to marker

do
{
ReadFile(readPipe, &itemIndex, sizeof(int), &dummy,
NULL);
if(itemIndex==-1)break;
ReadFile(readPipe, &size, sizeof(int), &dummy, NULL);
ReadFile(readPipe, buffer, size, &dummy, NULL);
}
while (1);
}

// get data from pipe

ReadFile(readPipe, &itemIndex, sizeof(int), &dummy, NULL);
ReadFile(readPipe, &size, sizeof(int), &dummy, NULL);
ReadFile(readPipe, buffer, size, &dummy, NULL);
buffer[size]=0;

// is the file name valid ?

OFSTRUCT ofs;
if(OpenFile(buffer, &ofs, OF_EXIST)==HFILE_ERROR)continue;

// load an image from disk (using PaintLib)

CWinBmp bitmap;
CAnyPicDecoder decoder;
try
{
decoder.MakeBmpFromFile(buffer, &bitmap, 0);
}
catch (CTextException exc)
{
pView->GetListCtrl().SetItem(itemIndex, 0, LVIF_IMAGE, NULL,
1, 0, 0, 0);
continue;
}

// create a CBitmap object from the data within the CWinBmp object

BITMAPINFOHEADER& bmiHeader=*bitmap.GetBMI();
BITMAPINFO& bmInfo=*(BITMAPINFO*)bitmap.GetBMI();
LPVOID lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
CClientDC dc(NULL);
HBITMAP hBmp=CreateDIBitmap(dc.m_hDC, &bmiHeader, CBM_INIT, lpDIBBits,
&bmInfo, DIB_RGB_COLORS);

CBitmap bmp;
bmp.Attach(hBmp);
// add the thumbnail to the image list

int imgPos=pView->m_imageList.Add(&bmp, RGB(0, 0, 0));
pView->GetListCtrl().SetItem(itemIndex, 0, LVIF_IMAGE, NULL, imgPos,
0, 0, 0);
}

ExitThread(1);
return 0;
}

The code uses SDK semaphores, pipes and threads because they are easier to handle and much straightforward than MFC threads and synchronization mechanisms.

The code is quite easy to follow and change to meet your needs, but if you need assistance, contact me. Also please send me bugs or updates, to keep this solution up-to-date. For more details on the sample application, contact me.
analysefirst 2008-02-29
  • 打赏
  • 举报
回复
下面是复制过来的内容,以防哪一天没有了


The solution presented in this article uses a JPEG reader class to read JPEG images and display them into a CListCtrl. The idea is to create an image list that holds the icons created from the JPEG thumbails.

The first problem arises with the fact that MFC class CImageList does not support higher color depths than 16 colors (4 bits per pixel). Another interesting issue is that image loading takes quite some time. This article addresses both these issues.
1. Creating an image list with higher color depth.

The less-known SDK macro ImageList_Create meets this problem.

// Create the image list with 100*100 icons and 32 bpp color depth

HIMAGELIST hImageList=ImageList_Create(100, 100, ILC_COLOR32, 0, 10);
m_imageList.Attach(hImageList);

// load the starting bitmap ("Loading..." and "Corrupt file")

CBitmap dummy;
dummy.LoadBitmap(IDB_NAILS100);
m_imageList.Add(&dummy, RGB(0, 0, 0));

// Use the image list in the list view

GetListCtrl().SetImageList(&m_imageList, LVSIL_NORMAL);
GetListCtrl().SetImageList(&m_imageList, LVSIL_SMALL);

analysefirst 2008-02-29
  • 打赏
  • 举报
回复
网上有现在开源的

一步步教你如何做的
http://www.codeproject.com/KB/combobox/thumbnailview.aspx
datoucaicai 2008-02-29
  • 打赏
  • 举报
回复
同楼上所说NM_CUSTOMDRAW是有不同阶段的

并且每个item都会draw

你需要判断哪个item是你新添加的,然后再通过LPNMCUSTOMDRAW结构体获得这个item的rect
jameshooo 2008-02-29
  • 打赏
  • 举报
回复
应该使用OwnerDraw,而不是CustomDraw,个人觉得CustomDraw用处不大,也很少被使用,因为控制有点麻烦。
菜牛 2008-02-29
  • 打赏
  • 举报
回复
NM_CUSTOMDRAW有不同阶段的,建议你看一下NM_CUSTOMDRAW的示例(MSDN上)
jlu3389 2008-02-29
  • 打赏
  • 举报
回复
恩,我看到MSDN上的描述了,只有在report情况下才可以。

不转ICON不可以吗?

我的打算用CustomDraw。
可我不知道怎么得到当前新添加ITEM的ID?

jameshooo 2008-02-29
  • 打赏
  • 举报
回复
晕,icon模式无法自绘,因为必须传递icon给窗口,窗口自己完成绘制,你把你的图片转换成icon提供给窗口就可以了。
jlu3389 2008-02-29
  • 打赏
  • 举报
回复
不是,是ICON的。
因为我需要在上边画我自己的图片。

我在SDK下尝试也收不到这个消息。
jameshooo 2008-02-29
  • 打赏
  • 举报
回复
就是这个style。你的listview是report模式吗?
jlu3389 2008-02-29
  • 打赏
  • 举报
回复
jameshooo:
在VS2005中,没有这个style。只有一个Owner Draw fixed。
选定后添加WM_DRAWITEM,可收不到这个消息啊
jameshooo 2008-02-29
  • 打赏
  • 举报
回复
OwnerDraw不是通知消息,而是窗口style,自绘窗口需要这个style。设置这个style后,你能在WM_DRAWITEM消息中绘制,能获得所有需要的item信息。
jlu3389 2008-02-29
  • 打赏
  • 举报
回复
感谢各位。

OwnerDraw?
我在MSDN中没有找到这个通知,还有,我不打算使用ImageList来添加图片。我就想得到新添加那个Item的ICON的大小,就是那个Rect。
内容概要:本文提出了一种基于改进扩散模型的高海拔地区新能源高波动出力场景生成方法,并提供了完整的Python代码实现。该方法针对高海拔地区风能、光伏等新能源出力波动剧烈、不确定性高的特点,通过优化扩散模型的结构与训练策略,有效捕捉历史数据的概率布特征与时序相关性,从而生成高质量、多样化的出力场景。文中详细阐述了模型的数学推导、网络架构设计、损失函数优化及采样算法改进,并通过实验证明其在拟合精度、场景多样性与稳定性方面优于传统生成模型,为电力系统在高比例新能源接入下的规划、调度与风险评估提供了可靠的场景输入支持。; 适合人群:具备一定Python编程能力和机器学习基础,从事新能源发电预测、电力系统析、智能优化、场景生成等方向研究的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①用于高海拔地区风电、光伏出力的不确定性建模与多场景生成;②支撑含高渗透率新能源的电力系统随机优化调度、鲁棒决策与风险评估;③为相关学术研究、论文复现与算法改进提供可运行的技术方案与代码基础; 阅读建议:建议读者结合所提供的完整资源(代码、数据集、说明文档)进行实践操作,重点关注扩散模型的前向加噪与反向去噪过程的设计细节,以及如何将其适配于新能源时序数据的生成任务,通过参数调优与对比实验深入理解模型的生成机制与性能边界。
内容概要:本文围绕基于静态约束法的配电网电动汽车接入容量评估展开研究,提出了一种在新型电力系统背景下评估主动配电网对电动汽车承载能力的方法。研究通过构建数学模型,结合潮流计算与关键约束条件(如电压越限、线路过载等),量化析配电网可承受的最大电动汽车充电负荷容量,旨在识别规模化电动汽车接入带来的潜在运行风险,并为电网规划与运行提供科学依据。文中配套提供了完整的Matlab代码实现,便于仿真验证与结果复现。此外,该研究与布式光伏承载力评估、电动汽车可调能力析等方向形成技术联动,展现了多主题协同的研究体系。; 适合人群:具备电力系统析基础理论知识及Matlab编程能力的高校研究生、科研机构研究人员,以及从事新能源并网、智能配电网规划与运行等相关领域的工程技术人员。; 使用场景及目标:①用于学术研究中的模型复现与论文撰写支撑;②评估实际配电网中电动汽车大规模接入的可行性与安全边界,指导充电基础设施布局;③作为高校教学案例,帮助学生深入理解电网承载力评估的核心原理、建模方法与仿真技术; 阅读建议:建议结合文中提及的相关研究方向(如二阶锥规划、多面体聚合方法等)进行对比学习,充利用所提供的Matlab代码与网盘资料开展仿真实验,重点关注约束条件的设定逻辑与潮流计算模块的实现细节,以深化对评估模型机理与工程应用价值的理解。
内容概要:本文围绕“考虑隐私保护的布式联邦学习电力负荷预测研究”展开,提出了一种基于Python实现的联邦学习框架,旨在解决居民或行业电力负荷预测中用户电表数据隐私泄露的风险。该研究通过构建布式机器学习模型,使各参与方在不共享原始数据的前提下协同训练全局模型,有效实现了数据“可用不可见”。文中详细阐述了联邦学习的整体架构设计、本地模型训练流程、参数加密传输与安全聚合机制,并结合差隐私等技术进一步增强系统的隐私保护能力。同时,研究利用真实电力负荷数据集进行了实验验证,展示了方法在预测精度与隐私保障之间的良好平衡,并提供了完整的代码实例与复现指南,便于后续研究与应用拓展。; 适合人群:具备一定机器学习基础和电力系统背景知识,从事智慧能源、隐私计算或人工智能相关方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 实现跨区域、跨主体的电力负荷协同预测,打破数据孤岛;② 在确保用户用电数据隐私安全的前提下提升负荷预测准确性;③ 推动联邦学习在智能电网、需求响应、虚拟电厂等场景中的实际部署与应用。; 阅读建议:建议结合文中提供的Python代码与网盘资料进行动手实践,重点关注联邦学习的通信轮次设计、模型聚合算法(如FedAvg)的实现细节以及差隐私噪声添加策略,深入理解其对模型性能与隐私强度的影响,为进一步优化与创新奠定基础。
VDA_Band_19.1_3rd edition_2026 English Inspection of Technical Cleanliness 内容概要:本文档为德国汽车工业协会(VDA)发布的第三版《技术清洁度检验:功能相关汽车部件的颗粒污染检测》(VDA 19.1),系统规范了汽车行业中零部件技术清洁度的检测方法与流程。文件涵盖从取样、提取、过滤到析的全流程标准化操作,重点更新了干法提取(如 Stamp Test 和刷吸法)、小于50µm颗粒的检测、光学子系统和SEM/EDX标准析方法,并引入统一材料类体系以提升结果可比性。同时明确了“标准析”与“自由检验”的区别,前者用于高兼容性检测,后者允许客户与供应商协商定制参数。文档还强化了对非可测组件的技术清洁保障、测量不确定度评估及方法验证的要求,并提供了多个实际案例支持应用落地。; 适合人群:适用于汽车制造业中从事质量控制、工艺开发、供应商管理及相关检测实验室的技术人员和管理人员,尤其适合具备一定质量管理或洁净度检测基础的专业人员。; 使用场景及目标:①用于制定和执行零部件清洁度检测标准;②指导 incoming/outgoing 检验及生产过程监控;③支持失效析与质量改进项目;④作为企业内部审核和技术交流的依据; 阅读建议:建议结合VDA 19.2及其他相关标准配套使用,重点关注各章节中的起始参数设定、方法选择逻辑及附录中的检查表示例,在实际操作中同步开展方法验证与人员培训,确保检测结果的有效性和可追溯性。

15,976

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧