利用WPF实现Windows屏保的制作

 更新时间:2022年07月26日 10:42:51   作者:驚鏵  
屏保程序的本质上就是一个Win32 窗口应用程序。本文将利用WPF实现Windows屏保的制作,文中的示例代码简洁易懂,对我们学习WPF有一定帮助,感兴趣的可以了解一下

介绍

框架使用.NET452

Visual Studio 2019;

项目使用 MIT 开源许可协议;

更多效果可以通过GitHub[1]|码云[2]下载代码;

也可以自行添加天气信息等。

正文

屏保程序的本质上就是一个Win32 窗口应用程序;

把编译好一个窗口应用程序之后,把扩展名更改为 scr,于是你的屏幕保护程序就做好了;

选中修改好的 scr 程序上点击右键,可以看到一个 安装 选项,点击之后就安装了;

安装之后会立即看到我们的屏幕保护程序已经运行起来了;

处理屏幕保护程序参数如下

/s 屏幕保护程序开始,或者用户点击了 预览 按钮;

/c 用户点击了 设置按钮;

/p 用户选中屏保程序之后,在预览窗格中显示;

实现代码

1)MainWindow.xaml 代码如下;

<Window x:Class="ScreenSaver.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:drawing="http://www.microsoft.net/drawing"
        xmlns:local="clr-namespace:ScreenSaver"
        mc:Ignorable="d" WindowStyle="None"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="MainGrid">
        <drawing:PanningItems ItemsSource="{Binding stringCollection,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"
                              x:Name="MyPanningItems">
            <drawing:PanningItems.ItemTemplate>
                <DataTemplate>
                    <Rectangle>
                        <Rectangle.Fill>
                            <ImageBrush ImageSource="{Binding .}"/>
                        </Rectangle.Fill>
                    </Rectangle>
                </DataTemplate>
            </drawing:PanningItems.ItemTemplate>
        </drawing:PanningItems>
        <Grid  HorizontalAlignment="Center" 
               VerticalAlignment="Top"
                Margin="0,50,0,0">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="FontSize" Value="90"/>
                    <Setter Property="FontWeight" Value="Black"/>
                    <Setter Property="Foreground" Value="White"/>
                </Style>
            </Grid.Resources>
            <WrapPanel>
                <TextBlock Text="{Binding Hour,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"/>
                <TextBlock Text=":" x:Name="PART_TextBlock">
                    <TextBlock.Triggers>
                        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Duration="00:00:01"
                                                 From="1"
                                                 To="0"
                                                 Storyboard.TargetName="PART_TextBlock"
                                                 Storyboard.TargetProperty="Opacity"
                                                 RepeatBehavior="Forever"
                                                 FillBehavior="Stop"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </TextBlock.Triggers>
                </TextBlock>
                <TextBlock Text="{Binding Minute,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"/>
            </WrapPanel>
            <TextBlock Grid.Row="1" FontSize="45" HorizontalAlignment="Center" Text="{Binding Date,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"/>
        </Grid>
    </Grid>
</Window>

2) MainWindow.xaml.cs 代码如下;

当屏保启动后需要注意如下

  • 将鼠标设置为不可见Cursors.None;
  • 将窗体设置为最大化WindowState.Maximized;
  • WindowStyle设置为"None";
  • 注意监听鼠标按下和键盘按键则退出屏保;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace ScreenSaver
{
    /// <summary>
    ///     MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty stringCollectionProperty =
            DependencyProperty.Register("stringCollection", typeof(ObservableCollection<string>), typeof(MainWindow),
                new PropertyMetadata(null));

        public static readonly DependencyProperty HourProperty =
            DependencyProperty.Register("Hour", typeof(string), typeof(MainWindow), new PropertyMetadata(null));

        public static readonly DependencyProperty MinuteProperty =
            DependencyProperty.Register("Minute", typeof(string), typeof(MainWindow), new PropertyMetadata(null));

        public static readonly DependencyProperty SecondProperty =
            DependencyProperty.Register("Second", typeof(string), typeof(MainWindow), new PropertyMetadata(null));

        public static readonly DependencyProperty DateProperty =
            DependencyProperty.Register("Date", typeof(string), typeof(MainWindow), new PropertyMetadata());

        private readonly DispatcherTimer timer = new DispatcherTimer();

        public MainWindow()
        {
            InitializeComponent();
            Loaded += delegate
            {
                WindowState = WindowState.Maximized;
                Mouse.OverrideCursor = Cursors.None;
                var date = DateTime.Now;
                Hour = date.ToString("HH");
                Minute = date.ToString("mm");
                Date =
                    $"{date.Month} / {date.Day}   {CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek)}";
                stringCollection = new ObservableCollection<string>();
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
                var directoryInfo = new DirectoryInfo(path);
                foreach (var item in directoryInfo.GetFiles())
                {
                    if (Path.GetExtension(item.Name) != ".jpg") continue;
                    stringCollection.Add(item.FullName);
                }

                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += delegate
                {
                    date = DateTime.Now;
                    Hour = date.ToString("HH");
                    Minute = date.ToString("mm");
                    Date =
                        $"{date.Month} / {date.Day}   {CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek)}";
                };
                timer.Start();
            };
            MouseDown += delegate { Application.Current.Shutdown(); };
            KeyDown += delegate { Application.Current.Shutdown(); };
        }

        public ObservableCollection<string> stringCollection
        {
            get => (ObservableCollection<string>)GetValue(stringCollectionProperty);
            set => SetValue(stringCollectionProperty, value);
        }


        public string Hour
        {
            get => (string)GetValue(HourProperty);
            set => SetValue(HourProperty, value);
        }

        public string Minute
        {
            get => (string)GetValue(MinuteProperty);
            set => SetValue(MinuteProperty, value);
        }

        public string Second
        {
            get => (string)GetValue(SecondProperty);
            set => SetValue(SecondProperty, value);
        }


        public string Date
        {
            get => (string)GetValue(DateProperty);
            set => SetValue(DateProperty, value);
        }
    }
}

到此这篇关于利用WPF实现Windows屏保的制作的文章就介绍到这了,更多相关WPF制作Windows屏保内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 解析c#在未出现异常情况下查看当前调用堆栈的解决方法

    解析c#在未出现异常情况下查看当前调用堆栈的解决方法

    本篇文章是对c#在未出现异常情况下查看当前调用堆栈的解决方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C#实现XOR密码(异或密码)的示例代码

    C#实现XOR密码(异或密码)的示例代码

    XOR密码(异或密码)是一种简单的加密算法,它使用异或(XOR)操作来对明文和密钥进行加密和解密,本文为大家介绍了C#实现XOR密码的相关知识,希望对大家有所帮助
    2024-01-01
  • C# 在PDF文档中创建表格的实现方法

    C# 在PDF文档中创建表格的实现方法

    表格能够一目了然的让用户看到数据信息,使信息显得有条理化,那么在pdf类型的文档中如何来添加表格并对表格进行格式化操作呢?下面小编给大家带来了C# 在PDF文档中创建表格的实现方法,需要的朋友参考下吧
    2017-12-12
  • UGUI实现随意调整Text中的字体间距

    UGUI实现随意调整Text中的字体间距

    这篇文章主要为大家详细介绍了UGUI实现随意调整字体间距的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C#入门教程之集合ArrayList用法详解

    C#入门教程之集合ArrayList用法详解

    这篇文章主要介绍了C#入门教程之集合ArrayList用法,结合具体实例分析了C#中集合的概念、功能、创建与使用方法,需要的朋友可以参考下
    2017-06-06
  • 如何在C#中使用指针

    如何在C#中使用指针

    这篇文章主要介绍了如何在C#中使用指针,文中代码简单易懂,帮助大家更好的工作和学习,感兴趣的朋友快来了解下
    2020-06-06
  • C#中this的使用实例分析

    C#中this的使用实例分析

    这篇文章主要介绍了C#中this的使用方法,this是C#程序设计中非常重要的一个关键字,本文以实例形式对此作出简单的用法分析,需要的朋友可以参考下
    2014-08-08
  • C#使用foreach语句遍历队列(Queue)的方法

    C#使用foreach语句遍历队列(Queue)的方法

    这篇文章主要介绍了C#使用foreach语句遍历队列(Queue)的方法,涉及foreach语句的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#值类型、引用类型中的Equals和==的区别浅析

    C#值类型、引用类型中的Equals和==的区别浅析

    这篇文章主要介绍了C#值类型、引用类型中的Equals和==的区别浅析,本文分别对C#值类型和引用类型中的Equals和==做了讲解和给出了实例,需要的朋友可以参考下
    2015-01-01
  • 详解LINQ入门(上篇)

    详解LINQ入门(上篇)

    这篇文章主要介绍了详解LINQ入门(上篇),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论