Microsoft KB Archive/281155

From BetaArchive Wiki

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.

  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:

(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: kbfaq kbheaderctrl kblistview kbprb KB281155