詳解如何利用C#實現(xiàn)設置系統(tǒng)時間
更新時間:2022年12月20日 08:36:07 作者:芝麻粒兒
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)設置系統(tǒng)時間功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實踐過程
效果

代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//api函數(shù)聲明
[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("系統(tǒng)日期修改成功!","提示",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;
//調(diào)用代碼
SYSTEMTIME t = new SYSTEMTIME();
t.Year = (short)mYear;
t.Month = (short)mMonth;
t.Day = (short)mDay;
t.Hour = (short)(dateTimePicker1.Value.Hour - 8);//這個函數(shù)使用的是0時區(qū)的時間,例如,要設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>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </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;
}
到此這篇關(guān)于詳解如何利用C#實現(xiàn)設置系統(tǒng)時間的文章就介紹到這了,更多相關(guān)C#設置系統(tǒng)時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用委托(delegate)實現(xiàn)在兩個form之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了C#使用委托(delegate)實現(xiàn)在兩個form之間傳遞數(shù)據(jù)的方法,涉及C#委托的使用技巧,需要的朋友可以參考下2015-04-04
C#正則表達式分解和轉(zhuǎn)換IP地址實例(C#正則表達式大全 c#正則表達式語法)
這是我發(fā)了不少時間整理的C#的正則表達式,新手朋友注意一定要手冊一下哦,這樣可以節(jié)省很多寫代碼的時間。下面進行了簡單總結(jié)2013-12-12
.net2.0+ Winform項目實現(xiàn)彈出容器層
在實際工作中,如果能像菜單一樣彈出自定義內(nèi)容,會方便很多,比如查詢時,比如下拉列表顯示多列信息時,比如在填寫某個信息需要查看一些信息樹時。這個時候自定義彈出界面就顯的非常重要了2015-08-08

