반응형
<Window x:Class="WpfApp6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp6"
mc:Ignorable="d"
Width="400" Height="400"
MouseMove="Window_MouseMove" MouseUp="Window_MouseUp">
<Canvas Name="canvas">
<Rectangle Name="box" Width="50" Height="50" Fill="Red"
Canvas.Left="50" Canvas.Top="50"
MouseDown="Box_MouseDown" MouseMove="Box_MouseMove"/>
<Rectangle Name="handle" Width="10" Height="10" Fill="Blue"
Canvas.Left="75" Canvas.Top="40"
MouseDown="Handle_MouseDown" MouseMove="Handle_MouseMove"/>
</Canvas>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp6
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool isDragging;
private Point lastPosition;
public MainWindow()
{
InitializeComponent();
}
private void Box_MouseDown(object sender, MouseButtonEventArgs e)
{
isDragging = true;
lastPosition = e.GetPosition(canvas);
box.CaptureMouse();
}
private void Box_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point currentPosition = e.GetPosition(canvas);
double deltaX = currentPosition.X - lastPosition.X;
double deltaY = currentPosition.Y - lastPosition.Y;
double newLeft = Canvas.GetLeft(box) + deltaX;
double newTop = Canvas.GetTop(box) + deltaY;
Canvas.SetLeft(box, newLeft);
Canvas.SetTop(box, newTop);
Canvas.SetLeft(handle, Canvas.GetLeft(handle) + deltaX);
Canvas.SetTop(handle, Canvas.GetTop(handle) + deltaY);
lastPosition = currentPosition;
}
}
private void Handle_MouseDown(object sender, MouseButtonEventArgs e)
{
isDragging = true;
lastPosition = e.GetPosition(canvas);
handle.CaptureMouse();
}
private void Handle_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point currentPosition = e.GetPosition(canvas);
double deltaX = currentPosition.X - lastPosition.X;
double deltaY = currentPosition.Y - lastPosition.Y;
double newWidth = box.Width + deltaX;
double newHeight = box.Height + deltaY;
if (newHeight > 0)
{
box.Height = newHeight;
Canvas.SetTop(handle, Canvas.GetTop(handle) + deltaY);
}
if (newWidth > 0)
{
box.Width = newWidth;
Canvas.SetLeft(handle, Canvas.GetLeft(handle) + deltaX);
}
lastPosition = currentPosition;
}
}
private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
isDragging = false;
box.ReleaseMouseCapture();
handle.ReleaseMouseCapture();
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging && e.LeftButton == MouseButtonState.Released)
{
isDragging = false;
box.ReleaseMouseCapture();
handle.ReleaseMouseCapture();
}
}
}
}
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
C# Enum 타입을 리스트로 만드는 방법 (0) | 2023.03.16 |
---|---|
C# Tesseract를 이용한 OCR 검출 샘플 코드 (0) | 2023.03.14 |
Server Streaming with gRPC and .NET Core (0) | 2023.02.16 |
C# gRPC file download (0) | 2023.02.16 |
C# grpc file upload (0) | 2023.02.16 |