效果图:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Example54
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class SetSystemDateTime //设置系统日期类
{
[DllImportAttribute("Kernel32.dll")]
public static extern void GetLocalTime(SystemTime st);
[DllImportAttribute("Kernel32.dll")]
public static extern void SetLocalTime(SystemTime st);
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public class SystemTime // 系统时间类
{
public ushort vYear;
public ushort vMonth;
public ushort vDayOfWeek;
public ushort vDay;
public ushort vHour;
public ushort vMinitue;
public ushort vSecond;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToString("F") +
" " + DateTime.Now.ToString("dddd");
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定修改当前日期吗?", "信息提示",
MessageBoxButtons.OKCancel) == DialogResult.OK)
{
DateTime Year = dateTimePicker1.Value; //得到时间信息
SystemTime MSTime = new SystemTime(); //创建系统时间类的对象
SetSystemDateTime.GetLocalTime(MSTime);//获取系统时间
MSTime.vYear = (ushort)dateTimePicker1.Value.Year;
MSTime.vMonth = (ushort)dateTimePicker1.Value.Month;
MSTime.vDay = (ushort)dateTimePicker1.Value.Day;
MSTime.vHour = (ushort)dateTimePicker2.Value.Hour;
MSTime.vMinitue = (ushort)dateTimePicker2.Value.Minute;
MSTime.vSecond = (ushort)dateTimePicker2.Value.Second;
SetSystemDateTime.SetLocalTime(MSTime);
button1_Click(null, null);
}
}
}
}