using System; using System.Windows.Forms; using DevExpress.XtraEditors; namespace WindowsFormsApplication1 { public class ScrollHelper { readonly XtraScrollableControl _scrollableControl; public ScrollHelperXtraScrollableControl scrollableControl) { _scrollableControl = scrollableControl; } public void EnableScrollOnMouseWheel) { _scrollableControl.VisibleChanged += OnVisibleChanged; } void OnVisibleChangedobject sender, EventArgs e) { _scrollableControl.Select); UnsubscribeFromMouseWheel_scrollableControl.Controls); SubscribeToMouseWheel_scrollableControl.Controls); } public void SubscribeToMouseWheelControl.ControlCollection controls) { foreach Control ctrl in controls) { ctrl.MouseWheel += OnMouseWheel; SubscribeToMouseWheelctrl.Controls); } } public void UnsubscribeFromMouseWheelControl.ControlCollection controls) { foreach Control ctrl in controls) { ctrl.MouseWheel -= OnMouseWheel; UnsubscribeFromMouseWheelctrl.Controls); } } void OnMouseWheelobject sender, MouseEventArgs e) { DevExpress.Utils.DXMouseEventArgs.GetMouseArgse).Handled = true; var scrollValue = _scrollableControl.VerticalScroll.Value; var largeChange = _scrollableControl.VerticalScroll.LargeChange; if e.Delta < 0) _scrollableControl.VerticalScroll.Value += _scrollableControl.VerticalScroll.LargeChange; else if scrollValue < largeChange) { _scrollableControl.VerticalScroll.Value = 0; } else { _scrollableControl.VerticalScroll.Value -= largeChange; } } public void DisableScrollOnMouseWheel) { _scrollableControl.VisibleChanged -= OnVisibleChanged; UnsubscribeFromMouseWheel_scrollableControl.Controls); } } }
调用方法:
private ScrollHelper _scrollHelper; public 构造函数) { InitializeComponent); _scrollHelper = new ScrollHelperxtraScrollableControl1); _scrollHelper.EnableScrollOnMouseWheel); }