我试图根据鼠标位置移动按钮,但移动按钮时它会闪烁。请在下面找到代码,
在 XAML 的代码下方,
<Button Name="Samplebutton"
PreviewMouseDown="Samplebutton_PreviewMouseDown"
PreviewMouseUp="Samplebutton_PreviewMouseUp"
PreviewMouseMove="Samplebutton_PreviewMouseMove"
Content="Moving" Width="100" Height="35"/>
CS,
private bool m_IsPressed = false;
private void Samplebutton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
m_IsPressed = true;
}
else
{
m_IsPressed = false;
}
}
private void Samplebutton_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
m_IsPressed = false;
}
private void Samplebutton_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (m_IsPressed)
{
TranslateTransform transform = new TranslateTransform();
transform.X = Mouse.GetPosition(sender as Button).X;
transform.Y = Mouse.GetPosition(sender as Button).Y;
this.Samplebutton.RenderTransform = transform;
}
}
有人请提供您的建议吗?