British Gent
British Gent

Reputation: 1

Create Button and Reference in message map

Im creating a button on my oncreate using message map.

I am unable to get a callback message from ON_BN_CLICKED when passing a reference to ICL_OK.

I don't believe its a parenting issue. The window is an CFrameWnd and parent is a CMainFrame.

Even getting all the messages and I can swtich between what I want to do as I have list boxes and input boxes to add also and edit / get response.

Thanks

Cant go into the gui main thread loop. Message map is the way I need to achieve this.

            okBtn.Create(_T("Ok Button"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        CRect(10, 10, BUTTON_WIDTH, HEIGHT), this, ICL_OK);

To click the button and get a response. Instead in using OnCmdMsg and getting a reference to its nID which I don't like. I want BN_CLICKED to work.

Refering to this answer Message map macros

I can again confirm oncmdmsg works but wm_command event does not fire. Message map macros

UPDATE: Still not working, alternative is to use ON_COMMAND_RANGE and still fires the WM_COMMAND so just have to restrict the amount of messages it handles. Hope it helps someone. If you want to generate a button the solution below might help you.

Upvotes: 0

Views: 276

Answers (1)

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

You are writing that the button is not showing in the window. There is a reason for that, and I would guess this: You define the button in a subroutine/method/function instead of defining it in its parent class.

Instead, in its parent class, whether that is the CMainFrame or some other Window, define a button like:

class CMainFrame : public CFrameWnd
{
/// bunch of stuff, including OnCreate() or OnCreateClient()
   CButton m_button;
};

In the class that houses the button, assuming CMainFrame for now, create the button... ideally in OnCreate() or OnCreateClient()

call the baseclass version then your button create....

int CMainFrame::OnCreate(LPCREATESTRUCT lpcs)
{
   int ret = __super::OnCreate(lpcs);
   if (ret != -1) {
       m_button.Create(_T("Ok Button"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, CRect(10, 10, BUTTON_WIDTH, HEIGHT), this, ICL_OK);
   }
   return ret;
}

If your constructor is in a method, then its destructor will be called at the end of the method. MFC CWnd derived windows classes usually call DestroyWindow() in their destructor and what this means is that the window is destroyed by the end of the call and that is the reason it is not visible.

Upvotes: 1

Related Questions