using System; using System.Collections.Generic; using System.Linq; using System.Text; 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 WpfApplication1 { /// /// MainWindow.xaml の相互作用ロジック /// public partial class MainWindow : Window { Point _leftDragOffset; Point _rightDragOffset; Point _middleDragOffset; Point LPod = new Point(); Point RPod = new Point(); Point RHi = new Point(); Point Rect = new Point(); Point LastP = new Point(); TranslateTransform _pan; ScaleTransform _zoom; RotateTransform _turn; public MainWindow() { InitializeComponent(); _pan = new TranslateTransform(); _zoom = new ScaleTransform(); _turn = new RotateTransform(); TransformGroup tGroup = new TransformGroup(); tGroup.Children.Add(_zoom); tGroup.Children.Add(_turn); tGroup.Children.Add(_pan); controlTarget.RenderTransform = tGroup; controlTarget.MouseDown += new MouseButtonEventHandler(controlTarget_MouseDown); controlTarget.MouseMove += new MouseEventHandler(controlTarget_MouseMove); controlTarget.MouseUp += new MouseButtonEventHandler(controlTarget_MouseUp); this.KeyDown += new KeyEventHandler(MainWindow_KeyDown); } void MainWindow_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { _pan.X = 0; _pan.Y = 0; _zoom.ScaleX = 1; _zoom.ScaleY = 1; _zoom.CenterX = 0; _zoom.CenterY = 0; } } void controlTarget_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) { var s = sender as UIElement; s.CaptureMouse(); _leftDragOffset = e.GetPosition(this); LPod.X = Canvas.GetLeft(s) -_leftDragOffset.X; LPod.Y = Canvas.GetTop(s) -_leftDragOffset.Y; } if (e.ChangedButton == MouseButton.Right) { var s = sender as UIElement; s.CaptureMouse(); _rightDragOffset = e.GetPosition(this); RPod.X = Canvas.GetLeft(s) -_leftDragOffset.X; RPod.Y = Canvas.GetTop(s) -_leftDragOffset.Y; Point Pos = e.GetPosition(s); RHi.X = (Pos.X / controlTarget.Width); RHi.Y = (Pos.Y / controlTarget.Height); Rect = new Point(controlTarget.Width, controlTarget.Height); } } void controlTarget_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { var s = sender as UIElement; Point p = e.GetPosition(this); double X = _leftDragOffset.X + (p.X - _leftDragOffset.X) + LPod.X; double Y = _leftDragOffset.Y + (p.Y - _leftDragOffset.Y) + LPod.Y; Canvas.SetLeft(s, X); Canvas.SetTop(s, Y); } if (e.RightButton == MouseButtonState.Pressed) { //if (LastP == e.GetPosition(this)) return; LastP = e.GetPosition(this); var s = sender as UIElement; Point p = e.GetPosition(this); //Point WP = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));//マウスドラッグしながらウインドウをリサイズする鬼畜はいないだろう・・・。 //Size WS = new Size(this.Width, this.Height); //Point RP = new Point(Canvas.GetLeft(controlTarget), Canvas.GetTop(controlTarget)); Size RS = new Size(controlTarget.Width,controlTarget.Height); double MouseW = p.X-_rightDragOffset.X; double MouseH = p.Y-_rightDragOffset.Y; double Recio = 0.005; Rect.X += (MouseW * Recio); Rect.Y += (MouseH * Recio); if (MouseW != 0 && Rect.Y>= 0) controlTarget.Width = Rect.Y; if (MouseH != 0 && Rect.Y >= 0) controlTarget.Height = Rect.Y; Size RS2 = new Size(controlTarget.Width, controlTarget.Height); double DW = RS2.Width - RS.Width; double DH = RS2.Height - RS.Height; double PX = (-RS.Width/2); double PY = (-RS.Width/2); Canvas.SetLeft(controlTarget, PX); Canvas.SetTop(controlTarget, PY); Point RP2 = new Point(Canvas.GetLeft(controlTarget), Canvas.GetTop(controlTarget)); PX = RP2.X + _rightDragOffset.X-((RHi.X*RS2.Width)-(RS.Width/2)); PY = RP2.Y + _rightDragOffset.Y-((RHi.Y*RS2.Height)-(RS.Height/2)); Canvas.SetLeft(controlTarget, (PX-(DW/2))); Canvas.SetTop(controlTarget, (PY -(DH/2))); } } void controlTarget_MouseUp(object sender, MouseButtonEventArgs e) { if(e.RightButton == MouseButtonState.Released && e.LeftButton == MouseButtonState.Released){ var s = sender as UIElement; s.ReleaseMouseCapture(); } } } }