WPF 制作 Windows 屏保
分享如何使用WPF 制作 Windows 屏保WPF 制作 Windows 屏保
作者:驚鏵
原文链接:https://github.com/yanjinhuagood/ScreenSaver
[*]框架使用.NET452;
[*]Visual Studio 2019;
[*]项目使用 MIT 开源许可协议;
[*]更多效果可以通过GitHub|码云下载代码;
[*]也可以自行添加天气信息等。
正文
[*]屏保程序的本质上就是一个 Win32 窗口应用程序;
[*]把编译好一个窗口应用程序之后,把扩展名更改为 scr,于是你的屏幕保护程序就做好了;https://files.mdnice.com/user/24276/2aaa7686-7b18-4974-bcd4-a148e187e8bf.png
[*]选中修改好的 scr 程序上点击右键,可以看到一个 安装 选项,点击之后就安装了;https://files.mdnice.com/user/24276/a040c878-645c-48b2-9ca1-8eef94b075e2.png
[*]安装之后会立即看到我们的屏幕保护程序已经运行起来了;
https://files.mdnice.com/user/24276/f9fa820c-a836-4703-8944-4ec7db2e25ad.png
[*]处理屏幕保护程序参数如下
[*]/s 屏幕保护程序开始,或者用户点击了 预览 按钮;
[*]/c 用户点击了 设置按钮;
[*]/p 用户选中屏保程序之后,在预览窗格中显示;https://files.mdnice.com/user/24276/585cc6ab-6ec0-4f64-85b8-41d1b0935e24.png
1)MainWindow.xaml 代码如下;
<Window x:Class="ScreenSaver.MainWindow"<br> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br> xmlns:d="http://schemas.microsoft.com/expression/blend/2008"<br> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"<br> xmlns:system="clr-namespace:System;assembly=mscorlib"<br> xmlns:drawing="http://www.microsoft.net/drawing"<br> xmlns:local="clr-namespace:ScreenSaver"<br> mc:Ignorable="d" WindowStyle="None"<br> Title="MainWindow" Height="450" Width="800"><br> <Grid x:Name="MainGrid"><br> <drawing:PanningItems ItemsSource="{Binding stringCollection,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"<br> x:Name="MyPanningItems"><br> <drawing:PanningItems.ItemTemplate><br> <DataTemplate><br> <Rectangle><br> <Rectangle.Fill><br> <ImageBrush ImageSource="{Binding .}"/><br> </Rectangle.Fill><br> </Rectangle><br> </DataTemplate><br> </drawing:PanningItems.ItemTemplate><br> </drawing:PanningItems><br> <Grid HorizontalAlignment="Center" <br> VerticalAlignment="Top"<br> Margin="0,50,0,0"><br> <Grid.RowDefinitions><br> <RowDefinition/><br> <RowDefinition/><br> </Grid.RowDefinitions><br> <Grid.Resources><br> <br> </Grid.Resources><br> <WrapPanel><br> <TextBlock Text="{Binding Hour,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"/><br> <TextBlock Text=":" x:Name="PART_TextBlock"><br> <TextBlock.Triggers><br> <EventTrigger RoutedEvent="FrameworkElement.Loaded"><br> <BeginStoryboard><br> <Storyboard><br> <DoubleAnimation Duration="00:00:01"<br> From="1"<br> To="0"<br> Storyboard.TargetName="PART_TextBlock"<br> Storyboard.TargetProperty="Opacity"<br> RepeatBehavior="Forever"<br> FillBehavior="Stop"/><br> </Storyboard><br> </BeginStoryboard><br> </EventTrigger><br> </TextBlock.Triggers><br> </TextBlock><br> <TextBlock Text="{Binding Minute,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"/><br> </WrapPanel><br> <TextBlock Grid.Row="1" FontSize="45" HorizontalAlignment="Center" Text="{Binding Date,RelativeSource={RelativeSource AncestorType=local:MainWindow}}"/><br> </Grid><br> </Grid><br></Window><br>2) MainWindow.xaml.cs 代码如下;
[*]当屏保启动后需要注意如下
[*]将鼠标设置为不可见Cursors.None;
[*]将窗体设置为最大化WindowState.Maximized;
[*]WindowStyle设置为"None";
[*]注意监听鼠标按下和键盘按键则退出屏保;
using System;<br>using System.Collections.ObjectModel;<br>using System.Globalization;<br>using System.IO;<br>using System.Windows;<br>using System.Windows.Input;<br>using System.Windows.Threading;<br><br>namespace ScreenSaver<br>{<br> /// <summary><br> /// MainWindow.xaml 的交互逻辑<br> /// </summary><br> public partial class MainWindow : Window<br> {<br> public static readonly DependencyProperty stringCollectionProperty =<br> DependencyProperty.Register("stringCollection", typeof(ObservableCollection<string>), typeof(MainWindow),<br> new PropertyMetadata(null));<br><br> public static readonly DependencyProperty HourProperty =<br> DependencyProperty.Register("Hour", typeof(string), typeof(MainWindow), new PropertyMetadata(null));<br><br> public static readonly DependencyProperty MinuteProperty =<br> DependencyProperty.Register("Minute", typeof(string), typeof(MainWindow), new PropertyMetadata(null));<br><br> public static readonly DependencyProperty SecondProperty =<br> DependencyProperty.Register("Second", typeof(string), typeof(MainWindow), new PropertyMetadata(null));<br><br> public static readonly DependencyProperty DateProperty =<br> DependencyProperty.Register("Date", typeof(string), typeof(MainWindow), new PropertyMetadata());<br><br> private readonly DispatcherTimer timer = new DispatcherTimer();<br><br> public MainWindow()<br> {<br> InitializeComponent();<br> Loaded += delegate<br> {<br> WindowState = WindowState.Maximized;<br> Mouse.OverrideCursor = Cursors.None;<br> var date = DateTime.Now;<br> Hour = date.ToString("HH");<br> Minute = date.ToString("mm");<br> Date =<br> $"{date.Month} / {date.Day} {CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek)}";<br> stringCollection = new ObservableCollection<string>();<br> var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");<br> var directoryInfo = new DirectoryInfo(path);<br> foreach (var item in directoryInfo.GetFiles())<br> {<br> if (Path.GetExtension(item.Name) != ".jpg") continue;<br> stringCollection.Add(item.FullName);<br> }<br><br> timer.Interval = TimeSpan.FromSeconds(1);<br> timer.Tick += delegate<br> {<br> date = DateTime.Now;<br> Hour = date.ToString("HH");<br> Minute = date.ToString("mm");<br> Date =<br> $"{date.Month} / {date.Day} {CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(date.DayOfWeek)}";<br> };<br> timer.Start();<br> };<br> MouseDown += delegate { Application.Current.Shutdown(); };<br> KeyDown += delegate { Application.Current.Shutdown(); };<br> }<br><br> public ObservableCollection<string> stringCollection<br> {<br> get => (ObservableCollection<string>)GetValue(stringCollectionProperty);<br> set => SetValue(stringCollectionProperty, value);<br> }<br><br><br> public string Hour<br> {<br> get => (string)GetValue(HourProperty);<br> set => SetValue(HourProperty, value);<br> }<br><br> public string Minute<br> {<br> get => (string)GetValue(MinuteProperty);<br> set => SetValue(MinuteProperty, value);<br> }<br><br> public string Second<br> {<br> get => (string)GetValue(SecondProperty);<br> set => SetValue(SecondProperty, value);<br> }<br><br><br> public string Date<br> {<br> get => (string)GetValue(DateProperty);<br> set => SetValue(DateProperty, value);<br> }<br> }<br>}<br>参考①参考②
参考资料
GitHub: https://github.com/yanjinhuagood/ScreenSaver
码云: https://gitee.com/yanjinhua/ScreenSaver
参考①: https://blog.walterlv.com/post/write-a-windows-screen-saver-using-wpf.html
参考②: https://wbsimms.com/create-screensaver-net-wpf/
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]