gpt4 book ai didi

c# - 如何设置日期时间选择器下拉列表以仅显示月份

转载 作者:太空狗 更新时间:2023-10-29 17:56:59 27 4
gpt4 key购买 nike

因此,点击下拉菜单时不会显示这个。

enter image description here

我希望下拉菜单在点击时是这样的。

enter image description here

非常感谢您的帮助。 :)

最佳答案

使用 windows messages approach ,您可以检测月历控件显示和强制月 View ,您可以检测 View 更改并在月到日 View 更改时关闭月历控件(选择月份后)。

实现它的最简单方法是覆盖 DateTimePicker。

public class MonthPicker : DateTimePicker
{
// initialize Format/CustomFormat to display only month and year.
public MonthPicker()
{
Format = DateTimePickerFormat.Custom;
CustomFormat = "MMMM yyyy";
}

// override Format to redefine default value (used by designer)
[DefaultValue(DateTimePickerFormat.Custom)]
public new DateTimePickerFormat Format
{
get => base.Format;
set => base.Format = value;
}

// override CustomFormat to redefine default value (used by designer)
[DefaultValue("MMM yyyy")]
public new string CustomFormat
{
get => base.CustomFormat;
set => base.CustomFormat = value;
}

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NOFITY)
{
var nmhdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
switch (nmhdr.code)
{
// detect pop-up display and switch view to month selection
case -950:
{
var cal = SendMessage(Handle, DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
SendMessage(cal, MCM_SETCURRENTVIEW, IntPtr.Zero, (IntPtr)1);
break;
}

// detect month selection and close the pop-up
case MCN_VIEWCHANGE:
{
var nmviewchange = (NMVIEWCHANGE)Marshal.PtrToStructure(m.LParam, typeof(NMVIEWCHANGE));
if (nmviewchange.dwOldView == 1 && nmviewchange.dwNewView == 0)
{
SendMessage(Handle, DTM_CLOSEMONTHCAL, IntPtr.Zero, IntPtr.Zero);
}

break;
}
}
}
base.WndProc(ref m);
}

private const int WM_NOFITY = 0x004e;
private const int DTM_CLOSEMONTHCAL = 0x1000 + 13;
private const int DTM_GETMONTHCAL = 0x1000 + 8;
private const int MCM_SETCURRENTVIEW = 0x1000 + 32;
private const int MCN_VIEWCHANGE = -750;

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

[StructLayout(LayoutKind.Sequential)]
private struct NMHDR
{
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}

[StructLayout(LayoutKind.Sequential)]
struct NMVIEWCHANGE
{
public NMHDR nmhdr;
public uint dwOldView;
public uint dwNewView;
}
}

关于c# - 如何设置日期时间选择器下拉列表以仅显示月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34846455/

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