利用WPF實(shí)現(xiàn)Windows屏保的制作
介紹
框架使用.NET452
;
Visual Studio 2019
;
項(xiàng)目使用 MIT 開(kāi)源許可協(xié)議;
更多效果可以通過(guò)GitHub[1]|碼云[2]下載代碼;
也可以自行添加天氣信息等。
正文
屏保程序的本質(zhì)上就是一個(gè)Win32
窗口應(yīng)用程序;
把編譯好一個(gè)窗口應(yīng)用程序之后,把擴(kuò)展名更改為 scr
,于是你的屏幕保護(hù)程序就做好了;
選中修改好的 scr
程序上點(diǎn)擊右鍵,可以看到一個(gè) 安裝 選項(xiàng),點(diǎn)擊之后就安裝了;
安裝之后會(huì)立即看到我們的屏幕保護(hù)程序已經(jīng)運(yùn)行起來(lái)了;
處理屏幕保護(hù)程序參數(shù)如下
/s 屏幕保護(hù)程序開(kāi)始,或者用戶點(diǎn)擊了 預(yù)覽
按鈕;
/c 用戶點(diǎn)擊了 設(shè)置
按鈕;
/p 用戶選中屏保程序之后,在預(yù)覽窗格中顯示;
實(shí)現(xiàn)代碼
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
代碼如下;
當(dāng)屏保啟動(dòng)后需要注意如下
- 將鼠標(biāo)設(shè)置為不可見(jiàn)
Cursors.None
; - 將窗體設(shè)置為最大化
WindowState.Maximized
; WindowStyle
設(shè)置為"None"
;- 注意監(jiān)聽(tīng)鼠標(biāo)按下和鍵盤(pán)按鍵則退出屏保;
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); ????????} ????} }
到此這篇關(guān)于利用WPF實(shí)現(xiàn)Windows屏保的制作的文章就介紹到這了,更多相關(guān)WPF制作Windows屏保內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析c#在未出現(xiàn)異常情況下查看當(dāng)前調(diào)用堆棧的解決方法
本篇文章是對(duì)c#在未出現(xiàn)異常情況下查看當(dāng)前調(diào)用堆棧的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#實(shí)現(xiàn)XOR密碼(異或密碼)的示例代碼
XOR密碼(異或密碼)是一種簡(jiǎn)單的加密算法,它使用異或(XOR)操作來(lái)對(duì)明文和密鑰進(jìn)行加密和解密,本文為大家介紹了C#實(shí)現(xiàn)XOR密碼的相關(guān)知識(shí),希望對(duì)大家有所幫助2024-01-01C# 在PDF文檔中創(chuàng)建表格的實(shí)現(xiàn)方法
表格能夠一目了然的讓用戶看到數(shù)據(jù)信息,使信息顯得有條理化,那么在pdf類型的文檔中如何來(lái)添加表格并對(duì)表格進(jìn)行格式化操作呢?下面小編給大家?guī)?lái)了C# 在PDF文檔中創(chuàng)建表格的實(shí)現(xiàn)方法,需要的朋友參考下吧2017-12-12UGUI實(shí)現(xiàn)隨意調(diào)整Text中的字體間距
這篇文章主要為大家詳細(xì)介紹了UGUI實(shí)現(xiàn)隨意調(diào)整字體間距的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法,涉及foreach語(yǔ)句的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#值類型、引用類型中的Equals和==的區(qū)別淺析
這篇文章主要介紹了C#值類型、引用類型中的Equals和==的區(qū)別淺析,本文分別對(duì)C#值類型和引用類型中的Equals和==做了講解和給出了實(shí)例,需要的朋友可以參考下2015-01-01