gpt4 book ai didi

c# - 如何从 DateTimePicker 控件(Windows 窗体控件)中删除 "Today"按钮

转载 作者:太空狗 更新时间:2023-10-30 00:15:50 27 4
gpt4 key购买 nike

在 DateTimePicker 控件中,当我们单击下拉按钮时,会显示一个日历。在日历的底部,有一个按钮:今天,点击它,将当前日期设置为所选日期。我想从控件中删除/隐藏该按钮。我该怎么做?

最佳答案

native 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 上运行时看起来像这样:

enter image description here

关于c# - 如何从 DateTimePicker 控件(Windows 窗体控件)中删除 "Today"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10918138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com