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
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
bool _leftIsDragging;
bool _rightIsDragging;
bool _middleIsDragging;
Point _leftDragOffset;
Point _rightDragOffset;
Point _middleDragOffset;
TranslateTransform _pan;
ScaleTransform _zoom;
RotateTransform _turn;
public MainWindow()
{
InitializeComponent();
_pan = new TranslateTransform();
_zoom = new ScaleTransform(1.0,1.0,controlTarget.Height/2,controlTarget.Width/2);
_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 = controlTarget.Height/2;
_zoom.CenterY = controlTarget.Width / 2;
}
}
void controlTarget_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
var s = sender as UIElement;
s.CaptureMouse();
_leftDragOffset = e.GetPosition(this);
_leftIsDragging = true;
}
else
{
if (e.ChangedButton == MouseButton.Right)
{
var s = sender as UIElement;
s.CaptureMouse();
_rightDragOffset = e.GetPosition(this);
_rightIsDragging = true;
//Point center = e.GetPosition(s);
//_zoom.CenterX = center.X;
//_zoom.CenterY = center.Y;
}
}
}
void controlTarget_MouseMove(object sender, MouseEventArgs e)
{
if (_leftIsDragging)
{
var s = sender as UIElement;
Point p = e.GetPosition(this);
_pan.X += p.X - _leftDragOffset.X;
_pan.Y += p.Y - _leftDragOffset.Y;
_leftDragOffset = p;
}
else
{
if (_rightIsDragging)
{
var s = sender as UIElement;
Point p = e.GetPosition(this);
double delta = (_rightDragOffset.Y - p.Y) * 0.01;
_zoom.ScaleX += delta;
_zoom.ScaleY += delta;
_rightDragOffset = p;
}
}
}
void controlTarget_MouseUp(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
var s = sender as UIElement;
s.ReleaseMouseCapture();
_leftIsDragging = false;
}
if (e.ChangedButton == MouseButton.Right)
{
var s = sender as UIElement;
s.ReleaseMouseCapture();
_rightIsDragging = false;
}
}
}
}