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

▶ TestWindow.xaml

<Window
    x:Class="DS.Test.WPF.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    SizeToContent="WidthAndHeight">
    <Canvas
        Name="canvas"
        Width="300"
        Height="300">
        <Thumb
            Name="thumb"
            Canvas.Left="280"
            Canvas.Top="280"
            Width="20"
            Height="20"
            Background="Blue"
            DragStarted="thumb_DragStarted"
            DragDelta="thumb_DragDelta" 
            DragCompleted="thumb_DragCompleted" />    
    </Canvas>
</Window>

 

728x90

 

▶ TestWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace DS.Test.WPF
{
    /// <summary>
    /// 테스트 윈도우
    /// </summary>
    public partial class TestWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - TestWindow()

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

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Thumb 드래그 시작시 처리하기 - thumb_DragStarted(sender, e)

        /// <summary>
        /// Thumb 드래그 시작시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void thumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            this.thumb.Background = Brushes.Orange;
        }

        #endregion

        #region Thumb 드래그 변화시 처리하기 - thumb_DragDelta(sender, e)

        /// <summary>
        /// Thumb 드래그 변화시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            double newCanvasWidth  = this.canvas.Width  + e.HorizontalChange;
            double newCanvasHeight = this.canvas.Height + e.VerticalChange;

            if((newCanvasWidth >= 0) && (newCanvasHeight >= 0))
            {
                this.canvas.Width  = newCanvasWidth;
                this.canvas.Height = newCanvasHeight;

                Canvas.SetLeft(this.thumb, Canvas.GetLeft(this.thumb) + e.HorizontalChange);
                Canvas.SetTop (this.thumb, Canvas.GetTop (this.thumb) + e.VerticalChange  );
            }
        }

        #endregion

        #region Thumb 드래그 완료시 처리하기 - thumb_DragCompleted(sender, e)

        /// <summary>
        /// Thumb 드래그 완료시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void thumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            this.thumb.Background = Brushes.Blue;
        }

        #endregion
    }
}

 

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

댓글을 달아 주세요