C# WPF 原神透明立绘时钟

By jerryxjr1220 at 2023-10-17 • 0人收藏 • 602人看过

放在桌面上的效果,每10秒随机切换,动画效果5秒。 间隔时间可自行设定。

screenshots.gif

<Window x:Class="WPFYuanShenDrawingVisual.Views.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:model="clr-namespace:WPFYuanShenDrawingVisual.Models"
        xmlns:vm="clr-namespace:WPFYuanShenDrawingVisual.ViewModels"
        xmlns:view="clr-namespace:WPFYuanShenDrawingVisual.Views"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:WPFYuanShenDrawingVisual"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:system="clr-namespace:System;assembly=System.Runtime"
        d:DataContext="{d:DesignInstance Type=vm:MainViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Width="400" Height="360"
        FontFamily="JetBrains Mono" 
        FontSize="{StaticResource TextFontSize}" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent">

    <hc:SimplePanel>
        <hc:ImageBlock x:Name="imageblock" Source="{Binding ImageSource}">
            <hc:ImageBlock.Triggers>
                <EventTrigger RoutedEvent="SizeChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="0" To="1" Duration="0:0:5"
                                             Storyboard.TargetName="imageblock"
                                             Storyboard.TargetProperty="Opacity">
                                
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </hc:ImageBlock.Triggers>
        </hc:ImageBlock>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" 
                   Text="{Binding CurrentTime}" Foreground="White"
                   Margin="0,200,0,0"/>
    </hc:SimplePanel>
</Window>
using System;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using WPFYuanShenDrawingVisual.Models;

namespace WPFYuanShenDrawingVisual.ViewModels;

public partial class MainViewModel : ObservableObject
{
    //[ObservableProperty] private DrawingVisualModel _visualModel;
    [ObservableProperty] private ImageSource _imageSource;
    [ObservableProperty] private string _currentTime;

    private DispatcherTimer timer;
    public MainViewModel()
    {
        //VisualModel = new DrawingVisualModel();
        ImageSource = new BitmapImage(new Uri("imgs/1.png", UriKind.RelativeOrAbsolute));
        CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (sender, args) =>
        {
            CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        };
        timer.Start();        
        //发送消息
        //WeakReferenceMessenger.Default.Send(new CustomizedMessage(new MessageModel()));
    }
}

//定义消息
// public class CustomizedMessage : ValueChangedMessage<MessageModel> // PropertyChangedMessage // RequestMessage ( Async + ..)
// {
//     public CustomizedMessage(MessageModel value) : base(value)
//     {
//     }
// }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using HandyControl.Controls;
using WPFYuanShenDrawingVisual.Models;
using WPFYuanShenDrawingVisual.ViewModels;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace WPFYuanShenDrawingVisual.Views;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private MainViewModel mainViewModel;

    public MainWindow()
    {
        InitializeComponent();

        #region 注册并接受消息

        //WeakReferenceMessenger.Default.Register<CustomizedMessage>(this, (o, m) => { MessageBox.Show("Received!"); });

        #endregion


        mainViewModel = new MainViewModel();
        this.DataContext = mainViewModel;

        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (sender, args) =>
        {
            if (DateTime.Now.Second % 10 == 0)
            {
                var rand = new Random(DateTime.Now.Microsecond);
                int i = rand.Next(minValue: 0, maxValue: 12) * 7 + 1;
                mainViewModel.ImageSource = new BitmapImage(new Uri($"imgs/{i}.png", UriKind.RelativeOrAbsolute));
                imageblock.RenderSize = (int)imageblock.RenderSize.Width == 400 ? new Size(401, 360) : new Size(400, 360);
            }
        };
        timer.Start();        
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        
        DragMove();
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        if(e.Key==Key.Escape) 
            Application.Current.Shutdown();
    }
}


5 个回复 | 最后更新于 2023-10-23
2023-10-17   #1

还可以增加立绘缓慢移动的效果

<Window x:Class="WPFYuanShenDrawingVisual.Views.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:model="clr-namespace:WPFYuanShenDrawingVisual.Models"
        xmlns:vm="clr-namespace:WPFYuanShenDrawingVisual.ViewModels"
        xmlns:view="clr-namespace:WPFYuanShenDrawingVisual.Views"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:WPFYuanShenDrawingVisual"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:system="clr-namespace:System;assembly=System.Runtime"
        d:DataContext="{d:DesignInstance Type=vm:MainViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Width="400" Height="360"
        FontFamily="JetBrains Mono" 
        FontSize="{StaticResource TextFontSize}" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent">

    <hc:SimplePanel>
        <hc:ImageBlock x:Name="imageblock" Source="{Binding ImageSource}">
            <hc:ImageBlock.RenderTransform>
                <TranslateTransform X="-50" x:Name="xpos"/>
            </hc:ImageBlock.RenderTransform>
            <hc:ImageBlock.Triggers>
                <EventTrigger RoutedEvent="SizeChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="0" To="1" Duration="0:0:5"
                                             Storyboard.TargetName="imageblock"
                                             Storyboard.TargetProperty="Opacity" />

                            <DoubleAnimation From="50" To="0" Duration="0:0:8"
                                             Storyboard.TargetName="xpos"
                                             Storyboard.TargetProperty="X" />

                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </hc:ImageBlock.Triggers>
        </hc:ImageBlock>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" 
                   Text="{Binding CurrentTime}" Foreground="White"
                   Margin="0,200,0,0"/>
    </hc:SimplePanel>
</Window>


2023-10-19   #2

除了handycontrol还有哪些比较好看好用的?material 除外

2023-10-19   #3

回复#2 @aubreychao :

还有wpfui,sunnyui

不过其实都差不多,熟悉了都好用。

2023-10-23   #4
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.InteropServices.JavaScript;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using WPFYuanShenDrawingVisual.Models;

namespace WPFYuanShenDrawingVisual.ViewModels;

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty] private DrawingVisualModel _visualModel;
    [ObservableProperty] private ImageSource _imageSource;
    [ObservableProperty] private string _currentTime;
    [ObservableProperty] private Image[] _images;

    private DispatcherTimer timer;
    public MainViewModel()
    {
        VisualModel = new DrawingVisualModel();
        Images = GetImageSource();
        ImageSource = Images[0].Source;
        CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (sender, args) =>
        {
            CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        };
        timer.Start();        
        //发送消息
        //WeakReferenceMessenger.Default.Send(new CustomizedMessage(new MessageModel()));
    }

    public Image[] GetImageSource()
    {
        List<Image> images = new();
        string url = "https://content-static.mihoyo.com/content/ysCn/getContentList?pageSize=206&pageNum=1&order=asc&channelId=350";
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("User-Agent", "MobileQQ/5.30.5");
        var result = client.GetStringAsync(url).Result;
        Regex regex = new Regex(@"(?<=""url"":"")(.*?\.png)(?="")");
        var matches = regex.Matches(result);
        foreach (var match in matches)
        {
            if (((Match)match).Value.Contains("{") || ((Match)match).Value.Contains("[")) continue;
            images.Add(new Image() { Source = (ImageSource)(new ImageSourceConverter().ConvertFromString(((Match)match).Value.Replace(@"\/", "/"))) });
        }
        return images.ToArray();
    }
}

//定义消息
// public class CustomizedMessage : ValueChangedMessage<MessageModel> // PropertyChangedMessage // RequestMessage ( Async + ..)
// {
//     public CustomizedMessage(MessageModel value) : base(value)
//     {
//     }
// }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using HandyControl.Controls;
using WPFYuanShenDrawingVisual.Models;
using WPFYuanShenDrawingVisual.ViewModels;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace WPFYuanShenDrawingVisual.Views;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private MainViewModel mainViewModel;

    public MainWindow()
    {
        InitializeComponent();

        #region 注册并接受消息

        //WeakReferenceMessenger.Default.Register<CustomizedMessage>(this, (o, m) => { MessageBox.Show("Received!"); });

        #endregion


        mainViewModel = new MainViewModel();
        this.DataContext = mainViewModel;

        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (sender, args) =>
        {
            if (DateTime.Now.Second % 10 == 0)
            {
                var rand = new Random(DateTime.Now.Microsecond);
                int i = rand.Next(minValue: 0, maxValue: 13) * 6 + 1;
                
                mainViewModel.ImageSource = mainViewModel.Images[i].Source;
                imageblock.RenderSize = (int)imageblock.RenderSize.Width == 400 ? new Size(401, 360) : new Size(400, 360);
            }
        };
        timer.Start();        
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        
        DragMove();
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        if(e.Key==Key.Escape) 
            Application.Current.Shutdown();
    }
}


2023-10-23   #5

screenshots.gif

增加实时天气预报

<Window x:Class="WPFYuanShenDrawingVisual.Views.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:model="clr-namespace:WPFYuanShenDrawingVisual.Models"
        xmlns:vm="clr-namespace:WPFYuanShenDrawingVisual.ViewModels"
        xmlns:view="clr-namespace:WPFYuanShenDrawingVisual.Views"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:WPFYuanShenDrawingVisual"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:system="clr-namespace:System;assembly=System.Runtime"
        d:DataContext="{d:DesignInstance Type=vm:MainViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Width="400" Height="360"
        FontFamily="JetBrains Mono" 
        FontSize="{StaticResource TextFontSize}" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent">

    <hc:SimplePanel>
        <hc:ImageBlock x:Name="imageblock" Source="{Binding ImageSource}">
            <hc:ImageBlock.RenderTransform>
                <TranslateTransform X="-50" x:Name="xpos"/>
            </hc:ImageBlock.RenderTransform>
            <hc:ImageBlock.Triggers>
                <EventTrigger RoutedEvent="SizeChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="0" To="1" Duration="0:0:5"
                                             Storyboard.TargetName="imageblock"
                                             Storyboard.TargetProperty="Opacity" />

                            <DoubleAnimation From="50" To="0" Duration="0:0:8"
                                             Storyboard.TargetName="xpos"
                                             Storyboard.TargetProperty="X" />

                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </hc:ImageBlock.Triggers>
        </hc:ImageBlock>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" 
                   Text="{Binding CurrentTime}" Foreground="White"
                   Margin="0,160,0,0"/>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" 
                   Text="{Binding Weather}" Foreground="White"
                   Margin="0,280,0,0"/>
    </hc:SimplePanel>
</Window>
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using WPFYuanShenDrawingVisual.Models;

namespace WPFYuanShenDrawingVisual.ViewModels;

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty] private DrawingVisualModel _visualModel;
    [ObservableProperty] private ImageSource _imageSource;
    [ObservableProperty] private string _currentTime;
    [ObservableProperty] private Image[] _images;
    [ObservableProperty] private string _weather;

    private DispatcherTimer timer;
    public MainViewModel()
    {
        VisualModel = new DrawingVisualModel();
        Images = GetImageSource();
        ImageSource = Images[61].Source;
        Weather = GetWeather();
        CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (sender, args) =>
        {
            CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        };
        timer.Start();        
        //发送消息
        //WeakReferenceMessenger.Default.Send(new CustomizedMessage(new MessageModel()));
    }

    public string GetWeather()
    {
        var sb = new StringBuilder();
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("User-Agent", "MobileQQ/5.30.5");
        client.DefaultRequestHeaders.Add("Host", "d1.weather.com.cn");
        client.DefaultRequestHeaders.Add("Referer", "http://hunan.promotion.weather.com.cn/");
        var result = client.GetStringAsync("http://d1.weather.com.cn/sk_2d/101190405.html").Result[11..];
        var jsonNode = JsonObject.Parse(result);
        sb.AppendLine($"{jsonNode!["date"]} {jsonNode!["cityname"]} 天气预报");
        sb.AppendLine($"天气:{jsonNode!["weather"]},当前温度:{jsonNode!["temp"]}℃");
        sb.AppendLine($"湿度:{jsonNode!["SD"]},空气质量:{jsonNode!["aqi"]}");
        return sb.ToString();
    }

    public Image[] GetImageSource()
    {
        List<Image> images = new();
        string url = "https://content-static.mihoyo.com/content/ysCn/getContentList?pageSize=206&pageNum=1&order=asc&channelId=350";
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("User-Agent", "MobileQQ/5.30.5");
        var result = client.GetStringAsync(url).Result;
        Regex regex = new Regex(@"(?<=""url"":"")(.*?\.png)(?="")");
        var matches = regex.Matches(result);
        foreach (var match in matches)
        {
            if (((Match)match).Value.Contains("{") || ((Match)match).Value.Contains("[")) continue;
            images.Add(new Image() { Source = (ImageSource)(new ImageSourceConverter().ConvertFromString(((Match)match).Value.Replace(@"\/", "/"))) });
        }
        return images.ToArray();
    }
}

//定义消息
// public class CustomizedMessage : ValueChangedMessage<MessageModel> // PropertyChangedMessage // RequestMessage ( Async + ..)
// {
//     public CustomizedMessage(MessageModel value) : base(value)
//     {
//     }
// }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using HandyControl.Controls;
using WPFYuanShenDrawingVisual.Models;
using WPFYuanShenDrawingVisual.ViewModels;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace WPFYuanShenDrawingVisual.Views;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private MainViewModel mainViewModel;

    public MainWindow()
    {
        InitializeComponent();

        #region 注册并接受消息

        //WeakReferenceMessenger.Default.Register<CustomizedMessage>(this, (o, m) => { MessageBox.Show("Received!"); });

        #endregion


        mainViewModel = new MainViewModel();
        this.DataContext = mainViewModel;

        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += (sender, args) =>
        {
            if (DateTime.Now.Second % 10 == 0)
            {
                var rand = new Random(DateTime.Now.Microsecond);
                int i = rand.Next(minValue: 0, maxValue: 13) * 6 + 1;
                
                mainViewModel.ImageSource = mainViewModel.Images[i].Source;
                imageblock.RenderSize = (int)imageblock.RenderSize.Width == 400 ? new Size(401, 360) : new Size(400, 360);
            }

            if (DateTime.Now.Minute % 60 == 0)
            {
                mainViewModel.Weather = mainViewModel.GetWeather();
            }
        };
        timer.Start();        
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        
        DragMove();
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        if(e.Key==Key.Escape) 
            Application.Current.Shutdown();
    }
}


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...