DevExpress 의 PanelControl 에서 스크롤 속성이 없다.

그래서 XtraScrollableControl 컨트롤을 사용하는데

여기에 다른 컨트롤을 갖다 놓으면 스크롤을 휠로 제어할수 없게된다ㅜㅠ

 

이를 위해선 아래 처럼 ScrollHelper 를 선언한후

XtraScrollableControl 컨트롤이 들어간 화면 초입에 아래처럼 코딩하면

문제없이 휠로도 스크롤이 가능해진다.

 

ScrollHelper scrollHelper = new ScrollHelper();

scrollHelper.EnableScrollOnMouseWheel();

 

또한 화면이 Dispose() 이벤트에서 아래처럼

기능을 꺼야한다.

scrollHelper.DisableScrollOnMouseWheel();

 

 

    public class ScrollHelper

    {

        XtraScrollableControl scrollableControl;

 

        public ScrollHelper(XtraScrollableControl scrollableControl)

        {

            this.scrollableControl = scrollableControl;

        }

 

        public void EnableScrollOnMouseWheel()

        {

            scrollableControl.VisibleChanged += OnVisibleChanged;

        }

 

        void OnVisibleChanged(object sender, EventArgs e)

        {

            scrollableControl.Select();

            UnsubscribeFromMouseWheel(scrollableControl.Controls);

            SubscribeToMouseWheel(scrollableControl.Controls);

        }

 

        private void SubscribeToMouseWheel(Control.ControlCollection controls)

        {

            foreach (Control ctrl in controls)

            {

                ctrl.MouseWheel += OnMouseWheel;

                SubscribeToMouseWheel(ctrl.Controls);

            }

        }

 

        private void UnsubscribeFromMouseWheel(Control.ControlCollection controls)

        {

            foreach (Control ctrl in controls)

            {

                ctrl.MouseWheel -= OnMouseWheel;

                UnsubscribeFromMouseWheel(ctrl.Controls);

            }

        }

 

        void OnMouseWheel(object sender, MouseEventArgs e)

        {

            DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;

            int scrollValue = scrollableControl.VerticalScroll.Value;

            int 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);

            scrollableControl = null;

        }

    }

 

 

참고

https://www.devexpress.com/Support/Center/Question/Details/K18512/how-to-enable-scrolling-in-xtrascrollablecontrol-by-using-the-mouse-wheel

+ Recent posts