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 a window procedure that intercepts scroll messages like msg = WM_HSCROLL OrElse msg = WM_VSCROLL OrElse msg = WM_MOUSEWHEEL etc.
This works to get an event when the user starts scrolling (so not when the user finishes). So I need to add a timer and guess when the user finishes scrolling. Now I personally don't like using timers for things like this. Any Idea's?
If required i can give you the code for detection of the start of the scrolling. I am using
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); } }
- MyExcelDeveloperCopper Contributor
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
- jaymcc510Iron Contributor
Awesome