c# - 如何在 DateTimePicker 中设置字体大小

标签 c# winforms datetime fonts picker

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

我试过下面的代码,但它不工作。

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

最佳答案

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

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);
    }
}

关于c# - 如何在 DateTimePicker 中设置字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20581204/

相关文章:

python - 从电子邮件中解析带有时区的日期?

mysql - 用于获取一个月中某天的每个第一个事件(按日期时间字段)的 SQL 查询

javascript - AngularJS 模型日期属性 - 向服务器提交了错误的值?

c# - 在 C# 中将类的字段打包到字节数组中

Silverlight GUI 中的 C# 代码验证

c# - .NET 设计 View 不运行 Windows 窗体 OnLoad

vb.net - DataGridView 标题对齐

c# - 如何在通用 Windows 应用程序中设计响应式 UI

c# - 在 SQL Server 中使用带有 URL 部分的单个分层页面表组合 URL

c# - 从用户界面调用System.Threading.Thread时,锁将挂起。