如何处理NM_CUSTOMDRAW事件以检索列表项 [英] How To Handle NM_CUSTOMDRAW event to retrieve List items
问题描述
我正在处理Win32/MFC项目.我有一个自定义CListCtrl控件,我必须不时添加一些字符串.我绝对需要对动态添加到CListCtrl的项目执行一些操作.
I'm working on a win32/MFC project. I have a custom CListCtrl control that I must to add, from time to time, some strings of characters. I absolutely need to perform some manipulations on items dynamically added to my CListCtrl.
基本上,我需要:
- 检测添加单个元素;
- 检索 _单个项目_立即(理想情况下,InsertItem()调用后不久); 在地图中
- 存储单个项目的值,我将使用它们执行其他操作.
- Detect adding of single elements;
- Retrieve _single items_ IMMEDIATELY AFTER(ideally, shortly after InsertItem() invocation);
- Store values of single items in a map, which I will use to perform other manipulations.
我考虑过这样做,以覆盖方法DrawItem().但是OnDraw事件似乎不适用于我的CListCtrl.
I thought about doing this overriding the method DrawItem(). but OnDraw event seems not to be available for my CListCtrl.
永远不会生成事件.
重要提示:请注意,MyCustomCListCtrl的"固定绘制"属性设置为" True ",而"查看">"属性设置为报告 NOT .
IMPORTANT: Please note that MyCustomCListCtrl have "On Draw Fixed" property set to True, but "View" property is NOT set as a report.
因此,我决定处理NW_CUSTOMDRAW事件,编写我的CustomDraw处理程序,如此处:
So, I have decided to handle NW_CUSTOMDRAW event, writing my CustomDraw Handler, as explained here and here:
此处,您可以查看另一个代码示例.
Here you can view another code example.
然后,我需要一种从CListCtrl中检索单个itemID的方法.
更确切地说,我需要一种从NMHDR结构中获取单个商品ID的方法.
Then, I need a way to retrieve single itemIDs from my CListCtrl.
More precisely, I need a way to get single item IDs from NMHDR struct.
我该怎么做?我只能获取添加的 LAST 项目的ID.我确信这是我找不到的简单错误.
How can I do this? I am only able to obtain the ID of the LAST item that I have added. I am sure it's a simple mistake I can't find.
下面的代码示例:
包含CList Ctrl的对话框的来源:
Source of Dialog that contains CList Ctrl:
/* file MyDlg.cpp */
#include "stdafx.h"
#include "MyDlg.h"
// MyDlg dialog
IMPLEMENT_DYNAMIC(MyDlg, CDialog)
MyDlg::MyDlg(CWnd* pParent)
: CDialog(MyDlg::IDD, pParent)
{
}
MyDlg::~MyDlg()
{
}
void MyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, listView); /* listView is a MyCustomCListCtrl object */
}
BEGIN_MESSAGE_MAP(MyDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &MyDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
BOOL MyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE;
}
/* OnBnClickedButton1 handler add new strings to MyCustomCListCtrl object */
void MyDlg::OnBnClickedButton1()
{
listView.InsertItem(0, "Hello,");
listView.InsertItem(1, "World!");
}
我的自定义CList Ctrl来源:
My Custom CList Ctrl source:
/* file MyCustomCListCtrl.cpp */
#include "stdafx.h"
#include "MyCustomCListCtrl.h"
MyCustomCListCtrl::MyCustomCListCtrl()
{
}
MyCustomCListCtrl::~MyCustomCListCtrl()
{
}
BEGIN_MESSAGE_MAP(MyCustomCListCtrl, CListCtrl)
//{{AFX_MSG_MAP(MyCustomCListCtrl)
//}}AFX_MSG_MAP
// ON_WM_DRAWITEM() /* WM_DRAWITEM NON-AVAILABLE */
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
END_MESSAGE_MAP()
// 'Owner Draw Fixed' property is already TRUE
/* void CTranslatedCListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
bool inside = true; /* Member function removed: I never enter here... */
} */
void MyCustomCListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
/* Here, I must retrieve single strings added to my MyCustomCListCtrl object */
LPNMLISTVIEW plvInfo = (LPNMLISTVIEW)pNMHDR;
LVITEM lvItem;
lvItem.iItem = plvInfo->iItem; /* Here I always get _the same_ ID: ID of last element...*/
lvItem.iSubItem = plvInfo->iSubItem; // subItem not used, for now...
int MyID = 0;
this->GetItem(&lvItem); // There mai be something error here?
MyID = lvItem.iItem;
CString str = this->GetItemText(MyID, 0); /* ...due to previous error, here I will always get the last string I have added("World!") */
// Immediately after obtaining ALL IDS, I can Do My Work
*pResult = 0;
}
感谢您的帮助!
P.S.请不要给我这样的提示:
P.S. Please do not give me tips like:
- 将固定的自画"属性设置为True;
- 检查是否已插入"ON_WMDRAWITEM()"行
- 将您的CListCtrl转换为报告;
我已经尝试了所有方法...:-)
I have already tried everything... :-)
谢谢大家!
IT
推荐答案
首先...谢谢您浪费宝贵的时间解决这个愚蠢问题.我从未发现有关 LVN_INSERT
事件的任何信息.我写科学软件(大部分在Linux平台上);我不是长期的Win32开发人员,所以我不了解Win32 API.正如您所建议的,我已经修改了 MyCustomCListCtrl
类的源文件.下面的代码似乎是实现我想要的最好(更快)的方式:
first of all... Thank you wasted your precious time with this stupid question.
I never found anything about LVN_INSERT
event.
I write scientific software(most on Linux platform); I am not a long-time Win32 developer, so I don't know Win32 APIs in depth.
I have modified source file of MyCustomCListCtrl
class, as you have suggested.
Code below seems to be the best( and faster )way to achieve what I want:
/* file MyCustomCListCtrl.cpp */
#include "stdafx.h"
#include "MyCustomCListCtrl.h"
MyCustomCListCtrl::MyCustomCListCtrl()
{
}
MyCustomCListCtrl::~MyCustomCListCtrl()
{
}
BEGIN_MESSAGE_MAP(MyCustomCListCtrl, CListCtrl)
//{{AFX_MSG_MAP(MyCustomCListCtrl)
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT(LVN_INSERTITEM, OnLvnInsertItem)
END_MESSAGE_MAP()
...
afx_msg void CTranslatedListCtrl::OnLvnInsertItem(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLISTVIEW plvInfo = (LPNMLISTVIEW)pNMHDR;
CString str = this->GetItemText(plvInfo->iItem, 0);
// Add Some Logic
*pResult = 0;
}
您可以确认吗?从我所看到的,它似乎有效.:-)再次感谢!
Can You confirm? From what I can see, it seems to work. :-) Thanks again!
IT
这篇关于如何处理NM_CUSTOMDRAW事件以检索列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!