MouseWheel Event With Picturebox?

随声附和 提交于 2020-01-02 05:06:25

问题


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:


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.




回答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.




回答3:


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
    ....
}



回答4:


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);
    }


来源:https://stackoverflow.com/questions/3399666/mousewheel-event-with-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!