利用WPF实现Windows屏保的制作
介绍
框架使用.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屏保内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://www.cnblogs.com/yanjinhua/p/16519908.html
相关文章
- VPN可以虚拟出一个专用网络,让远处的计算机和你相当于处在同一个局域网中,而中间的数据也可以实现加密传输,用处很大,特别是在一些大公司,分公司处在不同的区域。...2016-01-27
- 这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)
这篇文章主要介绍了c# WPF中通过双击编辑DataGrid中Cell的示例(附源码),帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-03- 这篇文章主要为大家详细介绍了C#创建一个简单windows窗体应用的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了Powershell 查询 Windows 日志的方法,需要的朋友可以参考下...2020-06-30
- 最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟着小编学习WPF实现类似360安全卫士界面的程序源码分享...2020-06-25
- 这篇文章主要介绍了C#实现windows form限制文本框输入的方法,涉及C#限制文本框输入的技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
- 本文实例讲述了JS实现不使用图片仿Windows右键菜单效果代码。分享给大家供大家参考,具体如下:这里演示JS不使用图片仿Windows右键菜单效果,这款代码灵活使用了文鼎字,配合CSS和JS做出了这个和系统右键菜单很相似的东东。...2015-10-23
C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法
这篇文章主要介绍了C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法,结合实例形式分析了GetDesktopWindow函数用于获取窗口句柄的具体使用方法与相关注意事项,需要的朋友可以参考下...2020-06-25- 作为一个新生事物,Linux吸引了不少眼球,但是它能否快捷、方便地与Windows资源共享,是一个很重要的问题。大家知道,Windows之间可以利用“网络邻居”来实现资源共享,而Linux...2016-11-25
- 这篇文章主要介绍了利用C#修改Windows操作系统时间,帮助大家更好的利用c#操作系统,感兴趣的朋友可以了解下...2020-12-08
- 本篇文章主要介绍了详解Windows下安装Nodejs步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...2017-05-22
C#3.0使用EventLog类写Windows事件日志的方法
这篇文章主要介绍了C#3.0使用EventLog类写Windows事件日志的方法,以简单实例形式分析了C#写windows事件日志的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25- 这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了C# WPF 通过委托实现多窗口间的传值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
Windows Server 2012 R2或2016无法安装.NET Framework 3.5.1的解决方法
这篇文章主要为大家详细介绍了Windows Server 2012 R2或2016无法安装.NET Framework 3.5.1,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-07-06Windows Server 2016 上配置 APACHE+SSL+PHP+perl的教程详解
Windows Server 2016 上配置 APACHE+SSL+PHP+perl怎么配置?小编推荐了一篇介绍Windows Server 2016 上配置 APACHE+SSL+PHP+perl的教程,有需要的同学快来看看吧! ...2017-07-06- 这篇文章主要介绍了C#实现控制Windows系统关机、重启和注销的方法,涉及C#调用windows系统命令实现控制开机、关机等操作的技巧,非常简单实用,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了C#中WPF使用多线程调用窗体组件的方法,涉及C#中多线程的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25