Forum Discussion
MyExcelDeveloper
Feb 28, 2025Copper Contributor
VSTO Excel Worksheet Scrolling Event
Hello all, I am trying to get an event from Excel when a user is finished scrolling using the scrollbars, the mousewheel etc. Now I could not find any Excel events, so i tried using API's. Basically...
Kidd_Ip
Mar 02, 2025MVP
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);
}
}
jaymcc510
Mar 04, 2025Iron Contributor
Awesome