C# WPF 原神透明立绘时钟
By
jerryxjr1220
at 2023-10-17 • 0人收藏 • 904人看过
放在桌面上的效果,每10秒随机切换,动画效果5秒。 间隔时间可自行设定。

<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-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

增加实时天气预报
<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();
}
}登录后方可回帖
还可以增加立绘缓慢移动的效果
<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>