原文:WPF中桌面屏保的制作(主要代码)

制作要点:
(1) 使用System.Windows.Threading.DispatcherTimer;
(2) 将Window属性设置为:
      this.WindowState = WindowState.Maximized;
      this.WindowStyle = WindowStyle.None;
      this.ResizeMode = ResizeMode.NoResize;
(3) 按ESC键时,关闭窗口。

关键代码:

        System.Windows.Threading.DispatcherTimer frameTimer;
        int lastTick;

        public Window1()
        {
            InitializeComponent();

            this.WindowState = WindowState.Maximized;
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;

            frameTimer = new System.Windows.Threading.DispatcherTimer();
            frameTimer.Tick += OnFrame;
            frameTimer.Interval = TimeSpan.FromSeconds(1.0 / 60.0);
            frameTimer.Start();

            this.lastTick = Environment.TickCount;

            rand = new Random(this.GetHashCode());

            this.Show();

            this.KeyDown += new System.Windows.Input.KeyEventHandler(Window1_KeyDown);

            //绘制屏保内容,可以是动画,也可以是动态更新的图片
            // DrawingSomethings();
        }

        void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // 当按Esc按钮时关闭窗口,即中止屏保
            if (e.Key == System.Windows.Input.Key.Escape)
                this.Close();
        }

        private void OnFrame(object sender, EventArgs e)
        {
        } 

         //绘制屏保内容,可以是动画,也可以是动态更新的图片
        private void DrawingSomethings()
        {
               // 代码.....
        }

05-08 15:49