Cyotek Forums

Source Code => Source Code => Topic started by: fjutoslaw on February 03, 2017, 07:13:06 PM

Title: Changing mouse scroll and zoom behaviour.
Post by: fjutoslaw on February 03, 2017, 07:13:06 PM
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.
Title: Re: Changing mouse scroll and zoom behaviour.
Post by: Richard Moss on February 04, 2017, 07:34:15 AM
Hello,

Welcome to the forums and thanks for the question. This question was actual asked on GitHub (https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox/issues/9) 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
Title: Re: Changing mouse scroll and zoom behaviour.
Post by: fjutoslaw on February 28, 2017, 08:59:23 PM
This is awesome! Big thanks kind man!
Title: Re: Changing mouse scroll and zoom behaviour.
Post by: Richard Moss on March 01, 2017, 04:54:54 PM
Glad it worked for you!

You may wish to keep an eye on issue 18 (https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox/issues/18) as I'm probably going to make this part of the core control.

Regards;
Richard Moss