这个程序用到了WPF里  “visual_Brush”(主要是为了实现分屏显示) , “UserControl” ,这两个知识点;

  在屏保状态下播放指定文件夹下的视频,而且能分屏显示;

  把编译好的屏保程序(原本是.exe)改名为.SCR 放到C:\Windos\System32下

   现在就可以设置为屏保了:譬如win7系统的 在桌面属性→→个性化设置里→→ 选择屏保程序→→ 这时候就能看到你写的屏保程序的名字了(切记改名.scr);

MainWindow.xaml:

  

<Window x:Class="visual_Brush.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://schemas.microsoft.com/surface/2008"
        Title="MainWindow" Height="1080" Width="3840"  WindowStyle="None" ResizeMode="NoResize"
        xmlns:local="clr-namespace:visual_Brush" WindowStartupLocation="Manual" Background="Black" Left="0" Top="0"
        MouseDown="Window_MouseDown" TouchDown="Window_TouchDown" Topmost="True">
    <Grid Height="1080" Width="3840" Name="grid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <local:UserControl_Video x:Name="Video" Height="1080" Width="1920" Grid.Column="0"></local:UserControl_Video>
        <Grid x:Name="ExtendedScreenGrid" Height="1080" Width="1920" Grid.Column="1" Background="Black" VerticalAlignment="Top" HorizontalAlignment="Right" >
            <Rectangle  StrokeThickness="0">
                <Rectangle.Fill>
                    <VisualBrush Visual="{Binding ElementName=Video}">
                    </VisualBrush>
                </Rectangle.Fill>
            </Rectangle>
        </Grid>
    </Grid>

</Window>

MainWindow.cs(后台代码)→)→)→)→)→)→)→)→)→)→)→)→

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
    }

UserControl_Video.xaml

<UserControl x:Class="visual_Brush.UserControl_Video"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" Loaded="UserControl_Loaded_1">
    <Grid x:Name="grid">
        <MediaElement Name="myMedia" LoadedBehavior="Play" MediaOpened="myMedia_MediaOpened">
        </MediaElement>
    </Grid>
</UserControl>

UserControl_Video.cs(后台代码)

public partial class UserControl_Video : UserControl
    {
        int i = 0;
        string[] carImageUris = Directory.GetFiles(@"D:\video");//指定文件夹目录

private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            myMedia.MediaEnded+=new RoutedEventHandler(myMedia_MediaEnded);
            myMedia.Source = new Uri(carImageUris[0], UriKind.Absolute);
        }

void myMedia_MediaOpened(object sender, RoutedEventArgs e)
        {
            myMedia.LoadedBehavior = MediaState.Manual;
            myMedia.Play();
        }

/// <summary>
        /// 当媒体结束时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         void myMedia_MediaEnded(object sender, RoutedEventArgs e)
         {
             if (i!= carImageUris.Length-1)
             {
                 i++;
                myMedia.Source = new Uri(carImageUris[i], UriKind.Absolute);
             }
           else
            {
                myMedia.Source = new Uri(carImageUris[0], UriKind.Absolute);
                i = 0;
            }
        }
        public UserControl_Video()
        {
            InitializeComponent();
            myMedia.Volume = 100;
            //myMedia.Play();
        }
       void mediaPlay(Object sender, EventArgs e)
       {
            myMedia.Play();
       }

}

  

04-30 03:24