Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to increase size of calendar popup in winform?

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?

like image 537
SamuraiJack Avatar asked Oct 23 '25 21:10

SamuraiJack


1 Answers

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);
    }
}
like image 88
Reza Aghaei Avatar answered Oct 25 '25 12:10

Reza Aghaei