LLucasAlday
LLucasAlday

Reputation: 2551

How to catch a scroll event in a CListCtrl?

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:

partialy visible item

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

Upvotes: 1

Views: 11623

Answers (3)

c00000fd
c00000fd

Reputation: 22265

A much better solution is to use LVN_BEGINSCROLL or LVN_ENDSCROLL notifications that are sent to the parent window. (They also account for mouse-wheel scrolling.)

Although that still doesn't solve the scrolling that occurs when a user moves the focused list item up and down using keyboard.

Upvotes: 0

Hokum
Hokum

Reputation: 41

Mousewheel scrolling trigger OnMouseWheel.

Upvotes: 3

demoncodemonkey
demoncodemonkey

Reputation: 11957

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);
}

Upvotes: 5

Related Questions