public partial class MainWindow : Window { readonly TranslateTransform _translator = new TranslateTransform(); readonly Canvas _canvas = new Canvas(); public MainWindow() { InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded); WindowState = System.Windows.WindowState.Maximized; _canvas.RenderTransform = _translator; mainGrid.Children.Add(_canvas); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { Ellipse ellipse; for (int x = 0; x < 40; x++) { for (int y = 0; y < 100; y++) { ellipse = new Ellipse(); ellipse.Stroke = Brushes.Black; ellipse.StrokeThickness = 1; ellipse.Fill = Brushes.Green; ellipse.Width = ellipse.Height = 10; Canvas.SetLeft(ellipse, y * 15); Canvas.SetTop(ellipse, x * 15); _canvas.Children.Add(ellipse); } } } Point _dragPoint; protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e) { _dragPoint = e.GetPosition(this); CaptureMouse(); base.OnMouseDown(e); } protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e) { if (IsMouseCaptured) { Point newPosition = e.GetPosition(this); Vector dragVector = newPosition - _dragPoint; _dragPoint = newPosition; _translator.X += dragVector.X; _translator.Y += dragVector.Y; } base.OnMouseMove(e); } protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e) { ReleaseMouseCapture(); base.OnMouseUp(e); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)