728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] EventTrigger 클래스 : RoutedEvent 속성을 사용해 애니메이션 사용하기 (0) | 2020.07.27 |
---|---|
[C#/WPF] Adorner 클래스 : 단순 어도너 사용하기 (0) | 2020.07.27 |
[C#/WPF] Adorner 클래스 : 크기 조정 어도너 사용하기 (0) | 2020.07.27 |
[C#/WPF] AccessText 클래스 : 액세스 텍스트 사용하기 (0) | 2020.07.27 |
[C#/WPF] DrawingVisual 클래스 : 비주얼 자식 추가하기 (0) | 2020.07.27 |
[C#/WPF] 마우스 드래그로 사각형 그리기 (0) | 2020.07.22 |
[C#/WPF] HierarchicalDataTemplate 엘리먼트 사용하기 (0) | 2020.07.21 |
[C#/WPF] TreeView 엘리먼트 사용하기 (0) | 2020.07.21 |
[C#/WPF] TreeViewItem 클래스 : 트리 뷰 구하기 (0) | 2020.07.21 |
[C#/WPF] Behavior<T> 클래스 : 마우스 왼쪽 버튼 컨텍스트 메뉴 지원 동작 사용하기 (0) | 2020.07.21 |
[C#/WPF] Behavior<T> 클래스 : 리스트 박스 선택 동작 사용하기 (0) | 2020.07.21 |
댓글을 달아 주세요