0

I have created a class change the appearance of the calander. The class is based on these previous stackoverflow questions:

Source1: Setting calendar size when overriding DateTimePicker to add week numbers

Source2: Increase Font Size of DateTimePicker Calender in Win7 Aero Theme

This is the 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);
    }
}

Afther implementing this class the visible text of the datetimepicker changes to whatever you choose. But when using dateTimePickerImpl.Value it always returns the it began with (the time the datetimpicker loaded). I already found out by adding a method to the ValueChanged event that this event never happens. I found some similar problems online but these were all solved by setting the checked property of the datetimepicker to true. In my case the checked property is already true.

What did i do to break the datetimepicker?

Community
  • 1
  • 1
Maiko Kingma
  • 929
  • 3
  • 14
  • 29
  • 1
    I tested and it works properly for me. – Reza Aghaei Oct 01 '15 at 13:23
  • It works on my Windows7 machine. The `ValueChanged` event fires. The `Value` value is correct. Do you have more than one calendar control on your form? If so, are you certain you're testing the correct one? – Ulric Oct 01 '15 at 13:39

1 Answers1

0

I had to create the datetime picker manually instead of dragging it from the toolbox into the designer.

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);
Maiko Kingma
  • 929
  • 3
  • 14
  • 29