授業の暇ネタとして、写真ボードアプリを作る。
演習を始めて、作業の進みに差が出てきたので、先に進んだ学生の暇つぶしネタとして壁に写真を貼った感じで表示するアプリを作ってみた。
フォルダ選択が出来てないのと、ウィンドウサイズが小さいと写真の枠がうまく表示されないというところがあるが、その他はうまく動いている感じ。ビヘイビアで写真をすいっと動かさせたいが、環境に差があるため、Visual Studioだけで出来るものにした。
貴重な情報をありがとうございました。
XAMLでズルいデザイン~その3・ズルい背景~ - SourceChord
Subtle Patterns | Free textures for your next web project.
SQパズルに見る開発テクニックの面白さ - 高橋 忍のブログ - Site Home - MSDN Blogs
[XAML/C#] WPF でタイマーを使うには (Windows フォームから WPF へ)
How to: Call an Event Handler in Visual Basic
VB.NET Rnd関数 マイナスの発生させ方 - VB.NETでRnd関数を用いてマイナスの値... - Yahoo!知恵袋
<Window x:Class="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>
<Grid.Background>
<ImageBrush ImageSource="Images/purty_wood.png"
Stretch="None"
TileMode="Tile"
Viewport="0,0,512,512"
ViewportUnits="Absolute" />
</Grid.Background>
<WrapPanel Name="WrapPanel1">
<MediaElement Name="MediaElement1"/>
</WrapPanel>
</Grid>
</Window>
Imports System.Windows.Threading
Imports System.IO
Class MainWindow
Dim queue As New Queue(Of String)
Dim timer As DispatcherTimer
Dim random As New Random()
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim files As String() = System.IO.Directory.GetFiles(
"C:\Users\XXXX\Pictures\family", "*", System.IO.SearchOption.AllDirectories)
For Each file As String In files
queue.Enqueue(file)
Next
AddMedia(queue.Dequeue())
AddMedia(queue.Dequeue())
AddMedia(queue.Dequeue())
AddMedia(queue.Dequeue())
timer = New DispatcherTimer(DispatcherPriority.Normal)
timer.Interval = New TimeSpan(0, 0, 10)
AddHandler timer.Tick, AddressOf dispatcherTimer_Tick
timer.Start()
End Sub
Private Sub dispatcherTimer_Tick(sender As Object, e As EventArgs)
If WrapPanel1.Children.Count > 0 Then
WrapPanel1.Children.RemoveAt(0)
End If
If queue.Count > 0 Then
AddMedia(queue.Dequeue())
End If
End Sub
Private Sub AddMedia(str As String)
If ".jpg".Equals(Path.GetExtension(str)) _
OrElse ".JPG".Equals(Path.GetExtension(str)) _
OrElse ".bmp".Equals(Path.GetExtension(str)) Then
AddPicture(str)
Else
AddMovie(str)
End If
End Sub
Private Sub AddPicture(str As String)
Dim bitmap As BitmapImage
bitmap = New BitmapImage(New Uri(str))
'portrait or statue
Dim image As New Image()
image.Source = bitmap
If bitmap.Width > bitmap.Height Then
image.Width = 500
Else
image.Width = 300
End If
'Stretch
image.Stretch = Stretch.Uniform
SetBorder(image)
End Sub
Private Sub AddMovie(str As String)
Dim media As New MediaElement()
media.Source = New Uri(str)
'portrait or statue
If media.ActualWidth > media.ActualHeight Then
media.Width = 500
Else
media.Width = 300
End If
'Stretch
media.Stretch = Stretch.Uniform
SetBorder(media)
End Sub
Private Sub SetBorder(elem As UIElement)
'border
Dim border As New Border()
border.Effect = New Effects.DropShadowEffect()
border.Child = elem
border.BorderBrush = Brushes.White
border.BorderThickness = New Thickness(8)
border.Margin = New Thickness(50, 30, 20, 20)
border.HorizontalAlignment = Windows.HorizontalAlignment.Left
border.VerticalAlignment = Windows.VerticalAlignment.Top
'roatation
Dim rotation As New RotateTransform()
rotation.Angle = random.Next(31) - 15
border.RenderTransform = rotation
WrapPanel1.Children.Add(border)
End Sub
End Class