WPF-鼠标移动控件

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 WpfApp2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

bool mouseStatus = false;
private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
var c = sender as Control;
_isMouseDown = true;
_mouseDownPosition = e.GetPosition(this);
var transform = c.RenderTransform as TranslateTransform;
if (transform == null)
{
transform = new TranslateTransform();
c.RenderTransform = transform;
}
_mouseDownControlPosition = new Point(transform.X, transform.Y);
c.CaptureMouse();
}

private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
var c = sender as Control;
_isMouseDown = false;
c.ReleaseMouseCapture();
}

private Point _mouseDownControlPosition;
private bool _isMouseDown;
private Point _mouseDownPosition;

private void Button_MouseMove(object sender, MouseEventArgs e)
{
if (_isMouseDown) {
var c = sender as Control;
var pos = e.GetPosition(this);
var dp = pos - _mouseDownPosition;
var transform = c.RenderTransform as TranslateTransform;
transform.X = _mouseDownControlPosition.X + dp.X;
transform.Y = _mouseDownControlPosition.Y + dp.Y;
}
}


}
}

posted @ 2022-01-17 11:39  Mrwangxs  阅读(226)  评论(0)    收藏  举报