No Message for ListView Scroll

T

Tom Bean

I have extended the ListView to detect scroll events by overriding WndProc()
and testing for the WM_VSCROLL and WM_HSCROLL events. This works fine if
the ListView is scrolled by clicking the scroll bar. If the last item,
which is partially below the bottom of the ListView, is selected by the
mouse or down-arrow, the items are scrolled up one and no WM_VSCROLL message
is sent.

How can I detect this scroll?
 
J

Jeffrey Tan[MSFT]

Hi Tom,

Thanks for your post.

Yes, I see your concern. If we use use SPY++ to view the messages of
ListView, we will see that when we select partial visible item , there will
not be any useful messages for indicating the scroll.

Because there is no such message in Win32 world, I do not think we hafve
much thing to do to resolve it. However, I have some idea may help you.

Currently, I suspect that when we click the partial visible item, the
LIstView internally calls SetScroll* APIs to change the scroll bar
position. For this issue, we may first record the original scrollbar
position information, then compare the new scrollbar position with the
original one to determine if the scrollbar position is changed. I record
the original position in OnSelectedIndexChanged event, then get the new
scroll bar position information in WM_PAINT message. Sample code like this:

public class MyListView : System.Windows.Forms.ListView
{
[StructLayout(LayoutKind.Sequential)]
public struct SCROLLBARINFO
{
public int cbSize;
public Rectangle rcScrollBar;
public int dxyLineButton;
public int xyThumbTop;
public int xyThumbBottom;
public int reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
public int[] rgstate;
}

[DllImport( "user32.dll", SetLastError=true,
EntryPoint="GetScrollBarInfo")]
private static extern int GetScrollBarInfo(IntPtr hWnd, uint idObject, ref
SCROLLBARINFO psbi);

const uint OBJID_VSCROLL=0xFFFFFFFB;
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged (e);
SCROLLBARINFO psbi = new SCROLLBARINFO();
psbi.cbSize = Marshal.SizeOf(psbi);

int nResult = GetScrollBarInfo(this.Handle, OBJID_VSCROLL, ref psbi); //
"this" is a scrollbar

if (nResult == 0)
{
int nLatError = Marshal.GetLastWin32Error(); // in kernel32.dll
}

fCheck=true;
oldval=psbi.xyThumbTop;
Console.WriteLine(psbi.xyThumbTop.ToString());
}

bool fCheck=false;
int oldval=-1;
IntPtr SB_ENDSCROLL=(IntPtr)8;
const int WM_VSCROLL=0x0115;
const int WM_PAINT=0xF;
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_PAINT)
{
SCROLLBARINFO psbi = new SCROLLBARINFO();
psbi.cbSize = Marshal.SizeOf(psbi);

int nResult = GetScrollBarInfo(this.Handle, OBJID_VSCROLL, ref psbi); //
"this" is a scrollbar

if (nResult == 0)
{
int nLatError = Marshal.GetLastWin32Error(); // in kernel32.dll
}

Console.WriteLine(psbi.xyThumbTop.ToString());
if(fCheck&&psbi.xyThumbTop!=oldval)
{
fCheck=false;
MessageBox.Show("WM_VSCROLL for select item");
}
else if(psbi.xyThumbTop==oldval)
{
fCheck=false;
}

}
if(m.Msg==WM_VSCROLL)
{
if(m.WParam==SB_ENDSCROLL)
{
fCheck=false;
MessageBox.Show("WM_VSCROLL");
}
}
base.WndProc (ref m);
}
}

This code works on my side. However, I think it is far from perfect, you
may do some more customization to it, and it may have some side effect.

Additionally, the code snippet still did not address the mouse wheel scroll
scenario, which may require more customization. But I think it should give
you some idea.

Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Tom,

Does my reply make sense to you? If you still have any concern, please feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tom Bean

Jeffery,

Yes, it makes sense but I haven't had a chance to implement it.

If I run into any problems, I will get back to you.

Thanks,
Tom
 
J

Jeffrey Tan[MSFT]

Ok, if you need further help, please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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