您可以获取本机窗口句柄并发送DTM_SETSYSTEMTIME
消息以设置所选日期以进行DateTimePicker
控制。
为此,我想您已经找到了该元素,那么您可以使用以下代码:
var date = new DateTime(1998, 1, 1);
DateTimePickerHelper.SetDate((IntPtr)element.Current.NativeWindowHandle, date);
日期时间选择器助手
这里是源代码DateTimePickerHelper
。该类有一个公共静态SetDate
方法,允许您为日期时间选择器控件设置日期:
using System;
using System.Runtime.InteropServices;
public class DateTimePickerHelper {
const int GDT_VALID = 0;
const int DTM_SETSYSTEMTIME = (0x1000 + 2);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
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;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg,
int wParam, SYSTEMTIME lParam);
public static void SetDate(IntPtr handle, DateTime date) {
var value = new SYSTEMTIME() {
wYear = (short)date.Year,
wMonth = (short)date.Month,
wDayOfWeek = (short)date.DayOfWeek,
wDay = (short)date.Day,
wHour = (short)date.Hour,
wMinute = (short)date.Minute,
wSecond = (short)date.Second,
wMilliseconds = 0
};
SendMessage(handle, DTM_SETSYSTEMTIME, 0, value);
}
}