Search code examples
visual-c++mfcdatetimepicker

How can I get OnCommand to detect DTN_DATETIMECHANGE?


My CDialog has two date time controls (one for a time and one for a date):

LTEXT           "Meeting Time:",IDC_STATIC,149,20,46,8
CONTROL         "",IDC_DTP_MEETING_TIME,"SysDateTimePick32",DTS_RIGHTALIGN | DTS_UPDOWN | WS_TABSTOP | 0x8,148,31,76,15
LTEXT           "Last Invited:",IDC_STATIC,149,49,42,8
CONTROL         "",IDC_DTP_LAST_INVITED,"SysDateTimePick32",DTS_RIGHTALIGN | WS_TABSTOP,147,60,100,15

I am trying to use OnCommand to detect when alot of the controls have been updated:

BOOL CCongregationDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
    const auto nMessage = HIWORD(wParam);
    const auto nControlID = LOWORD(wParam);

    if(IsWindowVisible())
    {
        if (nMessage == CBN_SELCHANGE ||
            nMessage == DTN_DATETIMECHANGE ||
            nMessage == EN_CHANGE)
        {
            IsModified(true);
            SetControlStates();

        }
        else if (nMessage == BN_CLICKED && nControlID == IDC_CHECK_LOCAL_CONGREGATION)
        {
            IsModified(true);
            SetControlStates();

        }
    }

    return __super::OnCommand(wParam, lParam);
}

It works fine for the combos and the edit boxes but the DTN_DATETIMECHANGE doesn't seem to be detected?


I found this conversation about Notifications where it states:

I found out, that DTN_DATETIMECHANGE works with OnNotify

I appreciate that I can add a handler for DTN_DATETIMECHANGE to each control and track IsModified that way. But is it not possible to do something similar to what I am doing with OnCommand?


Solution

  • Based on the comment from @Ilnspectable and furher research I came up with:

    BOOL CCongregationDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
        const NMHDR* pNMHDR = reinterpret_cast<NMHDR*>(lParam);
    
        if (pNMHDR != nullptr)
        {
    #pragma warning(suppress: 26454) 
            if (wParam == IDC_DTP_LAST_INVITED ||
                wParam == IDC_DTP_MEETING_TIME)
            {
                if (pNMHDR->code == DTN_DATETIMECHANGE)
                {
                    IsModified(true);
                    SetControlStates();
    
                }
    
            }
        }
    
        return __super::OnNotify(wParam, lParam, pResult);
    }
    

    Resources: