FlowLayoutPanel 支持中键滚轮滚动

FlowLayoutPanel控件不直接支持
 所以,Panel控件也是直接不支持MouseWheel事件来进行滚动滚轮的.
你可以添加MouseWheel事件,然后写上支持滚动的功能.也可以直接重写该控件.这样可以复用该控件.
如果只支持MouseWheel事件,还是不一定在滚动滚轮的时候,就能引发MouseWheel事件.所以,必须让鼠标停留在控件上时,让控件处于输入焦点状态.这是,滚动滚轮就可以引发MouseWheel事件了.
废话不多说,贴出代码

     ///
    /// 支持滚轮的FlowLayoutPanel.
    ///
    class ScrollFlowLayoutPanel : FlowLayoutPanel
    {
        public ScrollFlowLayoutPanel()
        {
          
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            this.Focus();
            base.OnMouseClick(e);
        }


        protected override void OnMouseEnter(EventArgs e)
        {
            this.Focus();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (e.Delta < 0)
            {
                if (this.VerticalScroll.Maximum > this.VerticalScroll.Value + 50)
                    this.VerticalScroll.Value += 50;
                else
                    this.VerticalScroll.Value = this.VerticalScroll.Maximum;
            }
            else
            {
                if (this.VerticalScroll.Value > 50)
                    this.VerticalScroll.Value -= 50;
                else
                {
                    this.VerticalScroll.Value = 0;
                }
            }
            
        }
    }
将继承的FlowLayoutPanel修改为Panel,就可以支持Panel的滚轮滚动了.


posted @ 2017-08-21 17:20  Net-Spider  阅读(556)  评论(0)    收藏  举报