2015-07-20 39 views
0

我正在開發一個WFA應用程序,根據點擊按鈕將計算機日期更改爲提前一天或一天​​後。這裏是我的代碼:通過點擊按鈕將工作站更改爲第二天或前一天

public Form1() 
     { 
      InitializeComponent(); 
      currentTime.Start(); 
     } 

     private void CurrentTime_Tick(object sender, EventArgs e) 
     { 
      DateTime dateTime = DateTime.Now; 
      this.lblTime.Text = dateTime.ToString(); 
     } 

     public struct Systemtime 
     { 
      public short WDay; 
     } 

     [DllImport("kernel32.dll", SetLastError = true)] 
     public static extern bool SetSystemTime([In] ref Systemtime st); 

     private void BtnChangeDate_Click(object sender, EventArgs e) 
     { 
      Systemtime st = new Systemtime(); 

      st.WDay++; 

      SetSystemTime(ref st); 
     } 

     private void BtnExit_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void BtnPreviousDay_Click(object sender, EventArgs e) 
     { 
      Systemtime st = new Systemtime(); 

      st.WDay--; 

      SetSystemTime(ref st); 
     } 

我現在的問題是按鈕什麼也不做,即使我有硬編碼的值。我需要做什麼才能正確地完成這項工作?

+0

這可以幫助你:http://stackoverflow.com/questions/650849/change-system-date-programatically – Adam

+0

@Adam - 我已經嘗試過這一點,這是行不通的。它用硬編碼的日子,但我希望用戶能夠移動他們想要的任何一天。 – BowerdotJson

+0

如果您在查找文檔時遇到問題 - [SYSTEMTIME結構](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v = vs.85).aspx) – crashmstr

回答

2

在調用SetSystemTime函數之前,您需要正確設置您的Systemtime結構,僅設置WDay值是不夠的。我會創建一個方法來執行下面的兩種方式。請注意,我使用內置方法獲取日期並添加/減去正確的天數。這可以處理所有情況,例如月/年的開始/結束。

private void ChangeDate(int numDays) 
{ 
    Systemtime st = new Systemtime(); 

    var newDate = DateTime.Now.AddDays(numDays); 

    //Set up all the values, no need to set wDayOfWeek as it's ignored by the set function 
    st.wYear = newDate.Year; 
    st.wMonth = newDate.Month; 
    st.wDay = newDate.Day; 
    st.wHour = newDate.Hour; 
    st.wMinute = newDate.Minute; 
    st.wSecond = newDate.Second; 
    st.wMilliseconds = newDate.Millisecond; 

    SetSystemTime(ref st); 
} 

現在你罵它:

//Forward 1 day 
ChangeDate(1); 

//Back 1 day 
ChangeDate(-1); 
+0

好吧,我有一直在調試你的方法,它正在改變程序中的日期,但不是在我的機器上。它也在我的CurrentTime_Tick方法中創建了一個無限循環,所以我認爲這是問題所在。 – BowerdotJson

0

這裏是解決方案。

public partial class Form1 : Form 
{ 
    [DllImport("kernel32.dll")] 
    static extern bool SetSystemTime([In] ref SYSTEMTIME st); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public struct SYSTEMTIME 
    { 
     public ushort wYear; 
     public ushort wMonth; 
     public ushort wDayOfWeek; 
     public ushort wDay; 
     public ushort wHour; 
     public ushort wMinute; 
     public ushort wSecond; 
     public ushort wMilliseconds; 
     public void FromDateTime(DateTime time) 
     { 
      wYear = (ushort)time.Year; 
      wMonth = (ushort)time.Month; 
      wDayOfWeek = (ushort)time.DayOfWeek; 
      wDay = (ushort)time.Day; 
      wHour = (ushort)time.Hour; 
      wMinute = (ushort)time.Minute; 
      wSecond = (ushort)time.Second; 
      wMilliseconds = (ushort)time.Millisecond; 
     } 
     public DateTime ToDateTime() 
     { 
      return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds); 
     } 
     public static DateTime ToDateTime(SYSTEMTIME time) 
     { 
      return time.ToDateTime(); 
     } 
    } 

    private void BtnChangeDate_Click(object sender, EventArgs e) 
    { 
     ChangeDateUp(0); 
    } 

    private void BtnPreviousDay_Click(object sender, EventArgs e) 
    { 
     ChangeDateDown(0); 
    } 
    private void ChangeDateUp(int numDays) 
    {    
     DateTime t = DateTime.Now.AddDays(1); 
     SYSTEMTIME st = new SYSTEMTIME(); 
     st.FromDateTime(t); 
     SetSystemTime(ref st); 
    } 

    private void ChangeDateDown(int numDays) 
    { 
     DateTime t = DateTime.Now.AddDays(-1); 
     SYSTEMTIME st = new SYSTEMTIME(); 
     st.FromDateTime(t); 
     SetSystemTime(ref st); 
    } 

    private void BtnExit_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
} 
相關問題