基本信息
源码名称:C#定时提醒休息小工具源码(可自动显示桌面和动画)
源码大小:1.88M
文件格式:.zip
开发语言:C#
更新时间:2019-08-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
定时休息,自动显示桌面和动画。 提供C#源代码。


到了指定休息的时间会弹出如下动画效果:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ShowDesktop
{
    delegate void SetVisible(bool visible);
    delegate void SetNotifyText(string text);
    public partial class AnimForm : Form
    {
        private Image m_imgImage = null;
        private EventHandler m_evthdlAnimator = null;
        System.Threading.Thread ShowThread = null;

        int nStep = 15;
        int nRest = 1;
        int nPause = 60;
        bool bPause = false;
        bool bShowDesktop = true;
        bool bShowAnim = true;
        bool bClosing = false;
        bool bAutoRun = false;

        public AnimForm()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            m_evthdlAnimator = new EventHandler(OnImageAnimate);

            this.Visible = false;
            this.TopMost = true;
            this.WindowState = FormWindowState.Minimized;

            ReadIniFile();

            ShowThread = new System.Threading.Thread(new System.Threading.ThreadStart(ShowDesktopThread));
            ShowThread.Name = "ShowDesktopThread";
            ShowThread.Start();
        }

        private void SetVisible(bool visible)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.InvokeRequired)
            {
                SetVisible d = new SetVisible(SetVisible);
                this.Invoke(d, new object[] { visible });
            }
            else
            {
                if (visible)
                    this.WindowState = FormWindowState.Normal;
                this.Visible = visible;
            }
        }

        private void SetNotifyText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.InvokeRequired)
            {
                SetNotifyText d = new SetNotifyText(SetNotifyText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                notifyIcon1.Text = text;
            }
        }

        private void ShowDesktopThread()
        {
            int nTickLast = System.Environment.TickCount;
            bool bLastPauseChecked = this.bPause;
            bool bLastFullScreenApp = false;
            while (true)
            {
                //有全屏程序运行
                if (RunningFullScreenApp)
                {
                    bLastFullScreenApp = true;
                    System.Threading.Thread.Sleep(100);
                    continue;
                }

                int nTickNow = System.Environment.TickCount;
                //更改暂停状态,重新计时
                if (bLastPauseChecked != this.bPause)
                {
                    bLastPauseChecked = this.bPause;
                    nTickLast = nTickNow;
                }
                //从全屏状态退出,重新计时
                if (bLastFullScreenApp == true)
                {
                    bLastFullScreenApp = false;
                    nTickLast = nTickNow;
                }

                int nRemain = nStep * 60 * 1000;
                //加上暂停的时间
                if (this.bPause)
                {
                    nRemain  = nPause * 60 * 1000;
                }
                nRemain -= nTickNow - nTickLast;
                int nMinute = nRemain /( 60 * 1000);
                int nSecond = nRemain / 1000 - nMinute * 60;
                string text = string.Format("还剩{0}分钟{1}秒休息…", nMinute, nSecond) ;
                SetNotifyText(text);

                //暂停时间结束,开始倒计时下次休息
                if (this.bPause && nTickNow - nTickLast > nPause * 60 * 1000)
                {
                    this.bPause = false;
                    nTickLast = nTickNow;
                }
                //开始休息
                else if (!this.bPause && nTickNow - nTickLast > nStep * 60 * 1000)
                //if (nRemain <= 0)
                {
                    if (this.bPause) this.bPause = false;
                    nTickLast = nTickNow;
                    if (bShowDesktop)//显示桌面
                        ShowDesktop();
                    System.Threading.Thread.Sleep(100);
                    if (bShowAnim)//显示动画
                        SetVisible(true);
                }
                //结束休息
                else if (!this.bPause && nTickNow - nTickLast > nRest * 60 * 1000)
                //else if (nTickNow - nTickLast > nRest * 60 * 1000)
                {
                    SetVisible(false);
                }
                System.Threading.Thread.Sleep(100);
            }
        }

        void ShowDesktop()
        {
            Type oleType = Type.GetTypeFromProgID("Shell.Application");
            object oleObject = System.Activator.CreateInstance(oleType);
            oleType.InvokeMember("ToggleDesktop", BindingFlags.InvokeMethod, null, oleObject, null);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (m_imgImage != null)
            {
                UpdateImage();
                // 绘制图片的当前帧
                e.Graphics.DrawImage(m_imgImage, new Rectangle(0, 0, m_imgImage.Width, m_imgImage.Height));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                Stream manifestResourceStream = this.GetType().Assembly.GetManifestResourceStream("ShowDesktop.amtf.gif");
                m_imgImage = Image.FromStream(manifestResourceStream, true);
            }
            catch
            {
                MessageBox.Show("无法载入动画文件!", "ShowDesktop");
                MyClose();
            }
            BeginAnimate();
        }
        #region functions of animator image
        private void BeginAnimate()
        {
            if (m_imgImage == null)
                return;

            if (ImageAnimator.CanAnimate(m_imgImage))
            {
                ImageAnimator.Animate(m_imgImage, m_evthdlAnimator);
            }
        }

        private void StopAnimate()
        {
            if (m_imgImage == null)
                return;

            if (ImageAnimator.CanAnimate(m_imgImage))
            {
                ImageAnimator.StopAnimate(m_imgImage, m_evthdlAnimator);
            }
        }

        private void UpdateImage()
        {
            if (m_imgImage == null)
                return;

            if (ImageAnimator.CanAnimate(m_imgImage))
            {
                ImageAnimator.UpdateFrames(m_imgImage);
            }
        }

        /// <summary>
        /// 动画图片帧变化的时间触发,这个事件并非在主线程中触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnImageAnimate(Object sender, EventArgs e)
        {
            this.Invalidate();
        }
        #endregion // End of functions of animator image

        private void AnimForm_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
            shape.AddEllipse(50, 60, 423, 438);
            this.Region = new Region(shape);
        }

        private void AnimForm_MouseDown(object sender, MouseEventArgs e)
        {
            this.Visible = false;
        }

        public SetForm form = null;

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (form == null)
            {
                form = new SetForm(SetParam, MyClose, nStep, nRest, bPause, nPause, bShowDesktop, bShowAnim, bAutoRun);
                form.Show();
            }
            else
            {
                form.Visible = true;
                form.WindowState = FormWindowState.Normal;
            }
        }

        private void ReadIniFile()
        {
            try
            {
                using (StreamReader reader = File.OpenText("ShowDesktop.ini"))
                {
                    string line = "";
                    string[] values;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.StartsWith("RestStep"))
                        {
                            values = line.Split(' ');
                            nStep = Convert.ToInt32(values[values.Length - 1]);
                            nStep = nStep <= 0 ? 1 : nStep;
                        }
                        else if (line.StartsWith("RestTime"))
                        {
                            values = line.Split(' ');
                            nRest = Convert.ToInt32(values[values.Length - 1]);
                            nRest = nRest <= 0 ? 1 : nRest;
                        }
                        else if (line.StartsWith("PauseTime"))
                        {
                            values = line.Split(' ');
                            nPause = Convert.ToInt32(values[values.Length - 1]);
                            nPause = nPause <= 0 ? 1 : nPause;
                        }
                        else if (line.StartsWith("ShowDesktop"))
                        {
                            values = line.Split(' ');
                            bShowDesktop = Convert.ToBoolean(values[values.Length - 1]);
                        }
                        else if (line.StartsWith("ShowAnim"))
                        {
                            values = line.Split(' ');
                            bShowAnim = Convert.ToBoolean(values[values.Length - 1]);
                        }
                        else if (line.StartsWith("AutoRun"))
                        {
                            //读配置文件不顶事!
                            //values = line.Split(' ');
                            //bAutoRun = Convert.ToBoolean(values[values.Length - 1]);
                        }
                    }
                    reader.Close();
                    if (!bShowDesktop && !bShowAnim)
                        bShowDesktop = true;
                    bAutoRun = IsAutoRun();//还是注册表说了算呀
                }
            }
            catch
            {
            }
        }
        private void WriteIniFile()
        {
            try
            {
                using (StreamWriter writer = File.CreateText("ShowDesktop.ini"))
                {
                    string line = string.Format("RestStep {0}", nStep);
                    writer.WriteLine(line);
                    line = string.Format("RestTime {0}", nRest);
                    writer.WriteLine(line);
                    line = string.Format("PauseTime {0}", nPause);
                    writer.WriteLine(line);
                    line = string.Format("ShowDesktop {0}", bShowDesktop);
                    writer.WriteLine(line);
                    line = string.Format("ShowAnim {0}", bShowAnim);
                    writer.WriteLine(line);
                    line = string.Format("AutoRun {0}", bAutoRun);
                    writer.WriteLine(line);
                    writer.Close();
                }
            }
            catch
            {
            }
        }

        public void SetParam(int nStep, int nRest, bool bPause, int nPause, bool bShowDesktop, bool bShowAnim, bool bAutoRun)
        {
            form = null;
            this.nStep = nStep;
            this.nRest = nRest;
            this.bPause = bPause;
            this.nPause = nPause;
            this.bShowDesktop = bShowDesktop;
            this.bShowAnim = bShowAnim;
            this.bAutoRun = bAutoRun;
            if (bAutoRun != IsAutoRun())
            {
                SetAutoRun(this.GetType().Assembly.Location, bAutoRun);
                //有时候写不成功的,所以要再读一遍真正的数值。
                this.bAutoRun = IsAutoRun();
            }
            WriteIniFile();
        }

        private bool IsAutoRun()
        {
            RegistryKey reg = null;
            try
            {
                string fileName = this.GetType().Assembly.Location;
                String name = fileName.Substring(fileName.LastIndexOf(@"/")   1);
                name = name.Substring(fileName.LastIndexOf(@"\")   1);
                reg = OpenRegistryPath(Registry.CurrentUser, @"/SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
                string value = reg.GetValue(name).ToString();
                return string.Equals(fileName, value);
            }
            catch
            {
                MessageBox.Show("无法读注册表,不能判断当前是否是开机自动运行。", "ShowDesktop");
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }
            return false;
        }

        private void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                String name = fileName.Substring(fileName.LastIndexOf(@"/")   1);
                name = name.Substring(fileName.LastIndexOf(@"\")   1);
                reg = OpenRegistryPath(Registry.CurrentUser, @"/SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
                if (reg == null)
                    throw new Exception();
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, "");
            }
            catch (Exception ex)
            {
                MessageBox.Show("尝试设置开机自动运行,写入系统注册表失败!\r\n请尝试关闭关闭第三方防护软件,\r\n或者关闭本程序,尝试以管理员身份再次运行。", "ShowDesktop");
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }
        }

        private RegistryKey OpenRegistryPath(RegistryKey root, string s)
        {
            s = s.Remove(0, 1)   @"/";
            while (s.IndexOf(@"/") != -1)
            {
                //root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")));
                root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")), true);
                s = s.Remove(0, s.IndexOf(@"/")   1);
            }
            return root;
        }

        public void MyClose()
        {
            ShowThread.Abort();
            this.RegisterAppBar(true);
            bClosing = true;
            if (m_imgImage != null)
            {
                StopAnimate();
                m_imgImage = null;
            }
            this.Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!bClosing)
            {
                this.Visible = false;
                e.Cancel = true;
            }
            return;
        }
    } 
 }