Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollBar doesn't scroll on Select()

I bring the focus on a scrollbar with vScrollBar.Select(); The scroll bar becomes focused but it doesn't scroll with mouse wheel.

It scrolls only if the mouse cursor is over the scrollbar.

How to make the scrollbar scrolling after Select() without placing the cursor over the scrollbar?

Environment: Windows 10, Windows forms, .NET 4.0

EDIT

I noticed that the scrollbar scrolls properly when I "scroll" with two fingers on the laptop touch pad, but not with the mouse wheel. Is it possible the problem to be because of a Windows 10 mouse/touch pad driver?

like image 900
Miroslav Popov Avatar asked Mar 15 '23 06:03

Miroslav Popov


1 Answers

I just got Win10 up and running, confirmed. This is a side-effect of a new Windows 10 feature, configured in Settings > Devices > Mouse & touchpad. It is named "Scroll inactive windows when I hover over them", it is turned on by default. This web page mentions it.

This is actually a very nice feature, I'll personally definitely keep it on and it is pretty likely your users will as well. Previous Windows versions sent the mouse wheel messages to the control with the focus, mystifying a great many users that got used to the way the mouse behaves in, say, a browser. Do note that the scrollbar helps, it redraws the thumb to indicate that it is no longer active when you move the mouse off the bar.

Fixing it is technically possible, you'll have to redirect the message back to the scrollbar. Nothing particularly pretty:

    public Form1() {
        InitializeComponent();
        panel1.MouseWheel += RedirectMouseWheel;
    }

    private bool redirectingWheel;

    private void RedirectMouseWheel(object sender, MouseEventArgs e) {
        if (this.ActiveControl != sender && !redirectingWheel) {
            redirectingWheel = true;
            SendMessage(this.ActiveControl.Handle, 0x020A, 
                new IntPtr(e.Delta << 16), IntPtr.Zero);
            redirectingWheel = false;
            var hmea = (HandledMouseEventArgs)e;
            hmea.Handled = true;
        }
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

But don't jump the gun yet, your users are apt to expect the Win10 behavior, eventually :)

like image 151
Hans Passant Avatar answered Apr 26 '23 16:04

Hans Passant