網站首頁 編程語言 正文
實踐過程
效果
代碼
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
相關推薦
- 2022-05-26 flutter實現底部抽屜效果_Android
- 2022-10-19 為什么不要在?Flutter?中使用全局變量_Android
- 2022-08-20 解決WPF繪制矢量圖形模糊的問題_C#教程
- 2022-05-22 C#單例模式與多線程用法介紹_C#教程
- 2022-02-12 uni-app 自定義導航欄 圖片按鈕
- 2022-01-30 composer 安裝包,提示找不到對應的包,很奇怪的問題,備忘
- 2022-07-07 淺談Redis的異步機制_Redis
- 2023-01-03 C++?Boost?MetaStateMachine定義狀態機超詳細講解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支