首页 新闻 会员 周边 捐助

求改系统时间的c++程序源码,要求弹出对话框,点击按钮每次增加系统时间6分钟,谢谢

0
悬赏园豆:180 [已解决问题] 解决于 2012-12-14 23:38

求改系统时间的c++程序源码,要求弹出对话框,点击按钮每次增加系统时间6分钟,谢谢

求改系统时间的c++程序源码,要求弹出对话框,点击按钮每次增加系统时间6分钟,谢谢

门外汉躬身求教的主页 门外汉躬身求教 | 初学一级 | 园豆:40
提问于:2012-12-13 17:44
< >
分享
最佳答案
0
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 SystemDate
{
    public partial class FrmSystemDate : Form
    {
        public DateTime dtTrue;
        //api函数声明
        //[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
        [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "SetLocalTime")]
        public extern static bool SetSystemTime(ref SYSTEMTIME time);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern long GetLastError();

        public FrmSystemDate()
        {
            InitializeComponent();
        }

        private void dtpDateNow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UpdateSystemDateTime();
            }
        }

        private void UpdateSystemDateTime()
        {

            DateTime dtNow = dtpDateNow.Value;
            SYSTEMTIME t = new SYSTEMTIME() { };
            t.wYear = (short)dtNow.Year;
            t.wMonth = (short)dtNow.Month;
            t.wDay = (short)dtNow.Day;
            t.wHour = (short)dtNow.Hour; //这个函数使用的是0时区的时间,对于用+8时区的,时间要自己算一下.如要设12点,则为12-8
            t.wMinute = (short)dtNow.Minute;
            t.wSecond = (short)dtNow.Second;
            bool v = SetSystemTime(ref t);
            if (!v)
            {
                long errorCode = GetLastError();
                //int code = Marshal.GetLastWin32Error();//.net常用这种方式代替GetLastError API
                MessageBox.Show("修改失败,Win32错误代码是{0},请查看GetLastError返回值的意义列表或调用FormatMessage查看" + errorCode.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dtTrue = DateTime.Now;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateSystemDateTime();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            dtpDateNow.Value = dtTrue;
            UpdateSystemDateTime();
        }

        private void MyTimer_Tick(object sender, EventArgs e)
        {
            dtTrue = dtTrue.AddMilliseconds(100);
            lblDateNow.Text = "实际时间:" + dtTrue.ToString("yyy-MM-dd HH:mm:ss");
        }

        private void btnUpdate_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                UpdateSystemDateTime();
        }

        private void btnReset_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.R)
                btnReset_Click(null, null);
        }

        private void cbChangeTime_CheckedChanged(object sender, EventArgs e)
        {
            if (cbChangeTime.Checked)
            {
                TimerAdd.Enabled = true;
                TimerAdd.Start();
            }
            else
            {
                TimerAdd.Enabled = false;
                TimerAdd.Stop();
            }
        }

        private void TimerAdd_Tick(object sender, EventArgs e)
        {
            DateTime dtValue = dtpDateNow.Value;
            dtpDateNow.Value = dtValue.AddMinutes(30);
            UpdateSystemDateTime();
        }

    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME
    {
        public short wYear;
        public short wMonth;
        public short wDayOfWeek;
        public short wDay;
        public short wHour;
        public short wMinute;
        public short wSecond;
        public short wMilliseconds;
    }




}

这是C#版的

收获园豆:180
JarviseZhou | 菜鸟二级 |园豆:382 | 2012-12-14 11:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册