亲宝软件园·资讯

展开

详解如何利用C#实现设置系统时间

芝麻粒儿 人气:0

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    //api函数声明   
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
    public extern static bool SetSystemTime(ref SYSTEMTIME time); 
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
        public short Year;
        public short Month;
        public short DayOfWeek;
        public short Day;
        public short Hour;
        public short Minute;
        public short Second;
        public short Miliseconds;
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        lblNowTime.Text = DateTime.Now.ToString();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        lblNowTime.Text = DateTime.Now.ToString();
        Microsoft.Win32.SystemEvents.TimeChanged+=new EventHandler(SystemEvents_TimeChanged);
    }

    private void SystemEvents_TimeChanged(object sender, EventArgs e)
    {
        MessageBox.Show("系统日期修改成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mYear = monthCalendar1.SelectionRange.Start.Year;
        mMonth = monthCalendar1.SelectionRange.Start.Month;
        mDay = monthCalendar1.SelectionRange.Start.Day;
        //调用代码   
        SYSTEMTIME t = new SYSTEMTIME();
        t.Year = (short)mYear;
        t.Month = (short)mMonth;
        t.Day = (short)mDay;
        t.Hour = (short)(dateTimePicker1.Value.Hour - 8);//这个函数使用的是0时区的时间,例如,要设12点,则为12-8   
        t.Minute = (short)dateTimePicker1.Value.Minute;
        t.Second = (short)dateTimePicker1.Value.Second;
        bool v = SetSystemTime(ref t);   
    }
    int mYear;
    int mDay;
    int mMonth;
    private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
        mYear = e.Start.Year;
        mMonth = e.Start.Month;
        mDay =e.Start.Day;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.tabPage1 = new System.Windows.Forms.TabPage();
        this.button2 = new System.Windows.Forms.Button();
        this.button1 = new System.Windows.Forms.Button();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
        this.label2 = new System.Windows.Forms.Label();
        this.lblNowTime = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.tabControl1.SuspendLayout();
        this.tabPage1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // monthCalendar1
        // 
        this.monthCalendar1.Location = new System.Drawing.Point(4, 15);
        this.monthCalendar1.Name = "monthCalendar1";
        this.monthCalendar1.ShowToday = false;
        this.monthCalendar1.ShowTodayCircle = false;
        this.monthCalendar1.TabIndex = 0;
        this.monthCalendar1.TitleBackColor = System.Drawing.Color.LightSkyBlue;
        this.monthCalendar1.TitleForeColor = System.Drawing.Color.Black;
        this.monthCalendar1.TrailingForeColor = System.Drawing.Color.White;
        this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);
        // 
        // tabControl1
        // 
        this.tabControl1.Controls.Add(this.tabPage1);
        this.tabControl1.Location = new System.Drawing.Point(4, 4);
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(300, 335);
        this.tabControl1.TabIndex = 1;
        // 
        // tabPage1
        // 
        this.tabPage1.Controls.Add(this.button2);
        this.tabPage1.Controls.Add(this.button1);
        this.tabPage1.Controls.Add(this.groupBox2);
        this.tabPage1.Controls.Add(this.groupBox1);
        this.tabPage1.Location = new System.Drawing.Point(4, 21);
        this.tabPage1.Name = "tabPage1";
        this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage1.Size = new System.Drawing.Size(292, 310);
        this.tabPage1.TabIndex = 0;
        this.tabPage1.Text = "时间和日期";
        this.tabPage1.UseVisualStyleBackColor = true;
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(163, 279);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 4;
        this.button2.Text = "取消";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(56, 279);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 3;
        this.button1.Text = "确定";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.dateTimePicker1);
        this.groupBox2.Controls.Add(this.label2);
        this.groupBox2.Controls.Add(this.lblNowTime);
        this.groupBox2.Controls.Add(this.label1);
        this.groupBox2.Location = new System.Drawing.Point(9, 182);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(277, 91);
        this.groupBox2.TabIndex = 2;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "设置时间";
        // 
        // dateTimePicker1
        // 
        this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Time;
        this.dateTimePicker1.Location = new System.Drawing.Point(80, 55);
        this.dateTimePicker1.Name = "dateTimePicker1";
        this.dateTimePicker1.ShowUpDown = true;
        this.dateTimePicker1.Size = new System.Drawing.Size(90, 21);
        this.dateTimePicker1.TabIndex = 3;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(9, 59);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(65, 12);
        this.label2.TabIndex = 2;
        this.label2.Text = "设置时间:";
        // 
        // lblNowTime
        // 
        this.lblNowTime.AutoSize = true;
        this.lblNowTime.Location = new System.Drawing.Point(81, 29);
        this.lblNowTime.Name = "lblNowTime";
        this.lblNowTime.Size = new System.Drawing.Size(0, 12);
        this.lblNowTime.TabIndex = 1;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(9, 29);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 0;
        this.label1.Text = "当前时间:";
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.monthCalendar1);
        this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.groupBox1.Location = new System.Drawing.Point(8, 5);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(278, 169);
        this.groupBox1.TabIndex = 1;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "选择日期";
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(306, 341);
        this.Controls.Add(this.tabControl1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "设置任务栏时间";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.tabControl1.ResumeLayout(false);
        this.tabPage1.ResumeLayout(false);
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        this.groupBox1.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.MonthCalendar monthCalendar1;
    private System.Windows.Forms.TabControl tabControl1;
    private System.Windows.Forms.TabPage tabPage1;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.DateTimePicker dateTimePicker1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label lblNowTime;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Timer timer1;

}

加载全部内容

相关教程
猜你喜欢
用户评论