How can I remove the “Today” button from DateTimePicker control (of Windows form Control)

随声附和 提交于 2019-11-29 00:29:14

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

It looks like this at runtime, on Windows 7:

jb11

I seem to recall wanting to modify this control myself a while ago, and finding out that it is not possible. The solution was to build your own custom control.

If it helps, I did find this Show a custom calendar dropdown with a derived DateTimePicker class, which links to a custom Winforms calendar Culture Aware Month Calendar and DatePicker.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!