自动化 .net 月历窗口

Posted

技术标签:

【中文标题】自动化 .net 月历窗口【英文标题】:Automating .net month calendar window 【发布时间】:2018-02-16 17:35:57 【问题描述】:

如何使用 QTP 在 .net 应用的月历对象中设置随机日期?窗口没有直接插入日期的输入框。该日历 UIAObject 的类名是 WindowsForms10.SysMonthCal32。

【问题讨论】:

【参考方案1】:

您可以获取本机窗口句柄并发送MCM_SETSELRANGE消息以设置MonthCalendar的选定日期。

为此,我想你已经找到了元素,那么你可以使用以下代码:

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

月历助手

这里是MonthCalendarHelper 的源代码。该类有两个公共静态方法,允许您为月历控件设置日期范围或选定日期:

using System;
using System.Runtime.InteropServices;

public class MonthCalendarHelper

    const int MCM_SETSELRANGE = (0x1000 + 6);
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    struct SYSTEMTIMEARRAY
    
        public short wYear1;
        public short wMonth1;
        public short wDayOfWeek1;
        public short wDay1;
        public short wHour1;
        public short wMinute1;
        public short wSecond1;
        public short wMilliseconds1;
        public short wYear2;
        public short wMonth2;
        public short wDayOfWeek2;
        public short wDay2;
        public short wHour2;
        public short wMinute2;
        public short wSecond2;
        public short wMilliseconds2;
    
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, int msg,
        int wParam, SYSTEMTIMEARRAY lParam);

    public static void SetDate(IntPtr handle, DateTime date)
    
        SetDateRange(handle, date, date);
    
    public static void SetDateRange(IntPtr handle, DateTime start, DateTime end)
    
        var value = new SYSTEMTIMEARRAY();
        value.wYear1 = (short)start.Year;
        value.wMonth1 = (short)start.Month;
        value.wDayOfWeek1 = (short)start.DayOfWeek;
        value.wDay1 = (short)start.Day;
        value.wYear2 = (short)end.Year;
        value.wMonth2 = (short)end.Month;
        value.wDayOfWeek2 = (short)end.DayOfWeek;
        value.wDay2 = (short)end.Day;
        SendMessage(handle, MCM_SETSELRANGE, 0, value);
    

【讨论】:

感谢您的快速回复。会检查的。

以上是关于自动化 .net 月历窗口的主要内容,如果未能解决你的问题,请参考以下文章

月历 vb.net 多个日期选择

VB.Net:动态创建的月历不会触发 Lost Focus 或 GotFocus

是否有 .NET 文化相关的“今天”字符串?

如何突出显示月历控件中的日期?

从窗口句柄获取自定义 .NET 类的组件类型

VB .NET SHDocVw.InternetExplorer:Windows 安全弹出窗口