Q281155: PRB: HDN_XXX Notifications Incorrectly Generated for CListView

Article: Q281155
Product(s): Microsoft C Compiler
Version(s): 
Operating System(s): 
Keyword(s): kbHeaderCtrl kbListView kbMFC kbDSupport kbGrpDSMFCATLkbfaq
Last Modified: 08-MAY-2002

-------------------------------------------------------------------------------
The information in this article applies to:

- The Microsoft Foundation Classes (MFC) 
-------------------------------------------------------------------------------

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.

1. Use the ClassWizard to add a handler to HDN_ENDTRACK for the
  CListView-derived view.

2. In the CListView-derived class's message map, delete the following line:

  	ON_NOTIFY_REFLECT(HDN_ENDTRACK, OnEndtrack)

3. 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.

MORE INFORMATION
================

Steps to Reproduce Behavior
---------------------------

1. Create an MFC AppWizard SDI EXE project named "App". Use the defaults. At the
  last step, change CAppView's base class to CListView.

2. Use ClassWizard to add a message handler under class CAppView for the message
  "=HDN_ENDTRACK". This will add the following handler to the message map:

  	ON_NOTIFY_REFLECT(HDN_ENDTRACK, OnEndtrack)

3. Add the following lines to CAppView::OnInitialUpdate():

  	CListCtrl& listctrl = GetListCtrl();
  	listctrl.ModifyStyle( 0, LVS_REPORT );
  	listctrl.InsertColumn ( 0, "Col1", LVCFMT_LEFT, 100 );
  	listctrl.InsertColumn ( 1, "Col2", LVCFMT_LEFT, 100 );

4. Build the project.

5. Set a break point in CAppView::OnEndtrack().

6. Run the application under the debugger.

7. Select and drag to move one of the column dividers.

Notice that the break point is not reached.

REFERENCES
==========

For additional information on the following topics, please see the MSDN Online
Library Web page listed:

  CListView
  http://msdn.microsoft.com/library/devprods/vs6/visualc/vcmfc/_mfc_clistview.htm

  WM_NOTIFY
  http://msdn.microsoft.com/library/psdk/shellcc/commctls/Common/Messages/WM_NOTIFY.htm

  ON_NOTIFY
  http://msdn.microsoft.com/library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn061.htm

  TN061: ON_NOTIFY and WM_NOTIFY Messages
  http://msdn.microsoft.com/library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn061.htm

  Header Control Reference
  http://msdn.microsoft.com/library/psdk/shellcc/commctls/Header/header_Header.htm

  Platform SDK Documentation, Header Controls
  http://msdn.microsoft.com/library/psdk/shellcc/CommCtls/Header/Header.htm

(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by S. Ganesh,
Microsoft Corporation.


Additional query words: HDN_BEGINDRAG HDN_BEGINTRACK HDN_DIVIDERDBLCLICK HDN_ENDDRAG HDN_ENDTRACK HDN_FILTERBTNCLICK HDN_FILTERCHANGE HDN_GETDISPINFO HDN_ITEMCHANGED HDN_ITEMCHANGING HDN_ITEMCLICK HDN_ITEMDBLCLICK HDN_TRACK

======================================================================
Keywords          : kbHeaderCtrl kbListView kbMFC kbDSupport kbGrpDSMFCATL kbfaq
Technology        : kbAudDeveloper kbMFC
Version           : :
Issue type        : kbprb

=============================================================================