1

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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Landstalker
  • 1,368
  • 8
  • 9
  • This sounds like you are trying to solve a different problem altogether. Are you perhaps trying to create a window whose child windows can be interacted with, but leaves the parent window inactive? – IInspectable Aug 07 '21 at 06:07
  • @IInspectable I want to disable the window that contains my button and make ONE exception for ONE button (m_pBtnCom) to be able to : click, see tooltip, ... – Landstalker Aug 07 '21 at 10:19
  • 2
    Yes, child window can't be independently enabled when parent window is disabled. Don't remove `WS_CHILD` flag from child window. You might consider using `GetWindow(GW_CHILD)` and `while ... GetWindow(GW_HWNDNEXT)` to disable/enable each child. You also have to handle dialog's close button. – Barmak Shemirani Aug 07 '21 at 14:55
  • 1
    @BarmakShemirani Thanks. that's what I was trying to avoid, but since I have no choice, I have to do it. – Landstalker Aug 07 '21 at 19:23
  • @BarmakShemirani suggestion is simple enough. – Andrew Truckle Aug 21 '21 at 13:26
  • Why not add your final code as an answer? – Andrew Truckle Aug 21 '21 at 13:27
  • 1
    Hi @AndrewTruckle, good idea, added it – Barmak Shemirani Aug 21 '21 at 14:56

1 Answers1

1

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();
}   
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77