简体   繁体   English

显示带有派生的DateTimePicker类的自定义日历下拉列表

[英]Show a custom calendar dropdown with a derived DateTimePicker class

My goal is to create a custom DateTimePicker class in .NET 2.0, which shows a custom calendar dropdown instead of the Windows default calendar popup. 我的目标是在.NET 2.0中创建自定义DateTimePicker类,该类显示自定义日历下拉列表,而不是Windows默认日历弹出窗口。

By observing Windows messages (see attached code), I am able to find and hide/close the calendar window after creation. 通过观察Windows消息(请参阅附加代码),我可以在创建后查找和隐藏/关闭日历窗口。

However, a problem remains: After the calendar window is closed, something is still blocking the mouse input. 但是,问题仍然存在:关闭日历窗口后,仍然有一些东西阻止鼠标输入。 For example, if you try to maximise the owner form of the customised DateTimePicker control after the calendar dropdown has been closed programmatically (attached code), the maximise button does not respond. 例如,如果您尝试以编程方式关闭日历下拉列表(附加代码)之后最大化自定义DateTimePicker控件的所有者形式,则最大化按钮将不响应。 Only the next click works. 仅下次单击有效。 Interestingly, the "non-functional click" fires the DTN_CLOSEUP notification, so it appears that the WM_CLOSE did not properly close the calendar. 有趣的是,“非功能性单击”会触发DTN_CLOSEUP通知,因此WM_CLOSE似乎没有正确关闭日历。

Any hints on how to accomplish my task are highly appreciated :) 非常感谢有关如何完成任务的任何提示:)

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == (int)SYSMSG.WM_REFLECT + (int)SYSMSG.WM_NOTIFY)
    {
        NMHDR nmhdr = (NMHDR)m.GetLParam(typeof(NMHDR));
        switch (nmhdr.code)
        {
            case DTN_DROPDOWN:
                // Hide window
                IntPtr calHandle = FindWindow("SysMonthCal32", null);
                SendMessage(calHandle, (int)SYSMSG.WM_SIZE, 0, SP.Convert.MakeLong(0, 0));

                this.BeginInvoke((MethodInvoker)delegate()
                {
                    SendMessage(calHandle, (int)SYSMSG.WM_CLOSE, 0, 0);
                });
                break;
        }

    }

    base.WndProc(ref m);
}

Instead of sending a WM_CLOSE have you tried sending a DTM_CLOSEMONTHCAL message instead? 您是否尝试发送DTM_CLOSEMONTHCAL消息而不是发送WM_CLOSE You would send this to the HWND of the DateTimePicker itself and not the child window. 您可以将其发送到DateTimePicker本身的HWND,而不是子窗口。 According to the documentation, the DateTime_CloseMonthCal macro sends this message and it seems like what you want to do. 根据文档, DateTime_CloseMonthCal宏发送此消息,并且看起来像您想要执行的操作。

I also don't think you'll need to use BeginInvoke to send it unless there's some problem with closing it in the same dispatch as a drop down notification. 我也认为您不需要使用BeginInvoke来发送它,除非在与下拉通知相同的调度中关闭它有问题。

#define DTM_FIRST        0x1000
#define DTM_CLOSEMONTHCAL (DTM_FIRST + 13)
#define DateTime_CloseMonthCal(hdp) SNDMSG(hdp, DTM_CLOSEMONTHCAL, 0, 0)

我终于找到了这个完全可自定义的datePicker(monthCalendar渲染是可以重写的): CodeProject上的Culture Aware月日历和Datepicker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM