■ XtraScrollableControl 클래스 : 마우스 휠 이벤트 사용하기

------------------------------------------------------------------------------------------------------------------------

예제

 

 

using DevExpress.XtraEditors;

 

...

 

/// <summary>

/// 스크롤 가능 컨트롤

/// </summary>

private XtraScrollableControl scrollableControl;

 

...

 

/// <summary>

/// 스크롤 헬퍼

/// </summary>

private ScrollHelper scrollHelper = null;

 

...

 

 

this.scrollHelper = new ScrollHelper(this.scrollableControl);

 

this.scrollHelper.EnableScrollOnMouseWheel();

 

...

 

if(this.scrollHelper != null)

{

    this.scrollHelper.DisableScrollOnMouseWheel();

}

 

 

ScrollHelper.cs

 

 

using System;

using System.Windows.Forms;

 

using DevExpress.Utils;

using DevExpress.XtraEditors;

 

namespace DSCORE.WINFORM.UI.CONTROL.DEVEXPRESS

{

    /// <summary>

    /// 스크롤 헬퍼

    /// </summary>

    /// <remarks>XtraScrollableControl의 마우스 휠 기능을 제공한다.</remarks>

    public class ScrollHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 소스 컨트롤

        /// </summary>

        private XtraScrollableControl sourceControl;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - ScrollHelper(sourceControl)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="sourceControl">소스 컨트롤</param>

        public ScrollHelper(XtraScrollableControl sourceControl)  

        {  

            this.sourceControl = sourceControl;  

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 마우스 WHEEL 스크롤 활성화 하기 - EnableScrollOnMouseWheel()

 

        /// <summary>

        /// 마우스 WHEEL 스크롤 활성화 하기

        /// </summary>

        public void EnableScrollOnMouseWheel()

        {  

            sourceControl.VisibleChanged += sourceControl_VisibleChanged;  

        }

 

        #endregion

        #region 마우스 WHEEL 스크롤 비활성화 하기 - DisableScrollOnMouseWheel()

 

        /// <summary>

        /// 마우스 WHEEL 스크롤 비활성화 하기

        /// </summary>

        public void DisableScrollOnMouseWheel()

        {  

            sourceControl.VisibleChanged -= sourceControl_VisibleChanged;  

 

            UnsubscribeFromMouseWheel(sourceControl.Controls);  

 

            sourceControl = null;  

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 소스 컨트롤 표시 여부 변경시 처리하기 - sourceControl_VisibleChanged(sender, e)

 

        /// <summary>

        /// 소스 컨트롤 표시 여부 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void sourceControl_VisibleChanged(object sender, EventArgs e)

        {  

            sourceControl.Select();

 

            UnsubscribeFromMouseWheel(sourceControl.Controls);  

 

            SubscribeToMouseWheel(sourceControl.Controls);  

        }

 

        #endregion

        #region 컨트롤 마우스 WHEEL 처리하기 - control_MouseWheel(sender, e)  

 

        /// <summary>

        /// 컨트롤 마우스 WHEEL 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void control_MouseWheel(object sender, MouseEventArgs e)  

        {  

            DXMouseEventArgs.GetMouseArgs(e).Handled = true;

 

            int scrollValue = sourceControl.VerticalScroll.Value;

            int largeChange = sourceControl.VerticalScroll.LargeChange;

 

            if(e.Delta < 0)

            {

                sourceControl.VerticalScroll.Value += sourceControl.VerticalScroll.LargeChange;

            }

            else

            {

                if(scrollValue < largeChange)

                {

                    sourceControl.VerticalScroll.Value = 0;

                }

                else

                {

                    sourceControl.VerticalScroll.Value -= largeChange;

                }

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 마우스 WHEEL 구독하기 - SubscribeToMouseWheel(controlCollection)  

 

        /// <summary>

        /// 마우스 WHEEL 구독하기

        /// </summary>

        /// <param name="controlCollection">컨트롤 컬렉션</param>

        private void SubscribeToMouseWheel(Control.ControlCollection controlCollection)

        {  

            foreach(Control control in controlCollection)  

            {  

                control.MouseWheel += control_MouseWheel;

 

                SubscribeToMouseWheel(control.Controls);

            }  

        }

 

        #endregion

        #region 마우스 WHEEL 구독 취소하기 - UnsubscribeFromMouseWheel(controlCollection)

 

        /// <summary>

        /// 마우스 WHEEL 구독 취소하기

        /// </summary>

        /// <param name="controlCollection">컨트롤 컬렉션</param>

        private void UnsubscribeFromMouseWheel(Control.ControlCollection controlCollection)

        {  

            foreach(Control control in controlCollection)

            {  

                control.MouseWheel -= control_MouseWheel;

 

                UnsubscribeFromMouseWheel(control.Controls);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요