첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ 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 클래스 : 마우스를 사용해 스크롤하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <Style x:Key="ScrollViewerStyleKey" TargetType="{x:Type ScrollViewer}">
            <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
            <Setter Property="VerticalScrollBarVisibility"   Value="Hidden" />
        </Style>
    </Window.Resources>
    <Grid>
        <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.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 스크롤 시작 포인트
        /// </summary>
        private Point scrollStartPoint;

        /// <summary>
        /// 스크롤 시작 오프셋
        /// </summary>
        private Point scrollStartOffset;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            SetItemsControlData();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected
        //////////////////////////////////////////////////////////////////////////////// Function

        #region PREVIEW 마우스 DOWN 처리하기 - OnPreviewMouseDown(e)

        /// <summary>
        /// PREVIEW 마우스 DOWN 처리하기
        /// </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  > this.scrollViewer.ViewportWidth ) ||
                         (this.scrollViewer.ExtentHeight > this.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 delta = new Point
                (
                    (mousePoint.X > this.scrollStartPoint.X) ? -(mousePoint.X - this.scrollStartPoint.X) : (this.scrollStartPoint.X - mousePoint.X),
                    (mousePoint.Y > this.scrollStartPoint.Y) ? -(mousePoint.Y - this.scrollStartPoint.Y) : (this.scrollStartPoint.Y - mousePoint.Y)
                );

                this.scrollViewer.ScrollToHorizontalOffset(this.scrollStartOffset.X + delta.X);
                this.scrollViewer.ScrollToVerticalOffset  (this.scrollStartOffset.Y + delta.Y);
            }

            base.OnPreviewMouseMove(e);
        }

        #endregion
        #region PREVIEW 마우스 UP 처리하기 - OnPreviewMouseUp(e)

        /// <summary>
        /// PREVIEW 마우스 UP 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            if(IsMouseCaptured)
            {
                Cursor = Cursors.Arrow;

                ReleaseMouseCapture();
            }

            base.OnPreviewMouseUp(e);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// 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()
        {
            itemsControl.Items.Clear();

            itemsControl.Items.Add(GetStackPanel(Brushes.Red  ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Green));
            itemsControl.Items.Add(GetStackPanel(Brushes.Blue ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Red  ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Green));
            itemsControl.Items.Add(GetStackPanel(Brushes.Blue ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Red  ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Green));
            itemsControl.Items.Add(GetStackPanel(Brushes.Blue ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Red  ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Green));
            itemsControl.Items.Add(GetStackPanel(Brushes.Blue ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Red  ));
            itemsControl.Items.Add(GetStackPanel(Brushes.Green));
            itemsControl.Items.Add(GetStackPanel(Brushes.Blue ));
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요