3

I am trying to increase calendar popup's size. Increasing font only increases the height of the calendar box and not the pop up. Dates in popup are still small. Can I do this without using any third party controls etc? If yes how?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • you can use WPF instead. Far better than winforms and very flexible with controls – Mohit S Dec 29 '17 at 09:28
  • @MohitShrivastava I wish I could. But I need to do it in existing application which is a winform application. – SamuraiJack Dec 29 '17 at 09:30
  • @Arbaaz You need to resize the external window of the dropdown. There is an example: [Setting calendar size when overriding DateTimePicker](https://stackoverflow.com/questions/14559074/setting-calendar-size-when-overriding-datetimepicker-to-add-week-numbers). – Jimi Dec 29 '17 at 10:10
  • @Jimi I have tried that solution but it only increases the size of popup but not the size of numbers(dates). It just adds extra padding on the four sided. – SamuraiJack Dec 29 '17 at 10:22
  • @Arbaaz There's another question [Increase Font Size of DateTimePicker Calender](https://stackoverflow.com/questions/16031370/increase-font-size-of-datetimepicker-calender-in-win7-aero-theme) that implies changing the Theme (uxtheme.dll -> SetWindowTheme). I don't know how much "portable" this could be, but it may give you some hints. – Jimi Dec 29 '17 at 10:34
  • @Jimi the secodn question shared by you have only one answer and that answer gives link to the first question shared by you. lol – SamuraiJack Dec 29 '17 at 10:39
  • @Arbaaz Yes, the answer. But I meant the Question. That guy already had the presentation aspect done, he didn't know how to enlarge the container window. Which leads to the answer you already have. – Jimi Dec 29 '17 at 10:41
  • @Jimi Thats great!! It worked! Though it means I will have to add control through code behind right? I cant simply change an existing datepicker to this custom datepicker. – SamuraiJack Dec 29 '17 at 10:57
  • @Arbaaz You can do two things: Use a class that derives from DateTimePicker, subclass it as per the examples and then instantiate it as you would do for any other class, or create a CustomControl. Those examples put together give you 90% ot the work. Then sell it :). – Jimi Dec 29 '17 at 11:21

1 Answers1

3

There is a CalendarFont property which is responsible to get/set font of the drop-down calendar. But the value will be applied just when the Visual Styles are disabled.

You can handle DropDown event of the DateTimePicker and find the MonthCalendar of the drop-down. Then disable Visual Styles just for that control. Then recalculate the required size of the control and set the size of drop-down based on minimum required size of the calendar.

Then the control will show drop-down using the font which you specified in CalendarFont property:

enter image description here

Code

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyDateTimePicker : DateTimePicker
{
    private const int SWP_NOMOVE = 0x0002;
    private const int DTM_First = 0x1000;
    private const int DTM_GETMONTHCAL = DTM_First + 8;
    private const int MCM_GETMINREQRECT = DTM_First + 9;

    [DllImport("uxtheme.dll")]
    private static extern int SetWindowTheme(IntPtr hWnd, string appName, string idList);
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, ref RECT lParam);
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
    int X, int Y, int cx, int cy, int uFlags);
    [DllImport("User32.dll")]
    private static extern IntPtr GetParent(IntPtr hWnd);
    [StructLayout(LayoutKind.Sequential)]
    private struct RECT { public int L, T, R, B; }
    protected override void OnDropDown(EventArgs eventargs)
    {
        var hwndCalendar = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
        SetWindowTheme(hwndCalendar, string.Empty, string.Empty);
        var r = new RECT();
        SendMessage(hwndCalendar, MCM_GETMINREQRECT, 0, ref r);
        var hwndDropDown = GetParent(hwndCalendar);
        SetWindowPos(hwndDropDown, IntPtr.Zero, 0, 0,
            r.R - r.L + 6, r.B - r.T + 6, SWP_NOMOVE);
        base.OnDropDown(eventargs);
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This doesn't work very well at all, I change the 6 to a 400 and it changes the whole thing to be massive with lots of blank space and the buttons to click are no larger. – Jay Croghan Oct 22 '22 at 12:12