Show a custom calendar dropdown with a derived DateTimePicker class

感情迁移 提交于 2019-12-02 13:40:14

问题


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.

By observing Windows messages (see attached code), I am able to find and hide/close the calendar window after creation.

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. 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.

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);
}

回答1:


Instead of sending a WM_CLOSE have you tried sending a DTM_CLOSEMONTHCAL message instead? You would send this to the HWND of the DateTimePicker itself and not the child window. According to the documentation, the DateTime_CloseMonthCal macro sends this message and it seems like what you want to do.

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.

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



回答2:


I finally found this fully customisable datePicker (monthCalendar rendering is override-able) : Culture Aware Month Calendar and Datepicker on CodeProject



来源:https://stackoverflow.com/questions/2137997/show-a-custom-calendar-dropdown-with-a-derived-datetimepicker-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!