<Window x:Class="ButtonSpeedTest.Window1"
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="627" Width="580">
<Canvas Background="Wheat" MouseMove="MyMouseMove">
<Button Canvas.Left="30" Canvas.Top="30" Height="37" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="66">Button</Button>
<Rectangle MouseDown="MyMouseDown" MouseUp="MyMouseUp" MouseMove="MyMouseMove" Name="rect" Canvas.Left="100" Canvas.Top="100" Fill="Red" Stroke="Black" Width="70" Height="50" />
<TextBox Canvas.Left="246" Canvas.Top="37.52" Height="23" Name="textBox1" Width="120" />
</Canvas>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ButtonSpeedTest
{
public partial class Window1 : Window
{
bool isDragging;
public Window1()
{
InitializeComponent();
}
private void MyMouseDown(object sender, MouseButtonEventArgs e)
{
isDragging = true;
}
private void MyMouseUp(object sender, MouseButtonEventArgs e)
{
isDragging = false;
}
private void MyMouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point position = e.GetPosition(this);
rect.SetValue(Canvas.LeftProperty, position.X - rect.Width / 2);
rect.SetValue(Canvas.TopProperty, position.Y - rect.Height / 4);
}
}
}
}