4
votes

INTRODUCTION: I have a class(custom window) derived from CWND. This custom class has a radio button(CButton) and a bunch of other static controls.

The PROBLEM: When the radio button is created it's grayed out and clicking it does nothing. Code used for creation is pretty simple:

m_radioButton->Create(_T("rButton1.1"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON , CRect(5,5,300,15), this,2001);

I tried to add the

ON_BN_CLICKED(2001, method())

event but that isn't getting triggered.

I also have the ShowWindow() and EnableWindow() methods on it but that didn't work either.

The QUESTION: Since this class isn't derived from a CDialog means DDX isn't available. Can that be the problem? Is there a way to get around it? My message map only has a SIZE, CREATE and DESTROY other than the ON_BN_CLICKED.

Any suggestion are welcome.

SIDE NOTE: MFC newbie here, your help is much appreciated.

Research: I found only this relevant stack overflow question but that doesn't help my case. Also came across this cool page on mfc subclassing but it doesn't answer my question.

SIDE QUESTION: Since I'm not getting any answers, Is this situation not that common and Is this fundamentally wrong should I not derive from CWND at all and derive from CDialog or something else?

1
The first radio button of a group should have WS_GROUP set, and also whatever control follows the last radio button in the tab order.Mark Ransom
P.S. the problem may be that you're using your own button class in the Create call, rather than the generic BUTTON class.Mark Ransom
Yeah that's true nonetheless, not having a WS_GROUP shouldn't give rise to these problems right? Also, the create is being called for the CButton. The problem is that the CButton inside my custom class is grayed out and I can't handle the events on it. I've added that in the question description so it's more clear.AdRoiT
First thing: Try to do this without creating too many own things at once. A simple window class with CButtons in it should be your starting point. Then, turn it into a radio button and only if that works, use your own windowclass. At some point, you will find that it fails, and there you have your (almost, at least) minimial example without which your question is off-topic here.Ulrich Eckhardt
Hey @UlrichEckhardt, Thanks for your input. Question: so when you say "a simple window" class, which class does that derive from? CWnd or CDialog which comes back to the same question. I have a minimal class that contains the radio button. But, I'll try to create just a regular button and see how that goes.AdRoiT

1 Answers

0
votes

I tried to replicate the scenario, but could not reproduce. Deriving from CWnd should not be any problem. I have provided sample code below, I have created a custom class named "CTestWindow" derived from CWnd and create a ribbon button inside OnCreate() of CTestWindow. I am able to click on radio button and method() is also being called as expected.

IMPLEMENT_DYNAMIC(CTestWindow,CWnd)
BEGIN_MESSAGE_MAP(CTestWindow, CWnd)
    ON_WM_CREATE()
    ON_BN_CLICKED(2001, method)
END_MESSAGE_MAP()

int CTestWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    CButton *m_radioButton = new CButton;
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    if(!m_radioButton->Create(_T("rButton1.1"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON , CRect(5,5,300,25), this,2001)){
        return -1;
    }
    return 0;
}

void CTestWindow::method(){
    AfxMessageBox(_T("I m clicked"));
}

Find below code for creating a instance of custom window:

CString strMyClass;
    try
    {
        strMyClass = AfxRegisterWndClass(
            CS_VREDRAW | CS_HREDRAW,
            ::LoadCursor(NULL, IDC_ARROW),
            (HBRUSH) ::GetStockObject(WHITE_BRUSH),
            ::LoadIcon(NULL, IDI_APPLICATION));
    }
    catch (CResourceException* pEx)
    {
        AfxMessageBox(_T("Couldn't register class! (Already registered?)"));
        pEx->Delete();
    }
    if(m_wndTest.Create(strMyClass,_T("Custom Window"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,CRect(0,0,400,400),this,200,NULL) == -1){
        return;
    }
    m_wndTest.SetWindowPos(NULL,120,120,500,500,SWP_SHOWWINDOW);