WPF 制作 Windows 屏保

 分享如何使用WPF 制作 Windows 屏保

WPF 制作 Windows 屏保

作者:驚鏵

原文链接:https://github.com/yanjinhuagood/ScreenSaver

  • 框架使用.NET452

  • Visual Studio 2019;

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

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

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

 正文

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

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

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

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

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

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

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

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

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);}}
}

参考①[3]参考②[4]

参考资料

[1]

GitHub: https://github.com/yanjinhuagood/ScreenSaver

[2]

码云: https://gitee.com/yanjinhua/ScreenSaver

[3]

参考①: https://blog.walterlv.com/post/write-a-windows-screen-saver-using-wpf.html

[4]

参考②: https://wbsimms.com/create-screensaver-net-wpf/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/284872.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 定时线程

功能需求&#xff1a;项目启动时&#xff0c;后天起一个定时线程&#xff0c;每个小时跑一次&#xff0c;查出数据发邮件出来。 主要使用 public void schedule(TimerTask task, long delay)task被安排在delay&#xff08;毫秒&#xff09;指定的时间后执行。 public void sche…

Windows 7 下右键发送到菜单项没了

为什么80%的码农都做不了架构师&#xff1f;>>> 问题描述: 突然有一天,Windows 7 下右键发送到菜单项没了,如图所示: 问题原因 黑人问号脸? 转载于:https://my.oschina.net/taadis/blog/1591398

【ArcGIS微课1000例】0016:ArcGIS书签操作(添加书签、管理书签)知多少?

书签可以将地图数据的某一视图状态保存起来,以便在使用时打开书签,直接回到这一视图状态。可创建多个书签以便快速回到不同的视图状态,也可以对书签进行管理。 文章目录 1 创建书签2 管理书签注意:书签只针对空间数据,在【布局视图】中是不能创建书签的。 1 创建书签 可…

Android之webView打开http链接页面无法加载显示net:ERR_CLEARTEXT_NOT_PERMITTED

1、问题 适配Android13后&#xff0c;webView打开http链接提示错误如下 net:ERR_CLEARTEXT_NOT_PERMITTED2、原因 Android 9.0 默认使用加密连接&#xff0c;这意味着老旧项目在android 9.0 设备上运行&#xff0c;会遇到异常的情况。 3、解决办法 android:usesCleartextTr…

分享一个WPF 实现 Windows 软件快捷小工具

分享一个WPF 实现 Windows 软件快捷小工具Windows 软件快捷小工具作者&#xff1a;WPFDevelopersOrg原文链接&#xff1a;https://github.com/WPFDevelopersOrg/SoftwareHelper框架使用.NET40&#xff1b;Visual Studio 2019;项目使用 MIT 开源许可协议&#xff1b;项目使用 MV…

学习环境配置:Manjaro、MSYS2以及常见软件

0.前言 在说Manjaro之前&#xff0c;要先说一下Linux发行版。对于各大发行版而言&#xff0c;内核只有版本的差异&#xff0c;最重要的区别就是包管理系统。常见的包管理系统包括&#xff1a;Pacman&#xff0c;Apt , Yum和Portage。在学习Linux的过程中&#xff0c;和大数人一…

【ArcGIS微课1000例】0017:ArcGIS测量距离和面积工具的巧妙使用

文章目录 1 交互式测量2 测量要素ArcGIS提供了快速测量距离和面积的工具,通过测量工具可对地图中的线和面进行测量。 工具条: 测量工具位于【工具】工具条上,如下图所示: 测量界面: 功能按钮简介: 可使用此工具在地图上绘制一条线或者一个面,然后获取线的长度与面的面…

[转]HTTP/3 未来可期?

2015 年 HTTP/2 标准发表后&#xff0c;大多数主流浏览器也于当年年底支持该标准。此后&#xff0c;凭借着多路复用、头部压缩、服务器推送等优势&#xff0c;HTTP/2 得到了越来越多开发者的青睐&#xff0c;不知不觉的 HTTP 已经发展到了第三代。本文基于兴趣部落接入 HTTP/3 …

只让输入数字、字母、中文的输入框

1.输入框只能输入字母和下横线的正则表达式 <input type"text" onkeyup"this.valuethis.value.replace(/[^_a-zA-Z]/g,)" onpaste"this.valuethis.value.replace(/[^_a-zA-Z]/g,)"> 2.只能输入数字和英文 <input type"text" …

华为手机logcat不出日志解决方案

进入拨号界面输入&#xff1a;*#*#2846579#*#* 依次选择ProjectMenu---后台设置----LOG设置---LOG开关 点击打开转载于:https://www.cnblogs.com/liugangBlog/p/8058259.html

Android之启动奔溃提示异常java.lang.SecurityException: Permission Denial: startForeground

1 问题 适配Android高版本,启动奔溃,提示异常 java.lang.SecurityException: Permission Denial: startForeground from pid=1824, uid=10479 requires android.permission.FOREGROUND_SERVICEat android.os.Parcel.createException(Parcel.java:

【ArcGIS微课1000例】0018:ArcGIS设置相对路径和数据源

文章目录 ArcGIS设置相对路径ArcGIS设置数据源ArcGIS设置相对路径 菜鸟们在使用ArcGIS时经常会碰到将地图文档(.mxd)拷贝到别的电脑上或改变一个路径时,出现数据丢失的现象,具体表现为图层前面出现一个红色的感叹号,如下图所示。 出现以上问题的根本原因是数据GSS.tif的原…

AI 之 OpenCvSharp 安卓手机摄像头识别人脸

OpenCvSharp是OpenCv的包装器&#xff0c;相当于底层是OpenCv只是用.Net的方式调用底层的接口的实现&#xff0c;所以&#xff0c;从OpenCv的知识架构来讲&#xff0c;源码是一样一样的。就是换个语言写而已。1. OpenCvSharp 尽可能地以原生 OpenCV C/C API 风格为蓝本。2. Ope…

C#中二进制和流之间的各种相互转换

一. 二进制转换成图片间的转换 12345MemoryStream ms new MemoryStream(bytes); ms.Position 0; Image img Image.FromStream(ms); ms.Close(); this.pictureBox1.Image二. C#中byte[]与string的转换代码 1. 123System.Text.UnicodeEncoding converter new System.Text.Uni…

ASCII码16进制对照表

ASCII码对照表 ASCII&#xff08;American Standard Code for Information Interchange&#xff0c;美国信息互换标准代码&#xff0c;ASCⅡ&#xff09;是基于拉丁字母的一套电脑编码系统。它主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统&#xff0c…

如何获得带时间的ping的结果

ping 192.168.1.91 | awk {print strftime("%Y-%m-%d %H:%M:%S") "\t" $0} 转载于:https://blog.51cto.com/351842/2051815

iVX低代码平台系列制作APP简单的个人界面

一、前言 我们知道&#xff0c;目前市场上开发app或者小程序这些应用&#xff0c;都离不开一个个人界面&#xff0c;就是类似下面的这种界面&#xff0c;我们可以利用iVX低代码平台来开发&#xff0c;简单快速&#xff0c;如果还有不知道iVX低代码平台是啥的&#xff0c;猛戳这…

王高利:Apache Httpd负载均衡Tomcat并实现Session Sticky和Session Cluster

Apache Httpd负载均衡Tomcat并实现Session Sticky和Session Clusterhttp://anyisalin.blog.51cto.com/10917514/1766736转载于:https://blog.51cto.com/wanggaoli/1770659

对比C#聊聊C++大一统的初始化运算符 {}

一&#xff1a;背景 最近发现 C 中的类型初始化操作&#xff0c;没有 {} 运算符搞不定的&#xff0c;蛮有意思&#xff0c;今天我们就来逐一列一下各自的用法以及汇编展现&#xff0c;本来想分为 值类型 和 引用类型 两大块&#xff0c;但发现在 C 中没这种说法&#xff0c;默认…

[转]【高并发】高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!

前言 很多小伙伴反馈说&#xff0c;高并发专题学了那么久&#xff0c;但是&#xff0c;在真正做项目时&#xff0c;仍然不知道如何下手处理高并发业务场景&#xff01;甚至很多小伙伴仍然停留在只是简单的提供接口&#xff08;CRUD&#xff09;阶段&#xff0c;不知道学习的并发…