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

1 篇文章 0 订阅

最近刚学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();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值