using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace DrapDropDemo { public partial class MainWindow : Window { private bool isDragging; public MainWindow() { InitializeComponent(); var rect = new Rectangle { Width = 100, Height = 100, Fill = new SolidColorBrush(Colors.Chartreuse) }; rect.MouseLeftButtonDown += rect_MouseLeftButtonDown; rect.MouseLeftButtonUp += rect_MouseLeftButtonUp; rect.MouseMove += rect_MouseMove; var canvas = new Canvas(); canvas.Children.Add(rect); Content = canvas; } private void rect_MouseMove(object sender, MouseEventArgs e) { if (!isDragging) return; var rect = sender as Rectangle; rect.SetValue(Canvas.LeftProperty, e.GetPosition(rect.Parent as Canvas).X - rect.ActualWidth/2); rect.SetValue(Canvas.TopProperty, e.GetPosition(rect.Parent as Canvas).Y - rect.ActualHeight/2); } private void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { isDragging = false; (sender as Rectangle).ReleaseMouseCapture(); } private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { isDragging = true; (sender as Rectangle).CaptureMouse(); } } }
2013年1月6日日曜日
Drap and Drop Shapes in WPF
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿