I want to scroll to the end of the listview, when a new item is added. I'm using CListViewCtrl from WTL. Here is my function for adding a new item and scrolling:
void CMainDlg::addMessage(CString msg)
{
m_wndList.InsertItem ( n++, msg);
SIZE size;
size.cx = 0;
size.cy = n;
m_wndList.Scroll(size);
}
n
is just a global variable:
int n = 0;
The function works, aside from not scrolling at all.
I've looked at the source for Scroll
in wtl and it looks fine:
BOOL Scroll(SIZE size)
{
ATLASSERT(::IsWindow(m_hWnd));
return (BOOL)::SendMessage(m_hWnd, LVM_SCROLL, size.cx, size.cy);
}
So why isn't it scrolling? I've tried n+1
, etc...
Yep, you scroll by pixels. Use the EnsureVisible
function instead (it sends LVM_ENSUREVISIBLE
).