Changing mouse scroll and zoom behaviour.

Started by fjutoslaw, February 03, 2017, 07:13:06 PM

Previous topic - Next topic

fjutoslaw

Hi,
I would like to change behaviour of image box and I am not sure how to do that. I would like to change so the mouse scroll will not zoom in but instead scroll the image down. To zoom user will use ctrl + scroll ( just like in browser). Any clues on how to achieve this would be appreciated. Thanks for the awesome control, it saved me a lot of work. cheers. Greg.

Richard Moss

Hello,

Welcome to the forums and thanks for the question. This question was actual asked on GitHub a few months ago and I posted a solution there. I've added the same solution below, let me know if this works for you.

QuoteHello, and thanks for the question. I took a quick look at this, and found it reasonable easy to do by overriding OnMouseWheel. There is a caveat though - the MouseWheel event will no longer be raised unless Control is held.

You can find my implementation below - I check to see if Control is pressed and if so, use the default implementation. If it isn't pressed, I then check to see if a vertical scrollbar is present, and if so get the current AutoScrollPosition property value, offset it by the wheel delta (making sure it doesn't fall out of the boundaries of the scrollbar), and then re-apply it. Seems to work fine - you could adapt the code to scroll horizontally instead of vertically, or to scroll horizontally if a vertical scrollbar isn't present.

protected override void OnMouseWheel(MouseEventArgs e)
{
  if ((Control.ModifierKeys & Keys.Control) != 0)
  {
    base.OnMouseWheel(e);
  }
  else if (this.VScroll)
  {
    Point position;
    int value;

    position = this.AutoScrollPosition;

    value = Math.Min(Math.Max(-position.Y - e.Delta, 0), this.VerticalScroll.Maximum);

    this.AutoScrollPosition = new Point(-position.X, value);
  }
}


Hope this information helps!

Regards;
Richard Moss
Read "Before You Post" before posting. Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.

fjutoslaw


Richard Moss

Glad it worked for you!

You may wish to keep an eye on issue 18 as I'm probably going to make this part of the core control.

Regards;
Richard Moss
Read "Before You Post" before posting. Do not send me private messages. Do not expect instant replies.

All responses are hand crafted. No AI involved. Possibly no I either.