■ XtraScrollableControl 클래스에서 마우스 휠 이벤트를 사용하는 방법을 보여주낟.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
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 } } |