■ 마우스 이벤트를 사용해 스크롤을 처리하는 방법을 보여준다.
▶ PanelScrollHelper.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 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media.Animation; namespace TestProject { /// <summary> /// 패널 스크롤 헬퍼 /// </summary> public class PanelScrollHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 최소 시간 /// </summary> private const int MINIMUM_TIME = 100; /// <summary> /// 최소 범위 /// </summary> private const int MINIMUM_RANGE = 30; /// <summary> /// 타겟 프레임워크 엘리먼트 /// </summary> private FrameworkElement targetFrameworkElement = null; /// <summary> /// 타겟 부모 프레임워크 엘리먼트 /// </summary> private FrameworkElement targetParentFrameworkElement = null; /// <summary> /// 시작점 /// </summary> private Point startPoint; /// <summary> /// 종료점 /// </summary> private Point endPoint; /// <summary> /// 이동 더블 애니메이션 /// </summary> private DoubleAnimation moveDoubleAnimation; /// <summary> /// 최근 이동 시간 /// </summary> private double lastMoveTime; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PanelScrollHelper(targetFrameworkElement) /// <summary> /// 생성자 /// </summary> /// <param name="targetFrameworkElement">타겟 프레임워크 엘리먼트</param> public PanelScrollHelper(FrameworkElement targetFrameworkElement) { this.targetFrameworkElement = targetFrameworkElement; this.targetParentFrameworkElement = this.targetFrameworkElement.Parent as FrameworkElement; this.targetFrameworkElement.PreviewMouseDown += targetFrameworkElement_PreviewMouseDown; this.targetFrameworkElement.PreviewMouseMove += targetFrameworkElement_PreviewMouseMove; this.targetFrameworkElement.PreviewMouseUp += targetFrameworkElement_PreviewMouseUp; this.moveDoubleAnimation = new DoubleAnimation(); this.moveDoubleAnimation.Duration = TimeSpan.FromMilliseconds(1000); this.moveDoubleAnimation.DecelerationRatio = 0.8d; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 타겟 프레임워크 엘리먼트 프리뷰 마우스 버튼 하강시 처리하기 - targetFrameworkElement_PreviewMouseDown(sender, e) /// <summary> /// 타겟 프레임워크 엘리먼트 프리뷰 마우스 버튼 하강시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetFrameworkElement_PreviewMouseDown(object sender, MouseButtonEventArgs e) { this.startPoint = e.GetPosition(this.targetFrameworkElement); this.targetFrameworkElement.CaptureMouse(); } #endregion #region 타겟 프레임워크 엘리먼트 프리뷰 마우스 이동시 처리하기 - targetFrameworkElement_PreviewMouseMove(sender, e) /// <summary> /// 타겟 프레임워크 엘리먼트 프리뷰 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetFrameworkElement_PreviewMouseMove(object sender, MouseEventArgs e) { if(e.LeftButton != MouseButtonState.Pressed) { return; } if((DateTime.Now.TimeOfDay.TotalMilliseconds - this.lastMoveTime) < MINIMUM_TIME) { return; } this.lastMoveTime = DateTime.Now.TimeOfDay.TotalMilliseconds; Point targetParentFrameworkElementPoint = e.GetPosition(this.targetParentFrameworkElement); if(targetParentFrameworkElementPoint.Y == this.endPoint.Y) { return; } this.endPoint = targetParentFrameworkElementPoint; Point targetFrameworkElementPoint = e.GetPosition(this.targetFrameworkElement); double movePosition = (targetFrameworkElementPoint.Y - this.startPoint.Y); if(Math.Abs(movePosition) < MINIMUM_RANGE) { return; } double currentCanvasTop = (double)(this.targetFrameworkElement.GetValue(Canvas.TopProperty)); double delta = movePosition * 2d; double minimumCanvasTop = this.targetParentFrameworkElement.ActualHeight - this.targetFrameworkElement.ActualHeight; double maximumCanvasTop = 0d; if(currentCanvasTop + delta < minimumCanvasTop) { delta += minimumCanvasTop - (currentCanvasTop + delta); } if(currentCanvasTop + delta > maximumCanvasTop) { delta -= currentCanvasTop + delta - maximumCanvasTop; } this.moveDoubleAnimation.By = delta; this.targetFrameworkElement.BeginAnimation(Canvas.TopProperty, this.moveDoubleAnimation); } #endregion #region 타겟 프레임워크 엘리먼트 프리뷰 마우스 버튼 상승시 처리하기 - targetFrameworkElement_PreviewMouseUp(sender, e) /// <summary> /// 타겟 프레임워크 엘리먼트 프리뷰 마우스 버튼 상승시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetFrameworkElement_PreviewMouseUp(object sender, MouseButtonEventArgs e) { this.targetFrameworkElement.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 39 |
<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="마우스 이벤트를 사용해 스크롤 처리하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Border HorizontalAlignment="Center" VerticalAlignment="Center" Width="600" Height="450" BorderBrush="Black" BorderThickness="1"> <Canvas Width="598" Height="448" HorizontalAlignment="Center" VerticalAlignment="Center" ClipToBounds="True"> <StackPanel x:Name="stackPanel" Canvas.Top="0" Width="598" HorizontalAlignment="Center" Background="BurlyWood"> <StackPanel.Resources> <Style TargetType="Button"> <Setter Property="Margin" Value="5" /> <Setter Property="Height" Value="30" /> </Style> </StackPanel.Resources> </StackPanel> </Canvas> </Border> </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 |
using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { for(int i = 0; i < 99; i++) { TextBlock textBlock = new TextBlock(); textBlock.Text = string.Format("{0:d2}번째 문장입니다.", i + 1); this.stackPanel.Children.Add(textBlock); } PanelScrollHelper helper = new PanelScrollHelper(this.stackPanel); } #endregion } } |