gpt4 book ai didi

c# - 仅更改月份和年份的 DateTimePicker

转载 作者:行者123 更新时间:2023-12-04 10:46:45 30 4
gpt4 key购买 nike

UI 应该保持不变我不想这样做:

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "mm-yyyy";
dateTimePicker1.ShowUpDown = true;

用户可以选择年份和月份,将 DateTimePicker 更改为 Month/Year 选择器,MonthCalendar 也是如此。
我不想使用第三方控件,但如果它是唯一的答案,我会使用任何免费的月/年选择器控件。

最佳答案

这是一个简单的 DateTimePicker 自定义控件,它预先定义了自定义 DateTime 格式并修改了 MonthCalendar查看,配置它仅显示打开时的月份和年份。
我添加了一个公共(public)属性 ShowToday ,允许指定 MonthCalendar 弹出窗口是否应显示 Today日历界面底部的日期。
自定义控件覆盖 OnDropDown要获取 MonthCalendar 弹出窗口的句柄,请发送 DTM_GETMONTHCAL 消息并更改其当前 View 发送 MCM_SETCURRENTVIEW , 指定 MCMV_YEAR 作为值参数。
这是它的样子:
DateTimePicker Custom Month Year only

using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[DesignerCategory("Code")]
class DateTimePickerYearMonth : DateTimePicker
{
public DateTimePickerYearMonth() {
this.CustomFormat = "MM-yyyy";
this.Format = DateTimePickerFormat.Custom;
this.Value = DateTime.Now;
}

[
Browsable(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(false),
Category("Appearance"),
Description("Shows or hides \"Today\" date at the bottom of the Calendar Control")
]
public bool ShowToday {
get => m_ShowToday;
set {
if (value != m_ShowToday) {
m_ShowToday = value;
ShowMonCalToday(m_ShowToday);
}
}
}

protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
ShowMonCalToday(m_ShowToday);
}

protected override void OnDropDown(EventArgs e)
{
var hWnd = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
if (hWnd != IntPtr.Zero) {
SendMessage(hWnd, MCM_SETCURRENTVIEW, 0, (int)MonCalView.MCMV_YEAR);
}
base.OnDropDown(e);
}

private void ShowMonCalToday(bool show)
{
int styles = SendMessage(this.Handle, DTM_GETMCSTYLE, 0, 0).ToInt32();
styles = show ? styles &~(int)MonCalStyles.MCS_NOTODAY : styles | (int)MonCalStyles.MCS_NOTODAY;
SendMessage(this.Handle, DTM_SETMCSTYLE, 0, styles);
}

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private const int DTM_FIRST = 0x1000;
private const int DTM_GETMONTHCAL = DTM_FIRST + 8;
private const int DTM_SETMCSTYLE = DTM_FIRST + 11;
private const int DTM_GETMCSTYLE = DTM_FIRST + 12;
private const int MCM_FIRST = 0x1000;
private const int MCM_GETCURRENTVIEW = MCM_FIRST + 22;
private const int MCM_SETCURRENTVIEW = MCM_FIRST + 32;
private bool m_ShowToday = false;

public enum MonCalView : int
{
MCMV_MONTH = 0,
MCMV_YEAR = 1,
MCMV_DECADE = 2,
MCMV_CENTURY = 3
}

public enum MonCalStyles : int
{
MCS_DAYSTATE = 0x0001,
MCS_MULTISELECT = 0x0002,
MCS_WEEKNUMBERS = 0x0004,
MCS_NOTODAYCIRCLE = 0x0008,
MCS_NOTODAY = 0x0010,
MCS_NOTRAILINGDATES = 0x0040,
MCS_SHORTDAYSOFWEEK = 0x0080,
MCS_NOSELCHANGEONNAV = 0x0100
}
}

关于c# - 仅更改月份和年份的 DateTimePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59671707/

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