4

我希望能够DateTimePicker通过AutomationElement. 它将时间存储为“hh:mm:ss tt”(即晚上 10:45:56)。

我得到这样的元素:

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

我相信我有两个选择:

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

或者

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

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

4

2 回答 2

1

您可以获取本机窗口句柄并发送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);
    }
}
于 2018-02-16T19:14:53.353 回答
0

此解决方案适用于基于 Wpf 的应用程序

object patternObj = AECollection[index].GetCurrentPattern(UIA.UIA_PatternIds.UIA_ValuePatternId);
if (patternObj != null) {
 (UIA.IUIAutomationValuePattern)patternObj.SetValue(itemVal);
}
于 2022-01-24T11:25:47.113 回答