您的位置:首页 > 编程语言 > C#

屏幕长满美女照片,还带播放音乐哟。(屏幕长满玫瑰花)C# WPF

2013-12-18 15:42 405 查看
最近刚学WPF,又恰逢朋友过生日,没什么好送的,就编写了个这个东西,现在贴在这作为纪念。

代码还有些需要改进的地方,不过现在比较忙,就不去优化了额,能实现效果就好。

屏幕长满美女照片(可以随便放什么照片),还带播放音乐哟。

XAML:

<Window x:Class="BirthdayGift.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
MouseDoubleClick="Window_MouseDoubleClick" Height="300" Width="300"
AllowsTransparency="True"
WindowStyle="None" Background="#00FFFFFF" Loaded="Window_Loaded">
<Grid Name="gd">
</Grid>
</Window>


C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Media;

namespace BirthdayGift
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
MediaElement me = new MediaElement();
DispatcherTimer timer = new DispatcherTimer();//创建定时器对象
int screenWidth = (int)SystemParameters.PrimaryScreenWidth; //获取屏幕宽
int screenHeight = (int)SystemParameters.PrimaryScreenHeight;//获取屏幕高
int index = 1;

public MainWindow()
{
InitializeComponent();

this.WindowState = System.Windows.WindowState.Maximized;//最大化窗口
this.Height = screenHeight;
this.Width = screenWidth;

timer.Tick += new EventHandler(timer_Tick); //添加事件,定时到事件
timer.Interval = TimeSpan.FromMilliseconds(200);//设置定时长
timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
Random rdm = new Random(unchecked((int)DateTime.Now.Ticks)); //创建随机生成器对象

int height = rdm.Next(screenHeight);//产生小于screenHeight的数
int width = rdm.Next(screenWidth);
Image im = new Image();

index = rdm.Next(1, 22);  //照片数目21,随机显示一张照片
im.Source = new BitmapImage(new Uri(@"photoes/"+index+".jpg", UriKind.Relative));//给出照片路径
im.Height = 200;
im.Width = 200;
im.Stretch = Stretch.Fill;
//给出距离屏幕边界的距离
im.Margin = new Thickness(width,height,screenWidth-im.Width-width,screenHeight-im.Height-height);

gd.Children.Add(im);//添加图片到窗口

}
/// <summa
/// 双击左键退出窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{

me.LoadedBehavior = MediaState.Manual;  //设置为手动控制
me.UnloadedBehavior = MediaState.Manual;
me.Source = new Uri(@"E:\Gift\HappyBirthday.mp3", UriKind.RelativeOrAbsolute);

me.IsHitTestVisible = true;
me.MediaEnded += new  RoutedEventHandler(me_MediaEnded);
gd.Children.Add(me);
me.Play();
}
/// <summary>
/// 媒体播放完后自动重播
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void me_MediaEnded(object sender, RoutedEventArgs e)
{
me.Stop();
me.Play();
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: