첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MoveThumb.cs

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

namespace TestProject
{
    /// <summary>
    /// 이동 썸
    /// </summary>
    public class MoveThumb : Thumb
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MoveThumb

        /// <summary>
        /// 생성자
        /// </summary>
        public MoveThumb()
        {
            DragDelta += Thumb_DragDelta;
        }

        #endregion

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

        #region 썸 드래그 델타 처리하기 - Thumb_DragDelta(sender, e)

        /// <summary>
        /// 썸 드래그 델타 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Control control = DataContext as Control;

            if(control != null)
            {
                double left = Canvas.GetLeft(control);
                double top  = Canvas.GetTop (control);

                Canvas.SetLeft(control, left + e.HorizontalChange);
                Canvas.SetTop (control, top  + e.VerticalChange  );
            }
        }

        #endregion
    }
}

 

728x90

 

▶ ResizeThumb.cs

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

namespace TestProject
{
    /// <summary>
    /// 크기 조정 썸
    /// </summary>
    public class ResizeThumb : Thumb
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - ResizeThumb()

        /// <summary>
        /// 생성자
        /// </summary>
        public ResizeThumb()
        {
            DragDelta += Thumb_DragDelta;
        }

        #endregion

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

        #region 썸 드래그 델타 처리하기 - Thumb_DragDelta(sender, e)

        /// <summary>
        /// 썸 드래그 델타 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Control control = DataContext as Control;

            if(control != null)
            {
                double deltaX;
                double deltaY;

                switch(VerticalAlignment)
                {
                    case VerticalAlignment.Bottom :

                        deltaY = Math.Min(-e.VerticalChange, control.ActualHeight - control.MinHeight);

                        control.Height -= deltaY;

                        break;

                    case VerticalAlignment.Top :

                        deltaY = Math.Min(e.VerticalChange, control.ActualHeight - control.MinHeight);

                        Canvas.SetTop(control, Canvas.GetTop(control) + deltaY);

                        control.Height -= deltaY;

                        break;

                    default :

                        break;
                }

                switch(HorizontalAlignment)
                {
                    case HorizontalAlignment.Left :

                        deltaX = Math.Min(e.HorizontalChange, control.ActualWidth - control.MinWidth);

                        Canvas.SetLeft(control, Canvas.GetLeft(control) + deltaX);

                        control.Width -= deltaX;

                        break;

                    case HorizontalAlignment.Right :

                        deltaX = Math.Min(-e.HorizontalChange, control.ActualWidth - control.MinWidth);

                        control.Width -= deltaX;

                        break;

                    default :

                        break;
                }
            }

            e.Handled = true;
        }

        #endregion
    }
}

 

300x250

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    WindowStartupLocation="CenterScreen"
    Width="800"
    Height="600"
    Title="Thumb 클래스 : 객체 이동하기/크기 조정하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <!-- MoveThumb 템플리트 -->
        <ControlTemplate x:Key="MoveThumbControlTemplateKey" TargetType="{x:Type local:MoveThumb}">
            <Rectangle Fill="Transparent" />
        </ControlTemplate>
        <!-- ResizeThumb 템플리트 -->
        <ControlTemplate x:Key="ResizeThumbControlTemplateKey" TargetType="{x:Type Control}">
            <Grid>
                <local:ResizeThumb
                    HorizontalAlignment="Left"
                    VerticalAlignment="Stretch"
                    Margin="-4 0 0 0"
                    Width="3"
                    Cursor="SizeWE" />
                <local:ResizeThumb
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Top"
                    Margin="0 -4 0 0"
                    Height="3"
                    Cursor="SizeNS" />
                <local:ResizeThumb
                    HorizontalAlignment="Right"
                    VerticalAlignment="Stretch"
                    Margin="0 0 -4 0"
                    Width="3"
                    Cursor="SizeWE" />
                <local:ResizeThumb
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Bottom"
                    Margin="0 0 0 -4"
                    Height="3"
                    Cursor="SizeNS" />

                <local:ResizeThumb
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Margin="-6 -6 0 0"
                    Width="7"
                    Height="7"
                    Cursor="SizeNWSE" />
                <local:ResizeThumb
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    Margin="0 -6 -6 0"
                    Width="7"
                    Height="7"
                    Cursor="SizeNESW" />
                <local:ResizeThumb
                    HorizontalAlignment="Left"
                    VerticalAlignment="Bottom"
                    Margin="-6 0 0 -6"
                    Width="7"
                    Height="7"
                    Cursor="SizeNESW" />
                <local:ResizeThumb
                    HorizontalAlignment="Right"
                    VerticalAlignment="Bottom"
                    Margin="0 0 -6 -6"
                    Width="7"
                    Height="7"
                    Cursor="SizeNWSE" />
            </Grid>
        </ControlTemplate>
        <ControlTemplate x:Key="DesignerItemControlTemplateKey" TargetType="ContentControl">
            <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                <local:MoveThumb
                    Template="{StaticResource MoveThumbControlTemplateKey}"
                    Cursor="SizeAll" />
                <Control Template="{StaticResource ResizeThumbControlTemplateKey}" />
                <ContentPresenter Content="{TemplateBinding ContentControl.Content}" />
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Canvas>
        <ContentControl Canvas.Left="200" Canvas.Top="200"
            MinWidth="50"
            MinHeight="50"
            Width="150"
            Height="150"
            Template="{StaticResource DesignerItemControlTemplateKey}">
            <Ellipse
                Fill="Red"
                IsHitTestVisible="False" />
        </ContentControl>
        <ContentControl Canvas.Left="400" Canvas.Top="200"
            MinWidth="50"
            MinHeight="50"
            Width="150"
            Height="150"
            Template="{StaticResource DesignerItemControlTemplateKey}">
            <Path
                Stretch="Fill"
                Fill="Blue"
                IsHitTestVisible="False"
                Data="M 0 5, 5 0, 10 5, 5 10 Z" />
    </ContentControl>
  </Canvas>
</Window>

 

▶ MainWindow.xaml.cs

using System.Windows;

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

        #region 생성자 - MainWindow()

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

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요