成都网站建设设计

将想法与焦点和您一起共享

怎么用WPF代码实现Windows屏保制作

这篇文章主要介绍“怎么用WPF代码实现Windows屏保制作”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用WPF代码实现Windows屏保制作”文章能帮助大家解决问题。

创新互联是一家专业提供新丰企业网站建设,专注与成都网站制作、网站建设、H5响应式网站、小程序制作等业务。10年已为新丰众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

正文

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

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

怎么用WPF代码实现Windows屏保制作

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

怎么用WPF代码实现Windows屏保制作

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

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

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

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

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

怎么用WPF代码实现Windows屏保制作

实现代码

1)MainWindow.xaml 代码如下;


    
        
            
                
                    
                        
                            
                        
                    
                
            
        
        
            
                
                
            
            
                
                    
                    
                    
                
            
            
                
                
                    
                        
                            
                                
                                    
                                
                            
                        
                    
                
                
            
            
        
    

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
{
    /// 
    ///     MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty stringCollectionProperty =
            DependencyProperty.Register("stringCollection", typeof(ObservableCollection), 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();
                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 stringCollection
        {
            get => (ObservableCollection)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屏保制作
链接分享:http://chengdu.cdxwcx.cn/article/igoppg.html