How to catch a scroll event in a CListCtrl?

rec picture rec · Sep 21, 2009 · Viewed 10.5k times · Source

I subclassed CListCtrl into my own class, and I use it in several dialogs and views. What I want to do is execute some code when the ClistCtrl is being scrolled vertically. I need this to be in the CListCtrl subclass itself.

I can detect the scrolling triggered when interacting with the scrollbar with the method provided by demoncodemonkey:

messagemap:

BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
    ON_WM_VSCROLL()
END_MESSAGE_MAP()

method declaration:

class CMyListCtrl : public CListCtrl
{
    //...
protected:
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    DECLARE_MESSAGE_MAP()
};

method implementation:

void CMyListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    //do some stuff here
    CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}

But:

  • Mousewheel scrolling does not trigger OnVScroll.

  • The automatic scrolling happening when a CListCtrl item partially visible at the bottom is clicked (it is scrolled up so the item is entirely visible) did not trigger OnVScroll either. For example:

partialy visible item

Clicking on item 9 causes the ClistCtrl to scroll a little so the item is completely visible.

Answer

demoncodemonkey picture demoncodemonkey · Sep 21, 2009

messagemap:

BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
    ON_WM_VSCROLL()
END_MESSAGE_MAP()

method declaration:

class CMyListCtrl : public CListCtrl
{
    //...
protected:
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    DECLARE_MESSAGE_MAP()
};

method implementation:

void CMyListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    //do some stuff here
    CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}