How to activate a button (CButton) located in a disabled window (CWnd)?

216 Views Asked by At

I have this code :

m_pBtnCom = new CButton();
m_pBtnCom->Create(_T("Push"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT|BS_VCENTER|BS_CENTER, rc, this, BTN_CMT);  

Where:

  • this = my derived CWnd class
  • rc = CRect button position
  • BTN_CMT = button id

Current context:
If I disable the parent CWnd by calling EnableWindow(FALSE), even if I call the function EnableWindow(TRUE) on the button (m_pBtnCom->EnableWindow(TRUE)), the latter remains disabled; Therefore, nothing works on it: click, tooltip, ...
I tried to remove WS_CHILD, without success

Question:
Is it possible to activate the button when the window (argument this in my code) is disabled?

1

There are 1 best solutions below

0
On

Child window can't be independently enabled when parent window is disabled. You can instead enable all children, then go back and enable the particular button.

Note, if you have IDCANCEL button, and you disable it, then the dialog's close button is not functioning either and it gets confusing. You may want to avoid disabling the cancel button and override OnCancel

void CMyDialog::enable_children(bool enable)
{
    auto wnd = GetWindow(GW_CHILD);
    while (wnd)
    {
        wnd->EnableWindow(enable);
        wnd = wnd->GetWindow(GW_HWNDNEXT);
    }
}

BOOL CMyDialog::OnInitDialog()
{
    CDialog::OnInitDialog();
    enable_children(FALSE);
    
    //re-enable one button
    if(GetDlgItem(IDCANCEL)) GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
    return TRUE;
}

void OnCancel()
{
    MessageBox(L"cancel...");
    CDialog::OnCancel();
}