7

I want to hover over a picturebox (or all pics and the main form) and use the mousewheel to scroll. However i have no luck. I wrote pictureBox1.MouseWheel and check the delta. I set a breakpoint for when it != 0. So far no matter what i did i couldnt get anything to happen. I tried mousemove as well and that didnt work. However breaking on the if statement works. I just can never get the wheel to work.

How do i make picturebox (or any control in the form) call a mousewheel event?

-edit- nevermind. I added the event to the object that has the event most of the time. It works pretty well. I am not sure why i didnt think of this before i wrote this question. I am still open to mouse over + wheel solutions.

1
  • 1
    I know this question is old. But this has been changed for Windows 10. In Windows 10, all of the controls the mouse is hovering on receives mouse wheel event.
    – LxL
    Jun 16, 2016 at 17:49

5 Answers 5

13

Windows doesn't send the mouse scroll message to the control that's hovered, it goes to the control with the focus. You already know how to fix the focus.

This behavior is getting unintuitive because of the way browsers and Office programs work. You'll find code to alter this in my answer in this thread. Beware that it works on any window in your app. You'll have to add filtering on the handle value if that's undesirable.


UPDATE: this behavior was changed in Win10. It has a new system setting named "Scroll inactive windows when I hover over them", turned on by default. So focus no longer matters and it now works very similar to way it behaves in a browser. Testing your app is important, you can see what happens on an older version of Windows by temporarily disabling the system option.

1
  • Excellent answer. You really helped me with all my GUI needs.
    – user34537
    Aug 3, 2010 at 21:20
2

This answer explains how to do it. In short, create a MouseEnter event on the picture-box that just focuses the picture-box. Then the picture-box will receive MouseWheel events just fine.

1
  • You should never take away the keyboard focus from the user because the mouse was moved! This is really a very very ugly workaround. Do not do this! Apart from that it does not work for me. Even if I set the focus to the pictureBox I do not get the wheel event.
    – Elmue
    May 20, 2015 at 17:49
1

The answers here are not working for me. I have the picturebox in a scrollable Pane and there is little more work to do for a correct functioning.

What you have to do is overwrite the OnMouseWheel() function in the Form. There you get the wheel event and you must check if the mouse is inside the picturebox. But that is not enough. Imagine you are displaying an image of 5000 x 5000 pixels inside a scrollable pane that shows only a little part of the image. Then you must also check if the mouse is over the Pane and all it's parents. The code below works independent of the scroll position of the scrollbars of any of the parent controls of the pictureBox.

/// <summary>
/// This must be overridden in the Form because the pictureBox never receives MouseWheel messages
/// </summary>
protected override void OnMouseWheel(MouseEventArgs e)
{
    // Do not use MouseEventArgs.X, Y because they are relative!
    Point pt_MouseAbs = Control.MousePosition; 
    Control i_Ctrl = pictureBox;
    do
    {
        Rectangle r_Ctrl = i_Ctrl.RectangleToScreen(i_Ctrl.ClientRectangle);
        if (!r_Ctrl.Contains(pt_MouseAbs))
        {
            base.OnMouseWheel(e);
            return; // mouse position is outside the picturebox or it's parents
        }
        i_Ctrl = i_Ctrl.Parent;
    }
    while (i_Ctrl != null && i_Ctrl != this);

    // here you have the mouse position relative to the pictureBox if you need it
    Point pt_MouseRel = pictureBox.PointToClient(pt_MouseAbs);

    // Do your work here
    ....
}
0

Just override Form's MouseWheel and check if e.X and e.Y are inside the location area of the PictureBox

  protected override void OnMouseWheel(MouseEventArgs e)
    {
        if (e.X >= soundGraph.Location.X && e.X <= soundGraph.Location.X + soundGraph.Width
            &&
            e.Y >= soundGraph.Location.Y && e.Y <= soundGraph.Location.Y + soundGraph.Height)
        { // do what you have to
        }
        base.OnMouseWheel(e);
    }
0

This works also if the picturebox is not at the 0,0 position in the form.

' =====================================================================================
'  Windows10 has a new system setting 
'   named "Scroll inactive windows when I hover over them", turned on by default.
' -------------------------------------------------------------------------------------
'  The following correction does the same for Windows 8 / 7 / XP
' =====================================================================================
Protected Overrides Sub OnMouseWheel(ByVal e As MouseEventArgs)
    Dim p As Point
    p = pbox_Graph.PointToClient(Me.PointToScreen(e.Location))
    If pbox_Graph.ClientRectangle.Contains(p) Then
        pbox_TimeGraph_MouseWheel(Me, e)
    Else
        MyBase.OnMouseWheel(e)
    End If
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.