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

代碼
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);//這個(gè)函數(shù)使用的是0時(shí)區(qū)的時(shí)間,例如,要設(shè)12點(diǎn),則為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>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(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 = "時(shí)間和日期";
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 = "設(shè)置時(shí)間";
//
// 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 = "設(shè)置時(shí)間:";
//
// 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 = "當(dāng)前時(shí)間:";
//
// 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 = "設(shè)置任務(wù)欄時(shí)間";
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#實(shí)現(xiàn)設(shè)置系統(tǒng)時(shí)間的文章就介紹到這了,更多相關(guān)C#設(shè)置系統(tǒng)時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)獲取本地內(nèi)網(wǎng)(局域網(wǎng))和外網(wǎng)(公網(wǎng))IP地址的方法分析
這篇文章主要介紹了C#實(shí)現(xiàn)獲取本地內(nèi)網(wǎng)(局域網(wǎng))和外網(wǎng)(公網(wǎng))IP地址的方法,結(jié)合實(shí)例形式總結(jié)分析了C#獲取IP地址相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
C#之Windows自帶打印功能的實(shí)現(xiàn)
這篇文章主要介紹了C#之Windows自帶打印功能的實(shí)現(xiàn)方式,具有很好的價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
C#實(shí)現(xiàn)ArrayList動態(tài)數(shù)組的示例
ArrayList是一個(gè)動態(tài)數(shù)組,可以用來存儲任意類型的元素,本文就來介紹一下C#實(shí)現(xiàn)ArrayList動態(tài)數(shù)組的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12

