UserControl: How to add MouseWheel Listener?

后端 未结 4 1533
既然无缘
既然无缘 2021-01-21 01:34

I\'m creating an UserControl that should react if the mouse is over the control and MouseWheel gets rotated.

Currently i\'m doing this as shown here:

            


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 02:23

    Paul Westcott's answer works great when using a FlowLayoutPanel. My implementation of MyOnMouseWheel event is:

    void MyOnMouseWheel(MouseEventArgs e)
    {
        int ChangeIncrement = (this.panel1.VerticalScroll.SmallChange * 4); //Change the 4 to any positive number to scroll more or less one each scroll event.
        if (e.Delta < 0)
        {
            int NewValue = this.panel1.VerticalScroll.Value + ChangeIncrement;
            if (NewValue > this.panel1.VerticalScroll.Maximum)
            {
                this.panel1.VerticalScroll.Value = this.panel1.VerticalScroll.Maximum;
            }
            else
            {
                this.panel1.VerticalScroll.Value = NewValue;
            }
        }
        else if (e.Delta > 0)
        {
            int NewValue = this.panel1.VerticalScroll.Value - ChangeIncrement;
            if (NewValue < this.panel1.VerticalScroll.Minimum)
            {
                this.panel1.VerticalScroll.Value = this.panel1.VerticalScroll.Minimum;
            }
            else
            {
                this.panel1.VerticalScroll.Value = NewValue;
            }
        }
        this.panel1.PerformLayout();
    }
    

提交回复
热议问题