Datetimepicker 文本值更改但值未给出预期值

Datetimepicker text value changes but value doesn't give the expected value

我创建了一个 class 更改压光机的外观。 class 基于这些先前的 Whosebug 问题:

来源 1:Setting calendar size when overriding DateTimePicker to add week numbers

来源 2:Increase Font Size of DateTimePicker Calender in Win7 Aero Theme

这是class:

class DateTimePickerImpl : DateTimePicker
{
    private const int McmFirst = 0x1000;
    private const int McmGetminreqrect = (McmFirst + 9);
    private const int McsWeeknumbers = 0x4;
    private const int DtmFirst = 0x1000;
    private const int DtmGetmonthcal = (DtmFirst + 8);

    [DllImport("User32.dll")]
    private static extern int GetWindowLong(IntPtr handleToWindow, int offsetToValueToGet);
    [DllImport("User32.dll")]
    private static extern int SetWindowLong(IntPtr h, int index, int value);
    [DllImport("uxtheme.dll")]
    private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
    [DllImport("User32.dll")]
    private static extern IntPtr SendMessage(IntPtr h, int msg, int param, int data);
    [DllImport("User32.dll")]
    private static extern IntPtr GetParent(IntPtr h);
    [DllImport("User32.dll")]
    private static extern int SendMessage(IntPtr h, int msg, int param, ref Rectangle data);
    [DllImport("User32.dll")]
    private static extern int MoveWindow(IntPtr h, int x, int y, int width, int height, bool repaint);

    protected override void OnDropDown(EventArgs e)
    {
        CalendarFont = new Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        const int offsetToGetWindowsStyles = (-16);

        IntPtr pointerToCalenderWindow = SendMessage(Handle, DtmGetmonthcal, 0, 0);
        SetWindowTheme(pointerToCalenderWindow, "", "");
        int styleForWindow = GetWindowLong(pointerToCalenderWindow, offsetToGetWindowsStyles);

        styleForWindow = styleForWindow | McsWeeknumbers;

        Rectangle rect = new Rectangle();
        SendMessage(pointerToCalenderWindow, McmGetminreqrect, 0, ref rect);

        rect.Width = rect.Width + 30;
        rect.Height = rect.Height + 10;

        SetWindowLong(pointerToCalenderWindow, offsetToGetWindowsStyles, styleForWindow);

        MoveWindow(pointerToCalenderWindow, 0, 0, rect.Width, rect.Height, true);

        IntPtr parentWindow = GetParent(pointerToCalenderWindow);
        MoveWindow(parentWindow, 0, 0, rect.Width, rect.Height, true);

        base.OnDropDown(e);
    }
}

实施此 class 后,日期时间选择器的可见文本将更改为您选择的任何内容。但是当使用 dateTimePickerImpl.Value 时,它总是 returns 它开始的时间(加载 datetimpicker 的时间)。我已经通过向 ValueChanged 事件添加一个方法发现该事件永远不会发生。我在网上发现了一些类似的问题,但这些都是通过将 datetimepicker 的 checked 属性 设置为 true 来解决的。在我的例子中 checked 属性 已经是真的。

我做了什么破坏了日期时间选择器?

我不得不手动创建日期时间选择器,而不是将它从工具箱拖到设计器中。

DateTimePickerImpl dtp = new DateTimePickerImpl();
dtp.Location = new Point(3, 254);
dtp.Name = "dtp";
dtp.Size = new Size(271, 26);
dtp.TabIndex = 25;
panel1.Controls.Add(dtp);