底下的程式, 是具有動畫的神奇寶貝圖案

Eve.cs
using System;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace MahalPokemon
{
class Eve:Image
{
Thread t1;
bool runFlag = true;
int delay;
BitmapImage[] img;
public Eve(int delay)
{
this.delay = delay;
img = new BitmapImage[36];
for (int i = 0; i < img.Length; i++)
{
img[i] = new BitmapImage(new Uri(
String.Format("pack://application:,,,/Resources/frame_{0:00}.png", i+1)
));
}
t1 = new Thread(task);
t1.Start();
}
private void task()
{
int index = 0;
while (runFlag)
{
try
{
index = (index + 1) % 36;
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
this.Source = img[index];
}));
Thread.Sleep(delay);
}
catch (Exception e) { }
}
}
public void Stop()
{
runFlag = false;
t1.Interrupt();
}
}
}
MainWindow.xaml
<Window x:Class="MahalPokemon.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MahalPokemon"
mc:Ignorable="d"
Closed="Window_Closed"
Loaded="Window_Loaded"
Title="MainWindow" Height="450" Width="800" WindowState="Maximized">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFF406" Offset="0"/>
<GradientStop Color="#FF00F70B" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Canvas x:Name="canvas" Grid.Row="0"/>
<Button x:Name="btn" Click="btn_Click" Content="產生神奇寶貝" Grid.Row="1" Width="100"/>
<Label Content="超神奇的物件導向教學" Grid.Row="2" HorizontalContentAlignment="Center"/>
</Grid>
</Window>
MainWindow.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace MahalPokemon
{
public partial class MainWindow : Window
{
Random r;
Eve eve;
public MainWindow()
{
InitializeComponent();
r = new Random();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
int canvasWidth = (int)canvas.ActualWidth;
int canvasHeight = (int)canvas.ActualHeight;
eve = new Eve(r.Next(1,100)*10);
int wh = r.Next(100, 250);
//wh=300;
eve.Width = wh;
eve.Height = wh;
eve.SetValue(Canvas.LeftProperty, (double)(r.Next(1,canvasWidth-wh)));
eve.SetValue(Canvas.TopProperty, (double)(r.Next(1,canvasHeight-wh)));
canvas.Children.Add(eve);
}
private void Window_Closed(object sender, EventArgs e)
{
for (int i=0;i< canvas.Children.Count; i++)
{
Eve eve = canvas.Children[i] as Eve;
eve.Stop();
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
/*
int wh = 100;
int canvasWidth = (int)canvas.ActualWidth;
int canvasHeight = (int)canvas.ActualHeight;
int x = canvasWidth / 100;
int y = canvasHeight / 100;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
eve = new Eve(r.Next(1, 10) * 50);
eve.Width = wh;
eve.Height = wh;
eve.SetValue(Canvas.LeftProperty, (double)(i * wh));
eve.SetValue(Canvas.TopProperty, (double)(j * wh));
canvas.Children.Add(eve);
}
}
*/
}
}
}
