Thursday 4 June 2009

TreeViewScroll

The .net TreeView doesn't expose a scroll event. Make your own by overriding DefWndProc.

   public class TreeViewScroll : TreeView
{
private const int WM_VSCROLL = 0x115;
private IntPtr SB_ENDSCROLL = new IntPtr(8);

public event MethodInvoker Scroll;

protected void OnScroll()
{
if (Scroll != null)
Scroll();
}

protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_VSCROLL)
{
if (m.WParam != SB_ENDSCROLL)
OnScroll();
}

base.DefWndProc(ref m);
}
}


The code above is sufficient for my needs, but if you need more details about the scroll (direction, where you scrolled to, etc) you need to interrogate the message further.

This link should tell you what the message contains:
http://msdn.microsoft.com/en-us/library/bb787577(VS.85).aspx

This link can tell you the scrollbar values:
http://www.pinvoke.net/default.aspx/Enums/ScrollBarCommands.html

No comments: