C++/MFC:处理多个 CListCtrl 标头 HDN_ITEMCLICK 事件

发布于 2024-08-27 19:11:20 字数 561 浏览 4 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞美少年 2024-09-03 19:11:20

好吧,我找到了一个解决方案,虽然我发现它有点脏,但它有效,所以我将其发布以供将来参考。

可以通过CListCtrl的GetHeaderCtrl成员函数来获取Header。然后您可以通过 m_hWnd 获取它的处理程序。因此,您所要做的就是测试该处理程序是否与 NMHDR 结构中的处理程序相同,因此代码如下所示:

void Ccreationprogramme::OnHdnItemclickList5(NMHDR *pNMHDR, LRESULT *pResult)
{  
  if (pNMHDR->hwndFrom == LC_gen_schedules.GetHeaderCtrl()->mhWnd)
  {
    // Code goes here
  }
    *pResult = 0;
}

感谢大家的帮助

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 :

void Ccreationprogramme::OnHdnItemclickList5(NMHDR *pNMHDR, LRESULT *pResult)
{  
  if (pNMHDR->hwndFrom == LC_gen_schedules.GetHeaderCtrl()->mhWnd)
  {
    // Code goes here
  }
    *pResult = 0;
}

Thanks all for the help

锦欢 2024-09-03 19:11:20

传递给消息处理程序的 LPARAM 实际上是指向 NMHEADER 结构的指针,该结构包含一个 NMHDR 结构,而该结构又包含>HWND 和发送消息的控件的控件 ID。您可以将其与列表控件的 HWND 进行比较,以确定单击了哪个窗口的标题控件。

或者,您可以从 CListCtrl 派生一个类,并将 HDN_ITEMCLICK 消息反射回列表控件。这样,每个列表控件对象都会处理自己的标头通知。

The LPARAM passed to your message handler is actually a pointer to an NMHEADER structure, which contains an NMHDR structure, which in turn contains the HWND and control ID of the control which sent the message. You may be able to compare that to your list controls' HWNDs 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文