自动化.net月日历窗口

时间:2018-02-16 17:35:57

标签: .net winforms ui-automation hp-uft monthcalendar

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

1 个答案:

答案 0 :(得分:1)

您可以获取原生窗口句柄并发送MCM_SETSELRANGE消息以设置MonthCalendar的所选日期。

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

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

<强> MonthCalendarHelper

以下是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);
    }
}