2012-02-17 156 views
3

我希望能够通过AutomationElementDateTimePicker元素设置为特定时间。它将时间存储为“hh:mm:ss tt”(即10:45:56 PM)。通过AutomationElement设置DateTimePicker元素

我得到的元素,例如:

ValuePattern p = AECollection[index].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

我相信我有两个选择:

p.SetValue("9:41:22 AM");

p.Current.Value = "9:41:22 AM";

Howev呃,第一个选项根本不起作用(我在某处读到这可能会在.NET 2.0中被破坏,但我使用的是.NET 3.0)。第二个选项告诉我该元素是只读的,我怎样才能改变状态,使它不是只读的?或者更简单地说,我怎么能更改时间:(?

回答

0

你可以得到本地窗口句柄,并发送DTM_SETSYSTEMTIME消息设置所选日期DateTimePicker控制。

要做到这一点,我想你已经找到元素,那么你可以使用follwing代码:

var date = new DateTime(1998, 1, 1); 
DateTimePickerHelper.SetDate((IntPtr)element.Current.NativeWindowHandle, date); 

DateTimePickerHelper

下面是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); 
    } 
} 
相关问题