■ ScrollViewer 클래스 : 마우스를 사용해 스크롤하기

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


TestProject.zip


MainWindow.xaml

 

 

<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

 

 

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();

 

            this.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

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요