Expression Blendでプレゼンツールを作ってみた。

毎回プレゼン用に、プロジェクトを作るのも面倒なので、今度はWPFで24風のプレゼンツールをつくってみた


Window1.xaml


<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Asuka.Window1"
x:Name="MainWindow"
Title="Asuka"
Width="640" Height="480" Background="#FF000000" MouseUp="MainWindow_MouseUp" />


Window1.xaml.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace Asuka
{
public partial class Window1
{
private string imageDirectoryPath = "./image";
private string soundFilePath = "./sound/ctu_ringtone1231.mid";

private List<Uri> imageUriList = new List<Uri>();
private List<Image> imageList = new List<Image>();

private TimeSpan TIME_SPAN = TimeSpan.FromSeconds(3);
private System.Windows.Threading.DispatcherTimer uiTimer;
private System.Windows.Threading.DispatcherTimer clockTimer;

private int displayedImageCount = 0;
private Canvas canvas;
private Label timerLabel;

private bool isStarted = false;

public Window1()
{
this.InitializeComponent();

DirectoryInfo imageDir = new DirectoryInfo(imageDirectoryPath);
foreach (FileInfo f in imageDir.GetFiles())
{
Uri uri = new Uri(f.FullName);
imageUriList.Add(uri);
}

//Windowsサイズの最大化
this.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
this.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
this.Top = 0;
this.Left = 0;
Application.Current.MainWindow.WindowState = WindowState.Maximized;
Application.Current.MainWindow.WindowStyle = WindowStyle.None;

canvas = new Canvas();
canvas.Height = this.Height;
canvas.Width = this.Width;
canvas.SetValue(Canvas.TopProperty, 0D);
canvas.SetValue(Canvas.LeftProperty, 0D);
this.AddChild(canvas);

timerLabel = new Label();
timerLabel.Content = DateTime.Now.ToString("HH:mm:ss");
timerLabel.FontSize = 80;
timerLabel.FontFamily = new FontFamily("7barP");
timerLabel.Foreground = Brushes.Yellow;
timerLabel.BorderBrush = Brushes.White;
//timerLabel.Opacity = 50;

canvas.Children.Add(timerLabel);
TranslateTransform trans = new TranslateTransform(this.Width / 2 - 136, this.Height /2 - 31);
timerLabel.RenderTransform = trans;

clockTimer = new System.Windows.Threading.DispatcherTimer();
clockTimer.Interval = TimeSpan.FromSeconds(0.1);
clockTimer.Tick += new EventHandler(clockTimer_Tick);
clockTimer.Start();

}
void clockTimer_Tick(object sender, EventArgs e)
{
timerLabel.Content = DateTime.Now.ToString("HH:mm:ss");
}

private void MainWindow_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (isStarted)
{
return;
}
isStarted = true;

//start the uitimer
uiTimer_Tick(null, null);
uiTimer = new System.Windows.Threading.DispatcherTimer();
uiTimer.Interval = TIME_SPAN;
uiTimer.Tick += new EventHandler(uiTimer_Tick);
uiTimer.Start();

MediaPlayer player = new MediaPlayer();
FileInfo soundFileInfo = new FileInfo(soundFilePath);
player.Open(new Uri(soundFileInfo.FullName));
player.Play();
System.Threading.Thread.Sleep(4000);
}

void uiTimer_Tick(object sender, EventArgs e)
{
if (displayedImageCount < imageUriList.Count)
{
if (displayedImageCount > 0)
{

TranslateTransform trans2 = new TranslateTransform(this.Width / 4,this.Height /4);
Image prevImage = imageList[displayedImageCount - 1];
prevImage.RenderTransform = trans2;

DoubleAnimation animationX = null;
DoubleAnimation animationY = null;
if (displayedImageCount % 4 == 1)
{
animationX = new DoubleAnimation(0, TIME_SPAN);
animationY = new DoubleAnimation(0, TIME_SPAN);
}
else if (displayedImageCount % 4 == 2)
{
animationX = new DoubleAnimation(this.Width / 2, TIME_SPAN);
animationY = new DoubleAnimation(0, TIME_SPAN);
}
else if (displayedImageCount % 4 == 3)
{
animationX = new DoubleAnimation(0, TIME_SPAN);
animationY = new DoubleAnimation(this.Height /2, TIME_SPAN);
}
else
{
animationX = new DoubleAnimation(this.Width /2, TIME_SPAN);
animationY = new DoubleAnimation(this.Height /2, TIME_SPAN);
}

trans2.BeginAnimation(TranslateTransform.XProperty, animationX);
trans2.BeginAnimation(TranslateTransform.YProperty, animationY);
}

//画像ロード
Image img = new Image();
img.Source = new BitmapImage(imageUriList[displayedImageCount]);
img.Width = this.Width / 2;
img.Height = this.Height / 2;
imageList.Add(img);

//canvas.Children.Add(img);
int index = canvas.Children.Count - 1;
canvas.Children.Insert(index ,img);
//中央に移動
TranslateTransform trans = new TranslateTransform(this.Width / 4, this.Height / 4);
img.RenderTransform = trans;

displayedImageCount++;
}

if (displayedImageCount == imageUriList.Count)
{
Button nextButton = new Button();
nextButton.Content = "NEXT";
nextButton.Click += nextButton_Click;
canvas.Children.Add(nextButton);

TranslateTransform trans = new TranslateTransform(this.Width -100, this.Height - 100);
nextButton.RenderTransform = trans;

uiTimer.Stop();
}
}

private void nextButton_Click(
Object sender,
RoutedEventArgs e
)
{

displayedImageCount = 0;
DispSlide();
}

private void DispSlide()
{
//表示内容の消去
canvas.Children.Clear();

//センターのスライドを表示
Image imgCenter = new Image();
imgCenter.Source = new BitmapImage(imageUriList[displayedImageCount]);
imgCenter.Width = (this.Width / 8) * 6;
imgCenter.Height = (this.Height / 8) * 6;
canvas.Children.Add(imgCenter);
//中央に移動
TranslateTransform trans = new TranslateTransform(this.Width / 8, this.Height / 8);
imgCenter.RenderTransform = trans;

if (displayedImageCount + 1 < imageUriList.Count)
{
//次ページのスライドを表示
Image imgNext = new Image();
imgNext.Source = new BitmapImage(imageUriList[displayedImageCount + 1]);
imgNext.Width = this.Width / 8;
imgNext.Height = (this.Height / 8) * 6;

canvas.Children.Add(imgNext);
TranslateTransform transNext = new TranslateTransform((this.Width / 8) * 7, this.Height / 8);
imgNext.RenderTransform = transNext;
imgNext.MouseUp += nextImage_MouseUp;
}

if (displayedImageCount > 0)
{
//前ページのスライドを表示
Image imgPrev = new Image();
imgPrev.Source = new BitmapImage(imageUriList[displayedImageCount - 1]);
imgPrev.Width = this.Width / 8;
imgPrev.Height = (this.Height / 8) * 6;

canvas.Children.Add(imgPrev);
TranslateTransform transPrev = new TranslateTransform(0, this.Height / 8);
imgPrev.RenderTransform = transPrev;
imgPrev.MouseUp += prevImage_MouseUp;
}

}

private void nextImage_MouseUp(
Object sender,
MouseButtonEventArgs e)
{
displayedImageCount++;
DispSlide();
}

private void prevImage_MouseUp(
Object sender,
MouseButtonEventArgs e)
{
displayedImageCount--;
DispSlide();
}
}
}