첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

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="마우스 드래그로 사각형 그리기"
    Background="RoyalBlue"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Background="#01000000">
        <Canvas
            IsHitTestVisible="False"
            IsEnabled="False">
            <ContentControl x:Name="addItemContentControl"
                Visibility="Collapsed">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <Grid >
                            <Rectangle x:Name="addItemRectangle"
                                SnapsToDevicePixels="True"
                                Stroke="Yellow"
                                StrokeThickness="1"
                                StrokeDashArray="1 1 1 3"
                                Fill="#770000ff" />
                            <Grid x:Name="topAdornerGrid">
                                <Rectangle
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Top"
                                    Margin="0 -18 0 0"
                                    Width="1"
                                    Height="15"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="TopLeft" />
                                <Rectangle
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Top"
                                    Margin="0 -10 0 0"
                                    Width="{Binding ActualWidth, ElementName=addItemRectangle}"
                                    Height="1"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="TopCenter" />
                                <Rectangle
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Margin="0 -18 0 0"
                                    Width="1"
                                    Height="15"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="TopRight" />
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Top"
                                    Margin="0 -22 0 0"
                                    Height="15"
                                    Width="{Binding ActualWidth, ElementName=addItemRectangle}"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    ClipToBounds ="True"
                                    TextAlignment="Center"
                                    Foreground="Yellow"
                                    FontSize="9"
                                    Text="{Binding ActualWidth, ElementName=addItemRectangle, StringFormat='0.00'}" />
                            </Grid>
                            <Grid x:Name="bottomAdornerGrid" >
                                <Rectangle
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Bottom"
                                    Margin="0 0 0 -18"
                                    Width="1"
                                    Height="15"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="BottomLeft" />
                                <Rectangle
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Bottom"
                                    Margin="0 0 0 -10"
                                    Height="1"
                                    Width="{Binding ActualWidth, ElementName=addItemRectangle}"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="BottomCenter"/>
                                <Rectangle
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Bottom"
                                    Width="1"
                                    Margin="0 0 0 -18"
                                    Height="15"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="BottomRight" />
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Bottom"
                                    Margin="0 0 0 -29"
                                    Width="{Binding ActualWidth, ElementName=addItemRectangle}"
                                    Height="15"
                                    Text="{Binding ActualWidth, ElementName=addItemRectangle, StringFormat='0.00'}"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    ClipToBounds ="True"
                                    TextAlignment="Center"
                                    Foreground="Yellow"
                                    FontSize="9" />
                            </Grid>
                            <Grid x:Name="leftAdornerGrid"
                                Margin="10 0 0 0">
                                <Rectangle
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Top"
                                    Margin="-27 0 0 0"
                                    Width="15"
                                    Height="1"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="LeftTop" />
                                <Rectangle
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Center"
                                    Height="{Binding ActualHeight, ElementName=addItemRectangle}"
                                    Margin="-20 0 0 0"
                                    Width="1"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="LeftCenter" />
                                <Rectangle
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Bottom"
                                    Margin="-27 0 0 0"
                                    Width="15"
                                    Height="1"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="LeftBottom" />
                                <StackPanel
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Center"
                                    Margin="-33 70 0 0"
                                    Height="25"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False">
                                    <StackPanel.RenderTransform>
                                        <RotateTransform Angle="270" />
                                    </StackPanel.RenderTransform>
                                    <TextBlock
                                        Height="25"
                                        Foreground="Yellow"
                                        FontSize="9"
                                        Text="{Binding ActualHeight, ElementName=addItemRectangle, StringFormat='0.00'}" />
                                </StackPanel>
                            </Grid>
                            <Grid x:Name="rightAdornerGrid"
                                Margin="10 0 0 0">
                                <Rectangle
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Top"
                                    Margin="0 0 -18 0"
                                    Width="15"
                                    Height="1"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="RightTop" />
                                <Rectangle
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Margin="0 0 -11 0"
                                    Width="1"
                                    Height="{Binding ActualHeight, ElementName=addItemRectangle}"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="RightCenter" />
                                <Rectangle
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Bottom"
                                    Margin="0 0 -18 0"
                                    Width="15"
                                    Height="1"
                                    Fill="Yellow"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False"
                                    Tag="RightBottom" />
                                <StackPanel
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Margin="0 0 -51 0"
                                    Height="25"
                                    SnapsToDevicePixels="True"
                                    IsHitTestVisible="False"
                                    IsEnabled="False">
                                    <StackPanel.RenderTransform>
                                        <RotateTransform Angle="90" />
                                    </StackPanel.RenderTransform>
                                    <TextBlock
                                        Height="25"
                                        Foreground="Yellow"
                                        FontSize="9"
                                        Text="{Binding ActualHeight, ElementName=addItemRectangle, StringFormat='0.00'}" />
                                </StackPanel>
                            </Grid>
                            <TextBlock
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Foreground="Yellow"
                                Text="+" />
                        </Grid>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>
        </Canvas>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

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

        #region Field

        /// <summary>
        /// 마우스 포인트
        /// </summary>
        private Point? mousePoint;
 
        /// <summary>
        /// 항목 추가 사각형
        /// </summary>
        private Rect addItemRectangle;

        #endregion

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

        #region 생성자 - MainWindow()

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

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 마우스 왼쪽 버튼 DOWN 처리하기 - OnMouseLeftButtonDown(e)

        /// <summary>
        /// 마우스 왼쪽 버튼 DOWN 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            this.mousePoint = e.GetPosition(this);

            CaptureMouse();
        }

        #endregion
        #region 프리뷰 마우스 이동시 처리하기 - OnPreviewMouseMove(e)

        /// <summary>
        /// 프리뷰 마우스 이동시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            base.OnPreviewMouseMove(e);
 
            if(Mouse.LeftButton == MouseButtonState.Pressed)
            {
                Point mousePoint = e.GetPosition(this);
 
                this.addItemContentControl.Visibility = Visibility.Visible;
 
                this.addItemRectangle = new Rect
                (
                    new Point
                    (
                        this.mousePoint.Value.X,
                        this.mousePoint.Value.Y
                    ),
                    new Point(mousePoint.X, mousePoint.Y)
                );
 
                Canvas.SetLeft(this.addItemContentControl, this.addItemRectangle.X);
                Canvas.SetTop (this.addItemContentControl, this.addItemRectangle.Y);

                this.addItemContentControl.Width  = Math.Abs(this.addItemRectangle.Width );
                this.addItemContentControl.Height = Math.Abs(this.addItemRectangle.Height);
            }
        }

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

        /// <summary>
        /// 마우스 UP 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            ReleaseMouseCapture();
        }

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

댓글을 달아 주세요