实现效果:
知识运用:
API函数SetSystemTime
实现代码:
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)] public extern static bool SetSystemTime(ref SYSTEMTIME IpSystemTime); [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; } int mYear; int mMonth; int mDay; private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e) { mYear = e.Start.Year; mMonth = e.Start.Month; mDay = e.Start.Day; } 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); //系统默认UTC时间 要减8 t.Minute = (short)dateTimePicker1.Value.Minute; t.Second = (short)dateTimePicker1.Value.Second; SetSystemTime(ref t); }