■ ScrollViewer 클래스에서 마우스를 사용해 스크롤을 하는 방법을 보여준다.
▶ 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="ScrollViewer 클래스 : 마우스를 사용해 스크롤하기"> <Window.Resources> <Style x:Key="ScrollViewerStyleKey" TargetType="{x:Type ScrollViewer}"> <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" /> <Setter Property="VerticalScrollBarVisibility" Value="Hidden" /> </Style> </Window.Resources> <Grid Margin="0"> <ScrollViewer Name="scrollViewer" Style="{StaticResource ScrollViewerStyleKey}"> <ItemsControl Name="itemsControl" VerticalAlignment="Center"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </ScrollViewer> </Grid> </Window> |
▶ MainWindow.xaml.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 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Threading; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 마찰 저항 /// </summary> private double friction; /// <summary> /// 디스패처 타이머 /// </summary> private DispatcherTimer dispatcherTimer = new DispatcherTimer(); /// <summary> /// 속도 /// </summary> private Vector velocity; /// <summary> /// 이전 포인트 /// </summary> private Point previousPoint; /// <summary> /// 스크롤 타겟 포인트 /// </summary> private Point scrollTargetPoint; /// <summary> /// 스크롤 시작 포인트 /// </summary> private Point scrollStartPoint; /// <summary> /// 스크롤 시작 오프셋 /// </summary> private Point scrollStartOffset; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); SetItemsControlData(); this.friction = 0.95; this.dispatcherTimer = new DispatcherTimer(); this.dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 20); this.dispatcherTimer.Tick += dispatcherTimer_Tick; this.dispatcherTimer.Start(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 마우스 DOWN PREVIEW 처리하기 - OnPreviewMouseDown(e) /// <summary> /// 마우스 DOWN PREVIEW 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPreviewMouseDown(MouseButtonEventArgs e) { if(this.scrollViewer.IsMouseOver) { this.scrollStartPoint = e.GetPosition(this); this.scrollStartOffset.X = this.scrollViewer.HorizontalOffset; this.scrollStartOffset.Y = this.scrollViewer.VerticalOffset; Cursor = (this.scrollViewer.ExtentWidth > scrollViewer.ViewportWidth) || (scrollViewer.ExtentHeight > scrollViewer.ViewportHeight) ? Cursors.ScrollAll : Cursors.Arrow; CaptureMouse(); } base.OnPreviewMouseDown(e); } #endregion #region 마우스 이동시 PREVIEW 처리하기 - OnPreviewMouseMove(e) /// <summary> /// 마우스 이동시 PREVIEW 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPreviewMouseMove(MouseEventArgs e) { if(IsMouseCaptured) { Point mousePoint = e.GetPosition(this); Point deltaPoint = new Point(this.scrollStartPoint.X - mousePoint.X, this.scrollStartPoint.Y - mousePoint.Y); this.scrollTargetPoint.X = this.scrollStartOffset.X + deltaPoint.X; this.scrollTargetPoint.Y = this.scrollStartOffset.Y + deltaPoint.Y; this.scrollViewer.ScrollToHorizontalOffset(this.scrollTargetPoint.X); this.scrollViewer.ScrollToVerticalOffset (this.scrollTargetPoint.Y); } base.OnPreviewMouseMove(e); } #endregion #region 마우스 UP PREVIEW 처리하기 - OnPreviewMouseUp(e) /// <summary> /// 마우스 UP PREVIEW 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPreviewMouseUp(MouseButtonEventArgs e) { if(IsMouseCaptured) { Cursor = Cursors.Arrow; ReleaseMouseCapture(); } base.OnPreviewMouseUp(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 디스패처 타이머 틱 처리하기 - dispatcherTimer_Tick(sender, e) /// <summary> /// 디스패처 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dispatcherTimer_Tick(object sender, EventArgs e) { if(IsMouseCaptured) { Point mousePoint = Mouse.GetPosition(this); this.velocity = this.previousPoint - mousePoint; this.previousPoint = mousePoint; } else { if(this.velocity.Length > 1) { this.scrollViewer.ScrollToHorizontalOffset(this.scrollTargetPoint.X); this.scrollViewer.ScrollToVerticalOffset (this.scrollTargetPoint.Y); this.scrollTargetPoint.X += this.velocity.X; this.scrollTargetPoint.Y += this.velocity.Y; this.velocity *= this.friction; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 스택 패널 구하기 - GetStackPanel(brush) /// <summary> /// 스택 패널 구하기 /// </summary> /// <param name="brush">브러시</param> /// <returns>스택 패널</returns> private StackPanel GetStackPanel(SolidColorBrush brush) { StackPanel stackPanel = new StackPanel(); stackPanel.Orientation = Orientation.Horizontal; for(int i = 0; i < 50; i++) { Rectangle rectangle = new Rectangle(); rectangle.Width = 100; rectangle.Height = 100; rectangle.Margin = new Thickness(5); rectangle.Fill = i % 2 == 0 ? Brushes.LightGray : brush; stackPanel.Children.Add(rectangle); } return stackPanel; } #endregion #region 항목 컨트롤 데이터 설정하기 - SetItemsControlData() /// <summary> /// 항목 컨트롤 데이터 설정하기 /// </summary> private void SetItemsControlData() { this.itemsControl.Items.Clear(); this.itemsControl.Items.Add(GetStackPanel(Brushes.Red )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Green)); this.itemsControl.Items.Add(GetStackPanel(Brushes.Blue )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Red )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Green)); this.itemsControl.Items.Add(GetStackPanel(Brushes.Blue )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Red )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Green)); this.itemsControl.Items.Add(GetStackPanel(Brushes.Blue )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Red )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Green)); this.itemsControl.Items.Add(GetStackPanel(Brushes.Blue )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Red )); this.itemsControl.Items.Add(GetStackPanel(Brushes.Green)); this.itemsControl.Items.Add(GetStackPanel(Brushes.Blue )); } #endregion } } |