Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

c# - How can I remove the "Today" button from DateTimePicker control (of Windows form Control)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...