本文介绍了如何在DateTimePicker中设置字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Window Form C#中的DateTimePicker中增加字体大小。我想在DateTime选择器中设置最小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;
//获取日历控件的句柄 - 禁用主题
IntPtr hCalendar = SendMessage(this.Handle,DTM_GETMONTHCAL,IntPtr.Zero,IntPtr.Zero);
if(hCalendar!= IntPtr.Zero)
{
SetWindowTheme(hCalendar,,);
//获取父弹出窗口的句柄 - 适当调整大小
IntPtr hParent = GetParent(hCalendar);
if(hParent!= IntPtr.Zero)
{
//您在此处指定的大小取决于所选的CalendarFont大小
MoveWindow(hParent,0,0,400, 300,true);
}
}
}
base.OnDropDown(eventargs);
}
}
I want to increase font size in DateTimePicker in Window Form C#. I want to set minimum 14 to 16 font size in DateTime picker.
I have tried below code but it's not working.
dateTimePicker1.CalendarFont = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));
解决方案
If you wish to retain visual styles for the other controls in your application, but disable for the date picker's drop down only, you can use the following code:
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);
}
}
这篇关于如何在DateTimePicker中设置字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!