根据鼠标位置移动 wpf 按钮但它闪烁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17295174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:04:32  来源:igfitidea点击:

Moving the wpf button based on mouse position but it blinking

c#wpfsilverlightwpf-controls

提问by Muthalagu

I tried to move the button based on the mouse position but it blinking when the button is moved. Please find the code below,

我试图根据鼠标位置移动按钮,但在移动按钮时它会闪烁。请找到下面的代码,

Below the code for XAML,

在 XAML 代码下方,

        <Button Name="Samplebutton"
                PreviewMouseDown="Samplebutton_PreviewMouseDown"
                PreviewMouseUp="Samplebutton_PreviewMouseUp"
                PreviewMouseMove="Samplebutton_PreviewMouseMove"
                Content="Moving" Width="100" Height="35"/>

CS,

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;
        }
    }

Any one please provide your suggestions?

任何人请提供您的建议?

采纳答案by Chris

Your "flickering" is caused by the transform in your PreviewMouseMovehandler.

您的“闪烁”是由PreviewMouseMove处理程序中的转换引起的。

You are transforming the X and Y positions of the control, using the position of the mouse relative to your Button control, which will change every time your transform is applied. Every time the button position changes, your Mouse.GetPosition(sender as Button).X& Ywill return different values, causing the button to change position again, and so on.

您正在转换控件的 X 和 Y 位置,使用鼠标相对于 Button 控件的位置,每次应用转换时都会更改。每次按钮位置改变时,你的Mouse.GetPosition(sender as Button).X&Y将返回不同的值,导致按钮再次改变位置,依此类推。

One way around this would be to get the mouse position from a parent element (e.g. Grid, Canvaswith a fixed position):

解决此问题的一种方法是从父元素获取鼠标位置(例如GridCanvas具有固定位置):

<Grid Name="myGrid">
    <Button Name="Samplebutton"
            PreviewMouseDown="Samplebutton_PreviewMouseDown"
            PreviewMouseUp="Samplebutton_PreviewMouseUp"
            PreviewMouseMove="Samplebutton_PreviewMouseMove"
            Content="Moving" Width="100" Height="35"/>
</Grid>

and

private void Samplebutton_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (m_IsPressed)
        {
            TranslateTransform transform = new TranslateTransform();
            transform.X = Mouse.GetPosition(myGrid).X;
            transform.Y = Mouse.GetPosition(myGrid).Y;
            this.Samplebutton.RenderTransform = transform;
        }
    }

This won't be the prettiest thing in the world, as it will "grab" the button from the top left, but you can adapt the position to obtain your desired behaviour.

这不会是世界上最漂亮的事情,因为它会从左上角“抓住”按钮,但您可以调整位置以获得所需的行为。

Good luck.

祝你好运。

Edit: If you don't want to explicitly name the element you wish to use for the relative position, you should be able to select the immediate parent by querying the senderobject:

编辑:如果您不想显式命名要用于相对位置的元素,您应该能够通过查询sender对象来选择直接父级:

transform.X = Mouse.GetPosition((sender as Button).Parent as FrameworkElement).X;

回答by Shivam cv

 private void button_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
    {
        button.CaptureMouse();
    }

    private void button_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
    {
        button.ReleaseMouseCapture();
    }

    private void button_PreviewMouseMove_1(object sender, MouseEventArgs e)
    {

        if (button.IsMouseCaptured)
        {
            Canvas.SetLeft(button, e.GetPosition(this).X);
            Canvas.SetTop(button, e.GetPosition(this).Y);

        }
    }