Can we map a checkbox to a bool variable in MFC instead of BOOL?

397 views Asked by At

Take this variable mapping in a CDialog:

DDX_Check(pDX, IDC_CHECK_PREVIEW_CURRENT_WEEK, m_bPreviewCurrentWeek);

By design in the IDE it to map to a BOOL variable instead of a bool variable.

I know that under normal circumstances you can just use a bool variable wherever TRUE or FALSE is used. But, when I try changing my member variable to a bool it fails to compile on the DDX_Check routine.

Can we use bool with a checkbox control?

1

There are 1 answers

0
Andrew Truckle On BEST ANSWER

@Jabberwocky's comment reminded me that I have already implemented some custom DDX routines:

So I delved into the MFC source and modified a copy in my own cpp file:

void AFXAPI DDX_CheckBool(CDataExchange* pDX, int nIDC, bool& value)
{
    pDX->PrepareCtrl(nIDC);
    HWND hWndCtrl;
    pDX->m_pDlgWnd->GetDlgItem(nIDC, &hWndCtrl);
    if (pDX->m_bSaveAndValidate)
    {
        value = static_cast<bool>(::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L));
        //ASSERT(value >= 0 && value <= 2);
    }
    else
    {
        //if (value < 0 || value > 2)
        //{
        //  TRACE(traceAppMsg, 0, "Warning: dialog data checkbox value (%d) out of range.\n",
        //      value);
        //  value = 0;  // default to off
        //}
        ::SendMessage(hWndCtrl, BM_SETCHECK, static_cast<WPARAM>(value), 0L);
    }
}

I commented out the validation checks since bool has only one of two values anyway. Shout if you think that was a mistake.

So I am now able to do this:

DDX_CheckBool(pDX, IDC_CHECK_SELECT_NAMES, m_bSelectStartingNames);

It works fine.