2016-09-28 122 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