• 大小: 170KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: C#
  • 标签:

资源简介

WinForm实现从NTP服务器同步本地时间,并且可以自动检测当前时区;通过程序中的相关设置可以实现双击运行程序一键式自动同步时间。

资源截图

代码片段和文件信息

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace SyncTime
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender EventArgs e)
        {
            Program.msgTxt = msgTxt;
            XBtn.PerformClick();
            
            bool autoGet = Properties.Settings.Default.AutoGet;
            bool autoSync = Properties.Settings.Default.AutoSync;
            autoGetChk.Checked = autoGet;
            autoSyncChk.Checked = autoSync;

            if (autoSync)
            {
                if (setTimeZone() && setTime())
                {
                    // 最小化窗口
                    this.WindowState = FormWindowState.Minimized;
                    // 隐藏任务栏图标
                    this.ShowInTaskbar = false;
                    // 启动自动退出线程
                    Thread exitThread = new Thread(exitProgram);
                    exitThread.IsBackground = true;
                    exitThread.Start();
                }
                else
                {
                    autoSyncChk.Checked = false;
                }
            }
            else if (autoGet)
            {
                if (!setTimeZone() || !getTime())
                {
                    autoGetChk.Checked = false;
                }
            }
        }

        private void XBtn_Click(object sender EventArgs e)
        {
            if(“>“ == XBtn.Text)
            {
                this.MaximumSize = new Size(700 300);
                this.MinimumSize = new Size(700 300);
                this.Width = 700;
                XBtn.Text = “<“;
                getBtn.Focus();
            }
            else
            {
                this.MaximumSize = new Size(373 300);
                this.MinimumSize = new Size(373 300);
                this.Width = 373;
                XBtn.Text = “>“;
                getBtn.Focus();
            }
        }

        private void notifyBtn_MouseClick(object sender MouseEventArgs e)
        {
            // 鼠标左键点击托盘图标
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    // 禁止程序自动退出
                    Program.exitFlag = false;
                    //还原窗体
                    this.WindowState = FormWindowState.Normal;
                    //系统任务栏显示图标  
                    this.ShowInTaskbar = true;
                }
                //激活窗体并获取焦点  
                this.Activate();
            }
        }

        private void autoGetChk_CheckedChanged(object sender EventArgs e)
        {
            Properties.Settings.Default.AutoGet = autoGetChk.Checked;
            Properties.Settings.Default.Save();
        }

        private void autoSyncChk_CheckedChanged(obj

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-03 19:13  SyncTime\
     目录           0  2017-10-03 19:13  SyncTime\.vs\
     目录           0  2017-10-03 19:13  SyncTime\.vs\SyncTime\
     目录           0  2017-10-03 19:13  SyncTime\.vs\SyncTime\v14\
     文件       71168  2017-10-05 15:17  SyncTime\.vs\SyncTime\v14\.suo
     目录           0  2017-10-04 21:17  SyncTime\SyncTime\
     文件        1042  2017-10-03 19:36  SyncTime\SyncTime\App.config
     文件       12368  2017-10-04 14:56  SyncTime\SyncTime\MainForm.Designer.cs
     文件       10627  2017-10-04 21:17  SyncTime\SyncTime\MainForm.cs
     文件       35490  2017-10-04 14:56  SyncTime\SyncTime\MainForm.resx
     文件        6517  2017-10-04 17:28  SyncTime\SyncTime\PrivilegeAPI.cs
     文件        4817  2017-10-04 14:35  SyncTime\SyncTime\PrivilegeUtil.cs
     文件        1490  2017-10-04 15:50  SyncTime\SyncTime\Program.cs
     目录           0  2017-10-03 19:36  SyncTime\SyncTime\Properties\
     文件        1324  2017-10-03 19:13  SyncTime\SyncTime\Properties\AssemblyInfo.cs
     文件        2829  2017-10-03 19:13  SyncTime\SyncTime\Properties\Resources.Designer.cs
     文件        5612  2017-10-03 19:13  SyncTime\SyncTime\Properties\Resources.resx
     文件        1969  2017-10-03 19:36  SyncTime\SyncTime\Properties\Settings.Designer.cs
     文件         548  2017-10-03 19:36  SyncTime\SyncTime\Properties\Settings.settings
     文件        4327  2017-10-03 21:19  SyncTime\SyncTime\SyncTime.csproj
     文件        9662  2017-10-03 21:19  SyncTime\SyncTime\T.ico
     文件        1174  2017-10-03 19:45  SyncTime\SyncTime\TimeAPI.cs
     文件        5089  2017-10-04 18:53  SyncTime\SyncTime\TimeUtil.cs
     文件        2691  2017-10-03 19:45  SyncTime\SyncTime\TimeZoneAPI.cs
     文件        4490  2017-10-04 14:44  SyncTime\SyncTime\TimeZoneUtil.cs
     目录           0  2017-10-03 19:28  SyncTime\SyncTime\bin\
     目录           0  2017-10-04 21:35  SyncTime\SyncTime\bin\Debug\
     文件       56320  2017-10-04 21:34  SyncTime\SyncTime\bin\Debug\SyncTime.exe
     文件        1042  2017-10-03 19:36  SyncTime\SyncTime\bin\Debug\SyncTime.exe.config
     文件       42496  2017-10-04 21:34  SyncTime\SyncTime\bin\Debug\SyncTime.pdb
     文件       22696  2017-10-05 15:17  SyncTime\SyncTime\bin\Debug\SyncTime.vshost.exe
............此处省略38个文件信息

评论

共有 条评论