■ 크기 조절 그립 만들기

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

 

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"

    Width="600"

    Height="450"

    Title="크기 조절 그립 테스트">

    <Grid>

        <local:MessageControl

            HorizontalAlignment="Center"

            VerticalAlignment="Center" />

    </Grid>

</Window>

 

 

MessageControl.xaml

 

 

<UserControl x:Class="TestProject.MessageControl"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Border

        HorizontalAlignment="Center"

        VerticalAlignment="Center"

        BorderBrush="#ff626161"

        BorderThickness="2"

        CornerRadius="3">

        <DockPanel Name="sizableContent"

            MinWidth="100"

            MinHeight="100"

            Background="LightGray"

            Focusable="False"

            LastChildFill="True">

            <Border

                DockPanel.Dock="Top"

                Background="Gray"

                Height="30">

                <DockPanel>

                    <Button

                        DockPanel.Dock="Right"

                        HorizontalAlignment="Center"

                        VerticalAlignment="Center"

                        Margin="0 0 4 0"

                        Width="16"

                        Height="16"

                        Background="Transparent"

                        HorizontalContentAlignment="Center"

                        VerticalContentAlignment="Top">

                        <Button.Content>

                            <Grid>

                                <Line X1="1" Y1="1" X2="8" Y2="8" Stroke="White" StrokeThickness="1" />

                                <Line X1="1" Y1="8" X2="8" Y2="1" Stroke="White" StrokeThickness="1" />

                            </Grid>

                        </Button.Content>

                    </Button>

                    <TextBlock

                        HorizontalAlignment="Left"

                        VerticalAlignment="Center"

                        Margin="10 0 0 0"

                        Foreground="White"

                        Text="메시지 컨트롤" />

                </DockPanel>

            </Border>

            <DockPanel

                DockPanel.Dock="Bottom"

                HorizontalAlignment="Stretch">

                <Thumb

                    DockPanel.Dock="Right"

                    VerticalAlignment="Bottom"

                    Margin="0 0 1 1"

                    DragStarted="thumb_DragStarted"

                    DragDelta="thumb_DragDelta"

                    DragCompleted="thumb_DragCompleted">

                    <Thumb.Style>

                        <Style TargetType="{x:Type Thumb}" BasedOn="{x:Null}">

                            <Style.Setters>

                                <Setter Property="Template">

                                    <Setter.Value>

                                        <ControlTemplate>

                                            <Grid Name="resizeGrid"

                                                DockPanel.Dock="Right"

                                                VerticalAlignment="Bottom">

                                                <Line X1="6"  Y1="18" X2="18" Y2="6"  Stroke="DarkGray" StrokeThickness="1.5" />

                                                <Line X1="10" Y1="18" X2="18" Y2="10" Stroke="DarkGray" StrokeThickness="1.5" />

                                                <Line X1="14" Y1="18" X2="18" Y2="14" Stroke="DarkGray" StrokeThickness="1.5" />

                                                <Grid.Style>

                                                    <Style TargetType="{x:Type Grid}">

                                                        <Style.Triggers>

                                                            <Trigger Property="IsMouseOver" Value="True">

                                                                <Setter Property="Cursor" Value="SizeNWSE" />

                                                            </Trigger>

                                                        </Style.Triggers>

                                                    </Style>

                                                </Grid.Style>

                                            </Grid>

                                        </ControlTemplate>

                                    </Setter.Value>

                                </Setter>

                            </Style.Setters>

                        </Style>

                    </Thumb.Style>

                </Thumb>

                <StackPanel

                    HorizontalAlignment="Center"

                    Orientation="Horizontal">

                    <Button Margin="12" Width="75" TabIndex="1" Content="Ok" />

                </StackPanel>

            </DockPanel>

            <StackPanel

                HorizontalAlignment="Center"

                Margin="16 16 16 4">

                <TextBlock Text="크기 조정을 위해 아래 오른쪽 코너를 드래그 하시기 바랍니다." />

            </StackPanel>

        </DockPanel>

    </Border>

</UserControl>

 

 

MessageControl.xaml.cs

 

 

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 메시지 컨트롤

    /// </summary>

    public partial class MessageControl : UserControl

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 커서

        /// </summary>

        private Cursor cursor;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MessageControl()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MessageControl()

        {

            InitializeComponent();

        }

 

        #endregion

 

 

        #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.cursor = Cursor;

 

            Cursor = Cursors.SizeNWSE;

        }

 

        #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 adjustX = this.sizableContent.ActualWidth  + e.HorizontalChange;

            double adjustY = this.sizableContent.ActualHeight + e.VerticalChange;

 

            adjustX = adjustX > this.sizableContent.MinWidth  ? adjustX : this.sizableContent.MinWidth;

            adjustY = adjustY > this.sizableContent.MinHeight ? adjustY : this.sizableContent.MinHeight;

 

            this.sizableContent.Width  = adjustX;

            this.sizableContent.Height = adjustY;

        }

 

        #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)

        {

            Cursor = this.cursor;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요