Microsoft KB Archive/281155

= PRB: HDN_XXX Notifications Incorrectly Generated for CListView =

Article ID: 281155

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft Foundation Class Library 4.2

-



This article was previously published under Q281155



SYMPTOMS
You use ClassWizard to add header control notification handlers such as HDN_ENDTRACK to a CListView, but the handlers do not get executed. This article provides a way to work around this problem.



CAUSE
The header control is a child window of the CListView. The header control uses WM_NOTIFY messages to send notifications to its parent, the CListView. For your code to trap HDN_XXX notification codes therefore, you must use ON_NOTIFY handlers in the CListView class. However, the ClassWizard incorrectly inserts a ON_NOTIFY_REFLECT handler instead of a ON_NOTIFY handler.



RESOLUTION
NOTE: You can use the following procedure for any HDN_XXX notification. This example uses HDN_ENDTRACK.  Use the ClassWizard to add a handler to HDN_ENDTRACK for the CListView-derived view.  In the CListView-derived class's message map, delete the following line: ON_NOTIFY_REFLECT(HDN_ENDTRACK, OnEndtrack)   Insert the following line just before the END_MESSAGE_MAP macro: ON_NOTIFY(HDN_ENDTRACK, 0, OnEndtrack) // 0 is the control ID of the Header control The prototypes/handlers that are generated by the wizard would remain the same. That is, of the form: afx_msg void OnEndtrack(NMHDR* pNMHDR, LRESULT* pResult); and void CAppView::OnEndtrack(NMHDR* pNMHDR, LRESULT* pResult) {   HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR; // TODO: Add your control notification handler code here *pResult = 0; } respectively. 



Steps to Reproduce Behavior
 Create an MFC AppWizard SDI EXE project named &quot;App&quot;. Use the defaults. At the last step, change CAppView's base class to CListView.  Use ClassWizard to add a message handler under class CAppView for the message &quot;=HDN_ENDTRACK&quot;. This will add the following handler to the message map: ON_NOTIFY_REFLECT(HDN_ENDTRACK, OnEndtrack) </li>  Add the following lines to CAppView::OnInitialUpdate: CListCtrl& listctrl = GetListCtrl; listctrl.ModifyStyle( 0, LVS_REPORT ); listctrl.InsertColumn ( 0, &quot;Col1&quot;, LVCFMT_LEFT, 100 ); listctrl.InsertColumn ( 1, &quot;Col2&quot;, LVCFMT_LEFT, 100 ); </li> Build the project.</li> Set a break point in CAppView::OnEndtrack.</li> Run the application under the debugger.</li> Select and drag to move one of the column dividers. Notice that the break point is not reached.</li></ol>

<div class="references_section">