详解如何利用C#实现设置系统时间
作者:芝麻粒儿 发布时间:2023-09-04 13:55:05
标签:C#,系统,时间
实践过程
效果
代码
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;
}
来源:https://blog.csdn.net/qq_27489007/article/details/128306502
0
投稿
猜你喜欢
- Android 应用启动欢迎界面广告0.写在前面在这篇教程中来实现一个类似于微信的的延迟3秒再进入主界面的效果。1.项目准备先新建一个空的a
- Spring课程工程构建 在IDEA 工作空间里面创建Spring文件夹。打开IDEA,file–>Open&am
- springboot读取文件,打成jar包后访问不到最新开发出现一种情况,springboot打成jar包后读取不到文件,原因是打包之后,文
- 一、概述针对八种基本数据类型定义相应的引用类型—包装类(封装类)。二、作用有了类的特点,就可以调用类中的方法,Java才
- HashMap的原理 HashMap的数据结构为数组+链表,以key,value的形式存值,通过调用put与get方法来存值与取值。它内部维
- 最初,XML 语言仅仅是意图用来作为 HTML 语言的替代品而出现的,但是随着该语言的不断发展和完善,人们越来越发现它所具有的优点:例如标记
- 如下所示:package test.com.gjob.services; import java.util.Properties
- Android5.0之后提供了JobService和JobScheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务
- 一、问题Flutter原有的图片缓存机制,是通过PaintingBinding.instance!.imageCache来管理缓存的,这个缓
- 前言老规矩,还是从最简单粗暴的开始。那么多简单算简单?多粗暴算粗暴?我告诉你可以不写一句代码,你信吗?直接把一个文件往IIS服务器上一扔,就
- Java Function的使用一、方法介绍表示接受一个参数并产生结果的函数。参数类型 T - 函数输入的类型R - 函数的结果类型方法介绍
- 前端代码:<form id= "uploadForm"> <p >指定文件名:
- 条码的应用已深入生活和工作的方方面面。在处理条码时,常需要和各种文档格式相结合。当需要在文档中插入、编辑或者删除条码时,可借助于一些专业的类
- zuul添加或修改请求参数一、为什么要用到这个在基于 springcloud 构建的微服务系统中,通常使用网关zuul来进行一些用户验证等过
- mybatis oracle 自增序列的代码如下所示:<insert id=" insert " useGener
- 前言序列化想必大家都很熟悉了,对象在进行网络传输过程中,需要序列化之后才能传输到客户端,或者客户端的数据序列化之后送达到服务端序列化的标准解
- 在Java中我们知道静态变量会在类加载时机的“初始化”阶段得到赋值(编译器会收集类中的静态变量及静态
- IntelliJ IDEA安装好以后,按说我们就要双击进行启动了,但在启动之前,我得给大家说一下IntelliJ IDEA安装以后的安装目录
- 基于springboot+vue的测试平台开发继续更新。一、前端Tree树形控件的append方法在elementUI 树控件下有个appe
- Bean Searcher 号称 任何复杂的查询都可以 一行代码搞定,但 Mybatis Plus 似乎也有类似的动态查询功能,它们有怎样的