WPF mouse move via mosuedown,mousemove and mouseup

//xaml
<Window x:Class="WpfApp43.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        WindowState="Maximized"
        MouseDown="Window_MouseDown"
        MouseMove="Window_MouseMove"
        MouseUp="Window_MouseUp"
        Closing="Window_Closing"
        xmlns:local="clr-namespace:WpfApp43"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="150"/>
        </Grid.ColumnDefinitions>
        <Canvas x:Name="cvs" Grid.Column="0"/>
        <ListBox x:Name="lbx" Grid.Column="1"/>
    </Grid>
</Window>







//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp43
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isMoving=false;
        private bool isCaptured = false;
        private int copyIdx = 0;
        private double limitedWidth, limitedHeiht;
        private int locationIdx = 0;
        private Ellipse elp { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            limitedHeiht = cvs.ActualHeight - 50;
            limitedWidth = cvs.ActualWidth - 50;
            InitCanvas();
        }

        private void InitCanvas()
        {
            elp = new Ellipse();
            elp.Width = 100;
            elp.Height = 100;
            elp.Stroke = new SolidColorBrush(Colors.Black);
            elp.StrokeThickness = 5;
            elp.Fill = new SolidColorBrush(Colors.Red);
            if(!cvs.Children.Contains(elp))
            {
                cvs.Children.Add(elp);
            }
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var pt = e.GetPosition(cvs);
            if(e.LeftButton == MouseButtonState.Pressed)
            {
                isCaptured = Mouse.Capture(elp);
                if(isCaptured)
                {
                    elp.StrokeThickness = 10;
                }
            }
            e.Handled = true;
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            var pt = e.GetPosition(cvs);
            if(e.LeftButton==MouseButtonState.Pressed && isCaptured &&
                pt.Y < limitedHeiht && pt.X < limitedWidth)
            {
                isMoving = true;
                LogEllipseLocation(pt);
                Canvas.SetLeft(elp, pt.X);
                Canvas.SetTop(elp, pt.Y);
            }
            e.Handled = true;
        }

        private void LogEllipseLocation(Point pt)
        {
            Application.Current.Dispatcher.InvokeAsync(() =>
            {
                lbx.Items.Add($"{++locationIdx},({pt.X},{pt.Y})");
                lbx.ScrollIntoView(lbx.Items[lbx.Items.Count - 1]);
            });
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var msgBoxResult=MessageBox.Show("Are you sure to exit?","Exit",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question,
                MessageBoxResult.Yes);
            if(msgBoxResult == MessageBoxResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true ;
            }
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            var pt = Mouse.GetPosition(cvs);
            if (isMoving &&  e.LeftButton == MouseButtonState.Released && 
                pt.X>50 && pt.Y>50 && pt.Y<limitedHeiht && pt.X<limitedWidth)
            {
                Canvas.SetLeft(elp,pt.X);
                Canvas.SetTop(elp,pt.Y);                
                isCaptured = false;
            }
            elp.StrokeThickness = 5;
            isMoving=false;
            isCaptured=false;
            e.Handled = true;
            elp.ReleaseMouseCapture();
        }


    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2024-11-24 16:56  FredGrit  阅读(3)  评论(0编辑  收藏  举报