MainWindow.xaml.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.Eventing.Reader;
  4. using System.Globalization;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.Windows.Threading;
  17. using Microsoft.Extensions.Configuration;
  18. namespace CountDown;
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public DateTime EventDate { get; set; }
  25. private DispatcherTimer _timer;
  26. readonly CultureInfo _enUS = new ("en-US");
  27. public TimeSpan Counter
  28. {
  29. get { return (TimeSpan)GetValue(CounterProperty); }
  30. set { SetValue(CounterProperty, value); }
  31. }
  32. public static readonly DependencyProperty CounterProperty =
  33. DependencyProperty.Register(nameof(Counter), typeof(TimeSpan), typeof(MainWindow), new PropertyMetadata(new TimeSpan(0, 0, 0)));
  34. public MainWindow()
  35. {
  36. InitializeComponent();
  37. DataContext = this;
  38. var dateString = App.Config?.GetChildren().FirstOrDefault(c => c.Key == "EventDate")?.Value ?? "2025-06-30 23:59:59";
  39. if (DateTime.TryParseExact(dateString, "yyyy-MM-dd HH:mm:s", _enUS,
  40. DateTimeStyles.None, out DateTime dateValue))
  41. {
  42. EventDate = dateValue;
  43. }
  44. else
  45. {
  46. EventDate = new DateTime(2025, 06, 30, 23, 59, 59, DateTimeKind.Local);
  47. }
  48. Left = System.Windows.SystemParameters.PrimaryScreenWidth - Width - 20;
  49. Top = System.Windows.SystemParameters.PrimaryScreenHeight - Height - 100;
  50. Topmost = true;
  51. BitmapImage image = new (new Uri("./background.png", UriKind.Relative));
  52. ImageBrush fond = new (image);
  53. this.Background = fond;
  54. _timer = new();
  55. _timer.Interval = new TimeSpan(0, 0, 1);
  56. _timer.Tick += TimerTicked;
  57. _timer.Start();
  58. }
  59. private void SetTime()
  60. {
  61. Counter = EventDate - DateTime.Now;
  62. }
  63. private void TimerTicked(object? sender, EventArgs e)
  64. {
  65. SetTime();
  66. }
  67. private void DoClick(object sender, MouseButtonEventArgs e)
  68. {
  69. var path = System.IO.Path.GetDirectoryName(new System.Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
  70. if (!string.IsNullOrEmpty(path))
  71. {
  72. Process.Start("explorer.exe", path);
  73. }
  74. }
  75. }