Forum Discussion
VSTO Excel Worksheet Scrolling Event
How about leveraging System.Windows.Forms.Timer alongside some conditions to determine when the user stops scrolling, without relying purely on timers:
- Hook into the scrolling messages to detect when scrolling starts.
- Start a System.Windows.Forms.Timer to check for the scrolling state at intervals.
- If no scrolling is detected for a specific duration, consider it as the end of scrolling.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class ScrollDetector
{
private const int WM_HSCROLL = 0x0114;
private const int WM_VSCROLL = 0x0115;
private const int WM_MOUSEWHEEL = 0x020A;
private Timer scrollTimer;
private bool isScrolling;
public ScrollDetector()
{
scrollTimer = new Timer();
scrollTimer.Interval = 100; // Check every 100ms
scrollTimer.Tick += ScrollTimer_Tick;
isScrolling = false;
// Set up your hook or message intercept here
}
private void ScrollTimer_Tick(object sender, EventArgs e)
{
if (!isScrolling)
{
scrollTimer.Stop();
OnScrollEnd();
}
isScrolling = false;
}
private void OnScrollEnd()
{
// Handle scroll end event
Console.WriteLine("Scrolling ended");
}
// Example window procedure to intercept scroll messages
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL)
{
isScrolling = true;
if (!scrollTimer.Enabled)
{
scrollTimer.Start();
}
}
base.WndProc(ref m);
}
}
Hello,
thanks very much for the replay. I did try that already but the thing is it's using timers, so it will not give an event when the user is finished scrolling. To get a 'smooth' result one needs to know when the users stops scrolling otherwise the timer has ended while the user is still scrolling, or the user is already 'long' finished scrolling, and there is the problem. Now catching the PgUP/DN/Arrow keys is not a problem but using the mousewheel or the scrollbars is. Also note that the scrolling event only starts the event when the user starts scrolling so not during scrolling (as far as I know).
Tnx Arie