tags:

views:

310

answers:

3

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.

+2  A: 

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.

Nick Meyer
Hi, thanks for the answer. I would want to try your second solution, but i'm a bit lost as to how to proceed. Could you give me some pointers ?
raph.amiard
+3  A: 

Check the values of the NMHDR structure.
http://msdn.microsoft.com/en-us/library/bb775514%28VS.85%29.aspx

Kyle Alons
Hi, thanks. I did, check the edit.
raph.amiard
So hwndFrom is not the hwnd of the list control or its GetHeaderCtrl()?
Kyle Alons
Hey Alons, yeah it is , i just figured that out .. It's not the one of the list control, but i didn't figure out until late that maybe there was a method to get the header. Sorry for being a little slow, i'm still learning the MFC way, and it's kind of hard on me. Thanks !
raph.amiard
A: 

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

raph.amiard