4

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?

4

2 回答 2

12

本机 Windows DateTimePicker 控件支持 DTM_SETMCSTYLE 消息来设置月历的样式。创建控件并更改默认样式时,您只需要一点 pinvoke 即可发送消息。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上,替换旧控件。

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 上:

在此处输入图像描述

于 2012-06-06T20:11:05.057 回答
0

我似乎记得不久前想自己修改这个控件,然后发现这是不可能的。解决方案是构建您自己的自定义控件。

如果有帮助,我确实找到了这个Show a custom calendar dropdown with a derived DateTimePicker class,它链接到自定义 Winforms 日历Culture Aware Month Calendar 和 DatePicker

于 2012-06-06T19:09:30.373 回答