■ ScrollViewer 클래스에서 마우스를 사용해 스크롤하는 방법을 보여준다.
▶ MouseCapture.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 |
using System.Windows; namespace TestProject { /// <summary> /// 마우스 캡처 /// </summary> public class MouseCapture { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 수직 오프셋 - VerticalOffset /// <summary> /// 수직 오프셋 /// </summary> public double VerticalOffset { get; set; } #endregion #region 포인트 - Point /// <summary> /// 포인트 /// </summary> public Point Point { get; set; } #endregion } } |
▶ MouseScrolling.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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace TestProject { /// <summary> /// 마우스 스크롤링 /// </summary> public class MouseScrolling : DependencyObject { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 마우스 캡처 딕셔너리 /// </summary> private static Dictionary<object, MouseCapture> _mouseCaptureDictionary = new Dictionary<object, MouseCapture>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 이용 가능 여부 첨부 속성 - IsEnabledProperty /// <summary> /// 이용 가능 여부 첨부 속성 /// </summary> public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached ( "IsEnabled", typeof(bool), typeof(MouseScrolling), new UIPropertyMetadata(false, IsEnabledPropertyChangedCallback) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이용 가능 여부 - IsEnabled /// <summary> /// 이용 가능 여부 /// </summary> public bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { SetValue(IsEnabledProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 이용 가능 여부 구하기 - GetIsEnabled(d) /// <summary> /// 이용 가능 여부 구하기 /// </summary> /// <param name="d">의존 객체</param> /// <returns>이용 가능 여부</returns> public static bool GetIsEnabled(DependencyObject d) { return (bool)d.GetValue(IsEnabledProperty); } #endregion #region 이용 가능 여부 설정하기 - SetIsEnabled(d, value) /// <summary> /// 이용 가능 여부 설정하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="value">값</param> public static void SetIsEnabled(DependencyObject d, bool value) { d.SetValue(IsEnabledProperty, value); } #endregion //////////////////////////////////////////////////////////////////////////////// Private ////////////////////////////////////////////////////////////////////// Event #region 이용 가능 여부 속성 변경시 콜백 처리하기 - IsEnabledPropertyChangedCallback(d, e) /// <summary> /// 이용 가능 여부 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void IsEnabledPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ScrollViewer scrollViewer = d as ScrollViewer; if(scrollViewer == null) { return; } if((bool)e.NewValue) { scrollViewer.Loaded += scrollViewer_Loaded; } else { scrollViewer_Unloaded(scrollViewer, new RoutedEventArgs()); } } #endregion #region 스크롤 뷰어 로드시 처리하기 - scrollViewer_Loaded(sender, e) /// <summary> /// 스크롤 뷰어 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void scrollViewer_Loaded(object sender, RoutedEventArgs e) { ScrollViewer scrollViewer = sender as ScrollViewer; if(scrollViewer == null) { return; } scrollViewer.Unloaded += scrollViewer_Unloaded; scrollViewer.PreviewMouseLeftButtonDown += scrollViewer_PreviewMouseLeftButtonDown; scrollViewer.PreviewMouseMove += scrollViewer_PreviewMouseMove; scrollViewer.PreviewMouseLeftButtonUp += scrollViewer_PreviewMouseLeftButtonUp; } #endregion #region 스크롤 뷰어 언로드시 처리하기 - scrollViewer_Unloaded(sender, e) /// <summary> /// 스크롤 뷰어 언로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void scrollViewer_Unloaded(object sender, RoutedEventArgs e) { ScrollViewer scrollViewer = sender as ScrollViewer; if(scrollViewer == null) { return; } _mouseCaptureDictionary.Remove(sender); scrollViewer.Loaded -= scrollViewer_Loaded; scrollViewer.Unloaded -= scrollViewer_Unloaded; scrollViewer.PreviewMouseLeftButtonDown -= scrollViewer_PreviewMouseLeftButtonDown; scrollViewer.PreviewMouseMove -= scrollViewer_PreviewMouseMove; scrollViewer.PreviewMouseLeftButtonUp -= scrollViewer_PreviewMouseLeftButtonUp; } #endregion #region 스크롤 뷰어 PREVIEW 마우스 왼쪽 버튼 DOWN 처리하기 - scrollViewer_PreviewMouseLeftButtonDown(sender, e) /// <summary> /// 스크롤 뷰어 PREVIEW 마우스 왼쪽 버튼 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void scrollViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ScrollViewer scrollViewer = e.OriginalSource as ScrollViewer; if(scrollViewer == null) { return; } _mouseCaptureDictionary[sender] = new MouseCapture { VerticalOffset = scrollViewer.VerticalOffset, Point = e.GetPosition(scrollViewer) }; } #endregion #region 스크롤 뷰어 PREVIEW 마우스 이동시 처리하기 - scrollViewer_PreviewMouseMove(sender, e) /// <summary> /// 스크롤 뷰어 PREVIEW 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void scrollViewer_PreviewMouseMove(object sender, MouseEventArgs e) { if(!_mouseCaptureDictionary.ContainsKey(sender)) { return; } if(e.LeftButton != MouseButtonState.Pressed) { _mouseCaptureDictionary.Remove(sender); return; } ScrollViewer scrollViewer = sender as ScrollViewer; if(scrollViewer == null) { return; } MouseCapture mouseCapture = _mouseCaptureDictionary[sender]; Point point = e.GetPosition(scrollViewer); double deltaY = point.Y - mouseCapture.Point.Y; if(Math.Abs(deltaY) > 5) { scrollViewer.CaptureMouse(); } scrollViewer.ScrollToVerticalOffset(mouseCapture.VerticalOffset - deltaY); } #endregion #region 스크롤 뷰어 PREVIEW 마우스 왼쪽 버튼 UP 처리하기 - scrollViewer_PreviewMouseLeftButtonUp(sender, e) /// <summary> /// 스크롤 뷰어 PREVIEW 마우스 왼쪽 버튼 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void scrollViewer_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ScrollViewer srollViewer = sender as ScrollViewer; if(srollViewer == null) { return; } srollViewer.ReleaseMouseCapture(); } #endregion } } |
▶ MainWindow.xaml
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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="ScrollViewer 클래스 : 마우스를 사용해 스크롤하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="Ellipse"> <Setter Property="Width" Value="100" /> <Setter Property="Height" Value="100" /> </Style> </Window.Resources> <Grid> <ScrollViewer VerticalScrollBarVisibility="Auto" local:MouseScrolling.IsEnabled="True"> <Canvas Width="740" Height="1200"> <Ellipse Canvas.Left="100" Canvas.Top="100" Fill="Red" /> <Ellipse Canvas.Left="200" Canvas.Top="200" Fill="Green" /> <Ellipse Canvas.Left="300" Canvas.Top="300" Fill="Blue" /> <Ellipse Canvas.Left="200" Canvas.Top="400" Fill="Red" /> <Ellipse Canvas.Left="100" Canvas.Top="500" Fill="Green" /> <Ellipse Canvas.Left="200" Canvas.Top="600" Fill="Blue" /> <Ellipse Canvas.Left="300" Canvas.Top="700" Fill="Red" /> <Ellipse Canvas.Left="200" Canvas.Top="800" Fill="Green" /> <Ellipse Canvas.Left="100" Canvas.Top="900" Fill="Blue" /> <Ellipse Canvas.Left="200" Canvas.Top="1000" Fill="Red" /> </Canvas> </ScrollViewer> </Grid> </Window> |