FlowLayoutPanel 支持中键滚轮滚动

原贴:http://blog.sina.com.cn/s/blog_6b965dd70101po76.html

FlowLayoutPanel控件不直接支持MouseWheel事件.即滚动滚轮也不会响应.所以必须手动来支持响应滚轮.

查看了一下FlowLayoutPanel控件的源码,原来FlowLayoutPanel控件是继承于Panel控件的.
所以,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的滚轮滚动了.

你可能感兴趣的:(FlowLayoutPanel 支持中键滚轮滚动)