问题描述
在DateTimePicker控件,当我们点击下拉按钮,日历获取显示。在日历的底部,有一个按钮:今天,这对被点击,设置当前日期的选定日期。
我想删除/隐藏控制该按钮。我该怎么办呢?
In the DateTimePicker control, when we click the dropdown button , a calendar gets displayed . at the bottom of the calendar , there is a button : Today , which on being clicked , sets the present date as the selected date.I want to remove/hide that button from the control. How can I do it?
推荐答案
本地的Windows DateTimePicker控件支持DTM_SETMCSTYLE消息设置月压延机的风格。你只需要一点点的PInvoke创建控件时发送消息并更改默认样式。添加一个新类到您的项目并粘贴如下所示的代码。编译。从工具箱到窗体顶部掉落新的控制,换旧人。
The native Windows DateTimePicker control supports the DTM_SETMCSTYLE message to set the style of the month calender. You just need a little pinvoke to send the message when the control is created and change the default style. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the old one.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class MyDateTimePicker : DateTimePicker {
protected override void OnHandleCreated(EventArgs e) {
int style = (int)SendMessage(this.Handle, DTM_GETMCSTYLE, IntPtr.Zero, IntPtr.Zero);
style |= MCS_NOTODAY | MCS_NOTODAYCIRCLE;
SendMessage(this.Handle, DTM_SETMCSTYLE, IntPtr.Zero, (IntPtr)style);
base.OnHandleCreated(e);
}
//pinvoke:
private const int DTM_FIRST = 0x1000;
private const int DTM_SETMCSTYLE = DTM_FIRST + 11;
private const int DTM_GETMCSTYLE = DTM_FIRST + 12;
private const int MCS_NOTODAYCIRCLE = 0x0008;
private const int MCS_NOTODAY = 0x0010;
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
它看起来像这样在运行时,在Windows 7上:
It looks like this at runtime, on Windows 7:
这篇关于我怎样才能去除"今天"从DateTimePicker控件按钮(Windows窗体控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!