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.pswp.cn/news/284872.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/284872.shtml
英文地址,請注明出處:http://en.pswp.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;不知道學習的并發…