C++/MFC:处理多个 CListCtrl 标头 HDN_ITEMCLICK 事件
我正在编写一个 MFC 应用程序,其中有一个在报表视图中包含多个 CListCtrl 的对话框。我希望其中之一可以排序。 所以我处理了 HDM_ITEMCLICK 事件,一切都工作得很好。除了如果我单击另一个 CListCtrl 的标题,它确实会对其他 CListCtrl 进行排序,这看起来确实有点愚蠢。
这显然是由于标头的 ID 为 0,这使得消息映射中的条目看起来像这样:
ON_NOTIFY(HDN_ITEMCLICK, 0, &Ccreationprogramme::OnHdnItemclickList5)
但是由于所有标头的 id 都为零,显然对话框的每个标头都会发送消息。
有一个简单的方法可以解决这个问题吗?
编辑:也许我不清楚,但我确实检查了 NMHDR 结构内的值。 HwndFrom 指针根据单击的标头而有所不同,这对我没有多大帮助,因为它的值在每个运行时明显不同。 idFrom 值为 0,这正是我上面解释的原因,因为这是每个标头的 id。谢谢
EDIT2: hwnd 指针值也不对应于 CListCtrl,可能是因为它完全来自不同的对象。
I'm coding an MFC application in which i have a dialog box with multiple CListCtrls in report view. I want one of them to be sortable.
So i handled the HDM_ITEMCLICK event, and everything works just fine .. Except that if i click on the headers of another CListCtrl, it does sort the OTHER CListCtrl, which does look kind of dumb.
This is apparently due to the fact that headers have an ID of 0, which make the entry in the message map look like this :
ON_NOTIFY(HDN_ITEMCLICK, 0, &Ccreationprogramme::OnHdnItemclickList5)
But since all the headers have an id of zero, apparently every header of my dialog sends the message.
Is there an easy way around this problem ?
EDIT: Maybe i wasn't clear, but i did check the values inside the NMHDR structure. The HwndFrom pointer is different depending on which header is clicked, which doesn't help me a lot since it's value is obviously different at each runtime. The idFrom value is 0, for the very reasons i explained above, because that's the id of every header. Thanks
EDIT2: The hwnd pointer values do also not correspond to the CListCtrl, probably because it's coming from a different object entirely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查 NMHDR 结构的值。
http://msdn.microsoft.com/en-我们/库/bb775514%28VS.85%29.aspx
Check the values of the NMHDR structure.
http://msdn.microsoft.com/en-us/library/bb775514%28VS.85%29.aspx
好吧,我找到了一个解决方案,虽然我发现它有点脏,但它有效,所以我将其发布以供将来参考。
可以通过CListCtrl的GetHeaderCtrl成员函数来获取Header。然后您可以通过 m_hWnd 获取它的处理程序。因此,您所要做的就是测试该处理程序是否与 NMHDR 结构中的处理程序相同,因此代码如下所示:
感谢大家的帮助
Ok i found a solution, though i find it a bit dirty but it works, so i'll post it for future reference.
You can get the Header through the GetHeaderCtrl member function of CListCtrl. You can then get it's handler thru m_hWnd. So all you got to do is to test if that handler is the same as the one in the NMHDR structure, so the code looks like this :
Thanks all for the help
传递给消息处理程序的 LPARAM 实际上是指向 NMHEADER 结构的指针,该结构包含一个 NMHDR 结构,而该结构又包含>HWND 和发送消息的控件的控件 ID。您可以将其与列表控件的
HWND
进行比较,以确定单击了哪个窗口的标题控件。或者,您可以从 CListCtrl 派生一个类,并将 HDN_ITEMCLICK 消息反射回列表控件。这样,每个列表控件对象都会处理自己的标头通知。
The
LPARAM
passed to your message handler is actually a pointer to anNMHEADER
structure, which contains anNMHDR
structure, which in turn contains theHWND
and control ID of the control which sent the message. You may be able to compare that to your list controls'HWND
s to determine which window's header control was clicked.Alternatively, you could derive a class from CListCtrl and reflect the HDN_ITEMCLICK messages back to the list control. That way, each list control object handles its own header's notifications.