如何在DateTimePicker中设置字体大小

时间:2013-12-14 08:31:40

标签: c# winforms datetime fonts picker

我想在Window Form C#中增加DateTimePicker中的字体大小。我想在DateTime选择器中设置最小14到16的字体大小。

我尝试了下面的代码,但它没有用。

dateTimePicker1.CalendarFont = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));

3 个答案:

答案 0 :(得分:6)

如果您希望保留应用程序中其他控件的视觉样式,但仅禁用日期选择器的下拉列表,则可以使用以下代码:

public class MyDateTimePicker : DateTimePicker
{
    [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
    static extern Int32 SetWindowTheme(IntPtr hWnd, String textSubAppName, String textSubIdList);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetParent(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    protected override void OnDropDown(EventArgs eventargs)
    {
        if (Application.RenderWithVisualStyles)
        {
            const int DTM_GETMONTHCAL = 0x1008;

            //Get handle of calendar control - disable theming
            IntPtr hCalendar = SendMessage(this.Handle, DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
            if (hCalendar != IntPtr.Zero)
            {
                SetWindowTheme(hCalendar, "", "");

                //Get handle of parent popup - resize appropriately
                IntPtr hParent = GetParent(hCalendar);
                if (hParent != IntPtr.Zero)
                {
                    //The size you specify here will depend on the CalendarFont size chosen
                    MoveWindow(hParent, 0, 0, 400, 300, true);
                }
            }
        }

        base.OnDropDown(eventargs);
    }
}

答案 1 :(得分:0)

在主程序删除/注释行Application.EnableVisualStyles();

并在你的后面添加新代码:

dateTimePicker1.Font = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));

答案 2 :(得分:-1)

在iOS中,你必须制作一个渲染器:

private void SetFont(CustomPicker view)
{
    UIFont uiFont;
    Control.Font = UIFont.SystemFontOfSize(11f); //the size which u want

}

在android中你必须设置渲染器的字体:

private void SetFont(CustomDatePicker view)
{
    if (view.Font != Font.Default) 
    {
        Control.TextSize = view.Font.ToScaledPixel();
        Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets,"Roboto-Bold.ttf");
        Control.Typeface = font;
        Control.Typeface = view.Font.ToTypeface();
    }
}