Feedback

WPF Window per Maus platzieren, solange Taste gedrückt wird

Sprache: C#

Positioniert ein Window Objekt, solange die linke Maustaste gedrückt wird.
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;

    /// <summary>
    /// Positioniert ein Window neu, anhand der Mauskoordinaten
    /// </summary>
    internal class MouseDragWindow
    {
        public MouseDragWindow(Window wnd)
        {
            window = wnd;
        }

        private Window window;
        private Point leftMouseLocation = new Point();
        private Point wndLocation = new Point();

        public Point CurrentMouseLocation
        {
            get
            {
                var pos = System.Windows.Forms.Control.MousePosition;
                return new Point(pos.X, pos.Y);
            }
        }
        public bool IsMouseLeftPressed
        {
            get
            {
                return Mouse.LeftButton == MouseButtonState.Pressed;
            }
        }

        /// <summary>
        /// Positioniert das Fenster, solange die linke Maustaste gehalten wird
        /// </summary>
        /// <param name="wnd">Das Fenster, welches bewegt werden soll</param>
        public static void DragWhileMouseLeftDown(Window wnd)
        {
            new MouseDragWindow(wnd).DragWhileMouseLeftDown();
        }

        /// <summary>
        /// Bewegt das instanzierte Fenster solange die linke Maustaste gedrückt wird
        /// </summary>
        public void DragWhileMouseLeftDown()
        {
            leftMouseLocation = CurrentMouseLocation;
            wndLocation = new Point(window.Left, window.Top);

            var bgW = new BackgroundWorker();
            bool isPressed = true;

            bgW.DoWork += (s, ev) =>
            {
                window.Dispatcher.Invoke(new Action(() =>
                {
                    isPressed = IsMouseLeftPressed;

                    // Versetzte Bewegung der Mouse
                    var currentMousePosition = CurrentMouseLocation;

                    var oldLocationNewLocation = wndLocation;
                    oldLocationNewLocation.Offset(currentMousePosition.X - leftMouseLocation.X, currentMousePosition.Y - leftMouseLocation.Y);

                    window.Left = oldLocationNewLocation.X;
                    window.Top = oldLocationNewLocation.Y;

                }));

            };

            bgW.RunWorkerCompleted += (s, ev) =>
            {
                if (isPressed)
                {
                    bgW.RunWorkerAsync();
                    return;
                }

                bgW.Dispose();
                bgW = null;
            };

            bgW.RunWorkerAsync();

        }
    }


Aufruf zum Beispiel:

private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     MouseDragWindow.DragWhileMouseLeftDown(this);
}
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;

    /// <summary>
    /// Positioniert ein Window neu, anhand der Mauskoordinaten
    /// </summary>
    internal class MouseDragWindow
    {
        public MouseDragWindow(Window wnd)
        {
            window = wnd;
        }

        private Window window;
        private Point leftMouseLocation = new Point();
        private Point wndLocation = new Point();

        public Point CurrentMouseLocation
        {
            get
            {
                var pos = System.Windows.Forms.Control.MousePosition;
                return new Point(pos.X, pos.Y);
            }
        }
        public bool IsMouseLeftPressed
        {
            get
            {
                return Mouse.LeftButton == MouseButtonState.Pressed;
            }
        }

        /// <summary>
        /// Positioniert das Fenster, solange die linke Maustaste gehalten wird
        /// </summary>
        /// <param name="wnd">Das Fenster, welches bewegt werden soll</param>
        public static void DragWhileMouseLeftDown(Window wnd)
        {
            new MouseDragWindow(wnd).DragWhileMouseLeftDown();
        }

        /// <summary>
        /// Bewegt das instanzierte Fenster solange die linke Maustaste gedrückt wird
        /// </summary>
        public void DragWhileMouseLeftDown()
        {
            leftMouseLocation = CurrentMouseLocation;
            wndLocation = new Point(window.Left, window.Top);

            var bgW = new BackgroundWorker();
            bool isPressed = true;

            bgW.DoWork += (s, ev) =>
            {
                window.Dispatcher.Invoke(new Action(() =>
                {
                    isPressed = IsMouseLeftPressed;

                    // Versetzte Bewegung der Mouse
                    var currentMousePosition = CurrentMouseLocation;

                    var oldLocationNewLocation = wndLocation;
                    oldLocationNewLocation.Offset(currentMousePosition.X - leftMouseLocation.X, currentMousePosition.Y - leftMouseLocation.Y);

                    window.Left = oldLocationNewLocation.X;
                    window.Top = oldLocationNewLocation.Y;

                }));

            };

            bgW.RunWorkerCompleted += (s, ev) =>
            {
                if (isPressed)
                {
                    bgW.RunWorkerAsync();
                    return;
                }

                bgW.Dispose();
                bgW = null;
            };

            bgW.RunWorkerAsync();

        }
    }


Aufruf zum Beispiel:

private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     MouseDragWindow.DragWhileMouseLeftDown(this);
}

1 Kommentar

  1. Warum so kompliziert ?
    Geht auch viel einfacher:

    [code]public partial class MyWindow
    {
    public MyWindow()
    {
    InitializeComponent();

    this.MouseLeftButtonDown += MyWindowMouseLeftButtonDown;
    }

    void MyWindowMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
    this.DragMove();
    }
    }[/code]