ScreensaverWindow.xaml.cs
上传用户:liuqi822
上传日期:2022-03-21
资源大小:88k
文件大小:3k
源码类别:

屏幕保护

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using TwitterClient;
  15. using System.Timers;
  16. using System.Globalization;
  17. namespace Screensaver1
  18. {
  19.     /// <summary>
  20.     /// Interaction logic for Window1.xaml
  21.     /// </summary>
  22.     public partial class ScreensaverWindow : Window
  23.     {
  24.         Point? lastMousePosition;
  25.         public static readonly DependencyProperty ScreensaverContentProperty =
  26.                 DependencyProperty.Register("ScreensaverContent", typeof(object), typeof(ScreensaverWindow), new UIPropertyMetadata(string.Empty));
  27.         TwitterService twitterService;
  28.         public ScreensaverWindow()
  29.         {
  30.             InitializeComponent();
  31.             this.DataContext = this;
  32.             twitterService = new TwitterService(Properties.Settings.Default.TwitterHashcode);
  33.             twitterService.FeedItemChanged += new EventHandler<FeedItemEventArgs>(twitterService_FeedItemChanged);
  34.             twitterService.Start();
  35.         }
  36.         void twitterService_FeedItemChanged(object sender, FeedItemEventArgs e)
  37.         {
  38.             this.Dispatcher.Invoke(new Action(() => this.ScreensaverContent = new FeedItemControl(e.Item)));
  39.         }
  40.         public object ScreensaverContent
  41.         {
  42.             get { return GetValue(ScreensaverContentProperty); }
  43.             set { SetValue(ScreensaverContentProperty, value); }
  44.         }
  45.         protected override void OnKeyDown(KeyEventArgs e)
  46.         {
  47.             Close();
  48.         }
  49.         protected override void OnMouseDown(MouseButtonEventArgs e)
  50.         {
  51.             Close();
  52.         }
  53.         protected override void OnMouseMove(MouseEventArgs e)
  54.         {
  55.             Point currentMousePosition = e.MouseDevice.GetPosition(this);
  56.             if (lastMousePosition.HasValue)
  57.             {
  58.                 // Check if the mouse position has changed
  59.                 if (Math.Abs(lastMousePosition.Value.X - currentMousePosition.X) > 0 ||
  60.                     Math.Abs(lastMousePosition.Value.Y - currentMousePosition.Y) > 0)
  61.                 {
  62.                     Close();
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 lastMousePosition = currentMousePosition;
  68.             }
  69.         }
  70.     }
  71. }