通过 AutomationElement 设置 DateTimePicker 元素

发布于 2025-01-06 11:00:10 字数 503 浏览 1 评论 0原文

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

我得到这样的元素:

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)。第二个选项告诉我该元素是只读的,如何更改状态以使其不是只读的?或者更简单地说,我如何更改时间:(?

I want to be able to set a DateTimePicker element to a certain time via AutomationElement. It stores the time as "hh:mm:ss tt" (i.e. 10:45:56 PM).

I get the element as such:

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

I believe I have two options:

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

or

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

However, the first option simply doesn't work (I read somewhere that this may be broken in .NET 2.0?, I am using .NET 3.0 though). The second option tells me that the element is read only, how can I change the status so that it is not read only? Or more simply, how can I change the time :( ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流绪微梦 2025-01-13 11:00:10

您可以获取本机窗口句柄并发送DTM_SETSYSTEMTIME消息来设置DateTimePicker控件的选定日期。

为此,我想您已经找到了该元素,然后您可以使用以下代码:

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);
    }
}

You can get the native window handle and send DTM_SETSYSTEMTIME message to set the selected date for DateTimePicker control.

To do so, I suppose you have found the element, then you can use follwing code:

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

DateTimePickerHelper

Here is the source code for DateTimePickerHelper. The class has a public static SetDate method which allow you to set a date for the date time picker control:

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);
    }
}
物价感观 2025-01-13 11:00:10

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

object patternObj = AECollection[index].GetCurrentPattern(UIA.UIA_PatternIds.UIA_ValuePatternId);
if (patternObj != null) {
 (UIA.IUIAutomationValuePattern)patternObj.SetValue(itemVal);
}

This solution works for Wpf based app

object patternObj = AECollection[index].GetCurrentPattern(UIA.UIA_PatternIds.UIA_ValuePatternId);
if (patternObj != null) {
 (UIA.IUIAutomationValuePattern)patternObj.SetValue(itemVal);
}
夏の忆 2025-01-13 11:00:10

我来到这里是因为我遇到了同样的问题。

我尝试通过以下方式设置 DateTimePicker 日期:

  • UIAutomation ValuePattern (无输入)
  • FlaUI Element as DateTimePicker (有例外)
  • Reza Aghaei 提到的解决方案(有例外)

您必须聚焦应用程序窗口,然后是 DateTimePicker,然后输入使用 SendKeys.SendWait 方法的日期。

来源:

这是使用 FlaUI 包的示例代码:

// This code uses the FlaUI package to retrieve the element
var automationId = "AUTOMATION ID OF YOUR DATETIME PICKER";
var process = Process.GetProcesses().First(p => p.MainWindowTitle.Contains("YOUR APPLICATION WINDOW TITLE"));
var application = Application.Attach(process.Id);
var appWindow = application.GetMainWindow(new UIA3Automation(), TimeSpan.FromSeconds(10));
var dateTimePicker = appWindow.FindFirstDescendant(cf => cf.ByAutomationId(automationId));

var targetTime = DateTime.Parse("13.04.2024 08:00");
appWindow.Focus();
datePicker.Focus();
SendKeys.SendWait("^{HOME}");
SendKeys.SendWait($"{targetTime:dd}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:MM}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:yyyy}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:HH}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait($"{targetTime:ss}");

我知道该示例代码可以简化,但这就是它在我的机器上的工作方式。

因此,我将其原样发布在这里。我希望这有帮助。

I came here because I had the same problem.

I've tried setting the DateTimePicker Date via:

  • UIAutomation ValuePattern (no input)
  • FlaUI Element as DateTimePicker (got an exception)
  • the solution mentioned by Reza Aghaei (got an exception)

You have to focus the application window, then the DateTimePicker and then enter the date using the SendKeys.SendWait method.

Sources:

Here's an example code that uses the FlaUI package:

// This code uses the FlaUI package to retrieve the element
var automationId = "AUTOMATION ID OF YOUR DATETIME PICKER";
var process = Process.GetProcesses().First(p => p.MainWindowTitle.Contains("YOUR APPLICATION WINDOW TITLE"));
var application = Application.Attach(process.Id);
var appWindow = application.GetMainWindow(new UIA3Automation(), TimeSpan.FromSeconds(10));
var dateTimePicker = appWindow.FindFirstDescendant(cf => cf.ByAutomationId(automationId));

var targetTime = DateTime.Parse("13.04.2024 08:00");
appWindow.Focus();
datePicker.Focus();
SendKeys.SendWait("^{HOME}");
SendKeys.SendWait(
quot;{targetTime:dd}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait(
quot;{targetTime:MM}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait(
quot;{targetTime:yyyy}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait(
quot;{targetTime:HH}");
SendKeys.SendWait("{RIGHT}");
SendKeys.SendWait(
quot;{targetTime:ss}");

I am aware that the example code can be simplified, but this is how it worked on my machine.

Therefore, I posted it here as it is. I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文