簡體   English   中英

如何使用DateTimePicker設置SetSystemTime?

[英]How to use DateTimePicker to SetSystemTime?

這可能很簡單,但是在Microsoft Visual Studio Microsoft.NET WinForms CompactFramework v2.0 Windows CE 5.0中看不到可以用於SetSystemTimeDateTimePicker屬性或方法。

編輯:更具體地說,如何從DateTimePicker中獲取所選日期,以便可以將其應用於SetSystemTime

我認為以下代碼段應該有效:

[DllImport("coredll.dll", SetLastError = true)]
static extern bool SetSystemTime(ref SYSTEMTIME time);

[StructLayoutAttribute(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;

    public SYSTEMTIME(DateTime value)
    {
        wYear = value.Year;
        wMonth = value.Month;
        wDayOfWeek = value.DayOfWeek;
        wDay = value.Day;
        wHour = value.Hour;
        wMinute = value.Minute;
        wSecond = value.Second;
        wMilliseconds = value.Milliseconds;
    }
}

public void setTimeButton_Click(object sender, EventArgs e)
{
    // DateTimePicker usually provide with the date but not time information
    // so we need to get the current time
    TimeSpan currentSystemTime = DateTime.Now.TimeOfDay;
    DateTime newDate = newDateTimePicker.Value.Date;
    // Join the date and time parts
    DateTime newDateTime = newDate.Add(currentSystemTime);

    SYSTEMTIME newSystemTime = new SYSTEMTIME(newDateTime);
    if (!SetSystemTime(newSystemTime))
        Debug.WriteLine("Error setting system time.");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM