2016-09-28 117 views
2

我正在使用C#和Visual Studio做一个WPF应用程序,当鼠标悬停在窗口上时,我需要一个按钮来跳转到窗口中的随机位置。当你将鼠标悬停在其上时,我需要同样的事情发生。我也不需要动画,只是为了跳到那里。C#WPF让一个按钮随机移动当我捣乱

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void butNo_MouseEnter(object sender, MouseEventArgs e) 
    { 

    } 
} 

我试过,但它没有工作,也是一个动画:

<Button.Triggers> 
      <EventTrigger RoutedEvent="Button.Click"> 
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimation From="0" To="100" Duration="0:0:2" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False" /> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Button.Triggers> 

任何帮助将不胜感激,谢谢。

编辑:

试过,但 '位置' 带有错误代码CS1061在VS.

private void butNo_MouseEnter(object sender, MouseEventArgs e) 
    { 
     Random x = new Random(); 
     Point pt = new Point(int.Parse(x.Next(200).ToString()), int.Parse(x.Next(250).ToString())); 
     butNo.Location = pt; 
    } 
+0

你试图移动按钮的地方在哪里? – RomCoo

+0

出于好奇,为什么? 另外,您可以更改MouseEnter事件上按钮的边距吗? –

+0

我试图移动按钮的部分位于第二个代码片段中。我在这个网站的其他地方找到了。 – shootstuff007

回答

0

我真的不喜欢为人做功课的问题,但我很无聊,所以在这里你走了。

您只需使用保证金并将左值和上值更新为网格高度/宽度内的随机数,以便它不会超出视图范围。

此外,请务必使用IsTabStop =“False”否则,您可以选中它并仍然单击它。

因此,使用代码隐藏最简单的例子:

XAML:

<Window x:Class="mousemove.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid x:Name="Grid1"> 
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="0,0,0,0" Name="button1" Width="75" MouseEnter="button1_MouseEnter" IsTabStop="False"/> 
</Grid> 

MainWindow.xaml.cs

private void button1_MouseEnter(object sender, MouseEventArgs e) 
{ 
    int maxLeft = Convert.ToInt32(Grid1.ActualWidth - button1.Width); 
    int maxTop = Convert.ToInt32(Grid1.ActualHeight - button1.Height); 
    Random rand = new Random(); 
    button1.Margin = new Thickness(rand.Next(maxLeft), rand.Next(maxTop), 0, 0); 
} 
+0

我们是否需要重新初始化每个button1_MouseEnter事件的Random对象?否则我们可以在页面加载时公开初始化同一个对象。如果我错了,请纠正我。 –

+0

'Canvas'会更适合这个问题,因为你可以使用'Canvas.SetLeft'和'Canvas.SetTop'方法来代替使用'Margin'。 –

+0

绝对有效的方法来做到这一点。由于OP显然对C#没有深入的了解,我只是尽可能使用这些代码,只需很少的代码就可以处理新项目。因为新项目使用Grid,所以我为此提出了解决方案。 – TrialAndError

0

下面是一个使用Canvas和动画解决方案。

<Canvas Name="cnv"> 
    <Button Name="btn" 
      Content="Click Me!" 
      IsTabStop="False" 
      Canvas.Left="0" 
      Canvas.Top="0" 
      MouseEnter="Button_MouseEnter"/> 
</Canvas> 

和代码隐藏:

Random rnd = new Random(); 

private void Button_MouseEnter(object sender, MouseEventArgs e) 
{ 
    //Work out where the button is going to move to. 
    double newLeft = rnd.Next(Convert.ToInt32(cnv.ActualWidth - btn.ActualWidth)); 
    double newTop = rnd.Next(Convert.ToInt32(cnv.ActualHeight - btn.ActualHeight)); 

    //Create the animations for left and top 
    DoubleAnimation animLeft = new DoubleAnimation(Canvas.GetLeft(btn), newLeft, new Duration(TimeSpan.FromSeconds(1))); 
    DoubleAnimation animTop = new DoubleAnimation(Canvas.GetTop(btn), newTop, new Duration(TimeSpan.FromSeconds(1))); 

    //Set an easing function so the button will quickly move away, then slow down 
    //as it reaches its destination. 
    animLeft.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut }; 
    animTop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut }; 

    //Start the animation. 
    btn.BeginAnimation(Canvas.LeftProperty, animLeft, HandoffBehavior.SnapshotAndReplace); 
    btn.BeginAnimation(Canvas.TopProperty, animTop, HandoffBehavior.SnapshotAndReplace); 
} 

这里是它在行动gif