6

Autoscrolling on drag, part 3: Dynamic autoscroll based on mouse position

 3 years ago
source link: https://devblogs.microsoft.com/oldnewthing/20210127-00/?p=104764
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Autoscrolling on drag, part 3: Dynamic autoscroll based on mouse position

Autoscrolling on drag, part 3: Dynamic autoscroll based on mouse position

Raymond Chen

Raymond

January 27th, 2021

A common variation on the basic autoscroll algorithm is to scroll faster the further the mouse moves away from the window. You can think of the mouse as tugging on the window via an imaginary rubber band, and the further away from the window you move the mouse, the tauter the rubber band becomes, and the stronger the force applied to the scrolling.

Or you can just say that moving the mouse further from the window makes scrolling faster, and not try to imbue it with pseudo-physics.

We can make these changes to the Try­Auto­Scroll function:

BOOL TryAutoScroll(HWND hwnd, POINT pt)
{
    if (pt.y < 0) {
        ScrollDelta(hwnd, pt.y);
        return TRUE;
    } else if (pt.y > g_cyWindow) {
        ScrollDelta(hwnd, pt.y - g_cyWindow);
        return TRUE;
    }
    return FALSE;
}

Instead of scrolling by a fixed amount at each autoscroll, we instead scroll by an amount proportional to the distance the mouse is from the window. If you move far outside the window, the window scrolls by a lot, but if you’re just barely outside the window, then the window scrolls just a little.

This is a common algorithm, but it too has some downsides.

For one, the maximum autoscroll speed is dependent on how big your monitor is. Which is great if you have a massive 4K monitor, but it’s kind of unfair to people with smaller monitors.

Furthermore, the maximum autoscroll also depends on how close the window is to the edge of the screen. To get high velocity downward scrolling, you have to move the window to the top of the screen so that there’s a lot of room below the window for you to pull the invisible rubber band. This is particularly problematic when the window is maximized, because you may not have any pixels of space outside the window at all!

Next time, a different autoscroll algorithm that avoids these problems.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK