Scroll bars in treeview - how can you get onscroll as well as theirposition?

B

Benny Raymond

I've looked around msdn for about an hour now and can't figure out how
to place an event when the user scrolls, or even how to get the value of
where the scroll bar is actually placed...

Anyone know how to use this event, or how to get the scroll bar info?

~Benny
 
G

Guest

Hi Benny

I have the same problem that u faced. And I see that there are no replies
with solutions. If you have found the solution please let me know how to go
about it.

Thanks and Regards,
Manu
 
L

Ludovic SOEUR

Hi Benny and Manu,

I wrote this class to add the scroll event and 2 getters to have the
horizontal and vertical position of the scrollbars.
Assume your Treeview is named myTreeView ( TreeViewScroll myTreeView) :
To get the horizontal position, use myTreeView.HScrollPos (be careful of
the unit. I think it's in pixels)
To get the vertical position use myTreeView.VScrollPos (be careful of the
unit. I think it's in lines)
To add a listener for the scrollbars use myTreeView.Scroll+=new
ScrollEventHandler(myTreeView_Scroll);
This event is almost the same as the usual scroll event for a control. The
only difference is that the newValue is not set when event like
small(de)increment or large(de)increment occurs. If you want so, use the
getters and the scrolltype to determine the newValue or calculate it in the
WndProc. For events like thumbposition or thumbtrack, everything is
correctly filled.

Here is the source code :

using System;
using System.Windows.Forms;
public class TreeViewScroll:TreeView {
private const int SB_HORZ=0;
private const int SB_VERT=1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetScrollPos(int hWnd,int nBar);
public int HScrollPos {
get {return GetScrollPos((int)Handle,SB_VERT);}
}
public int VScrollPos {
get {return GetScrollPos((int)Handle,SB_HORZ);}
}
public event ScrollEventHandler Scroll;
protected override void WndProc(ref Message m) {
if (Scroll!=null) {
switch(m.Msg) {
case KERMLSoftware.Helpers.Win32.WinMsg.WM_VSCROLL: {
ScrollEventType
t=(ScrollEventType)Enum.Parse(typeof(ScrollEventType),
(m.WParam.ToInt32()&65535).ToString());
Scroll(m.HWnd,new
ScrollEventArgs(t,((int)(m.WParam.ToInt64()>>16))&255));
} break;
}
}
base.WndProc (ref m);
}
}

Hope it helps,

Ludovic SOEUR.
 
L

Ludovic SOEUR

I have forgotten to replace all my strings. Instead of
case KERMLSoftware.Helpers.Win32.WinMsg.WM_VSCROLL : {,
use case WM_VSCROLL:case WM_HSCROLL: {
with theses consts :
private const int WM_VSCROLL=0x0115
private const int WM_HSCROLL=0x0114;

Sorry for the mistake.

Hope it helps,

Ludovic SOEUR.

Ludovic SOEUR said:
Hi Benny and Manu,

I wrote this class to add the scroll event and 2 getters to have the
horizontal and vertical position of the scrollbars.
Assume your Treeview is named myTreeView ( TreeViewScroll myTreeView) :
To get the horizontal position, use myTreeView.HScrollPos (be careful of
the unit. I think it's in pixels)
To get the vertical position use myTreeView.VScrollPos (be careful of the
unit. I think it's in lines)
To add a listener for the scrollbars use myTreeView.Scroll+=new
ScrollEventHandler(myTreeView_Scroll);
This event is almost the same as the usual scroll event for a control. The
only difference is that the newValue is not set when event like
small(de)increment or large(de)increment occurs. If you want so, use the
getters and the scrolltype to determine the newValue or calculate it in the
WndProc. For events like thumbposition or thumbtrack, everything is
correctly filled.

Here is the source code :

using System;
using System.Windows.Forms;
public class TreeViewScroll:TreeView {
private const int SB_HORZ=0;
private const int SB_VERT=1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetScrollPos(int hWnd,int nBar);
public int HScrollPos {
get {return GetScrollPos((int)Handle,SB_VERT);}
}
public int VScrollPos {
get {return GetScrollPos((int)Handle,SB_HORZ);}
}
public event ScrollEventHandler Scroll;
protected override void WndProc(ref Message m) {
if (Scroll!=null) {
switch(m.Msg) {
case KERMLSoftware.Helpers.Win32.WinMsg.WM_VSCROLL: {
ScrollEventType
t=(ScrollEventType)Enum.Parse(typeof(ScrollEventType),
(m.WParam.ToInt32()&65535).ToString());
Scroll(m.HWnd,new
ScrollEventArgs(t,((int)(m.WParam.ToInt64()>>16))&255));
} break;
}
}
base.WndProc (ref m);
}
}

Hope it helps,

Ludovic SOEUR.


Manu said:
Hi Benny

I have the same problem that u faced. And I see that there are no replies
with solutions. If you have found the solution please let me know how to go
about it.

Thanks and Regards,
Manu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top