Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been able to create a CButton in a CView derived class, and it is visible.
The problem is, no matter how and where I insert the ON_BN_CLICKED event management map entry, it seems the event is not fired or at least not propagated to the correct message map.
This is my CView derived class Initial update handler:

void CMFC_csv2hstView::OnInitialUpdate()  // 
{
	// TODO: add draw code for native data here
	RECT R;
	R.bottom = 250;
	R.top = 100;
	R.left = 100;
	R.right = 250; 
	
	/*CButton CB;*/
	CB.Create(_T("Hello"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, R, this, 1);
	

}


and the related message map :


IMPLEMENT_DYNCREATE(CMFC_csv2hstView, CView)

BEGIN_MESSAGE_MAP(CMFC_csv2hstView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CMFC_csv2hstView::OnFilePrintPreview)
	ON_COMMAND(WM_CREATE, &CMFC_csv2hstView::OnCreateWindow)
	//ON_COMMAND(BN_CLICKED, &CMFC_csv2hstView::OnButton) //-->
	ON_BN_CLICKED(1, &CMFC_csv2hstView::OnButton)//-
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()


As you can see the "OnButton" method is my OnClick handler , where I put a breakpoint, but its never reached.It seems that, however you click the button, the event is not managed.

I have to say I made lots of tries before reaching this point, I tried to put the event handler in every possible message map around the application (even if sometimes it was clearly nonsense).
Some told me , that maybe the ID I was assigning to the CButton (1) was already in use, I tried to look in the application for other objects with the same ID but couldn't find it. Is there a way to assign an ID and be sure it's not already used ?

I looked around a lot of tutorials and online resources, it seems noone takes in consideration the eventuality of managing a Button without the use of Dialog Boxes.
Is this a rule ? is there a limit in MFC framework which makes it not possible to do what I want to do ?

The application is a MDI and I am using VS 2013 Community

What I have tried:

Putting :

ON_BN_CLICKED(1, &CMFC_csv2hstView::OnButton)


in CWinApp, MainFrm
Posted
Updated 12-Dec-17 0:26am
Comments
Jochen Arndt 12-Dec-17 3:41am    
Where is CB defined?

It must be a member of your view class. If so, ON_BN_CLICKED() should work provided that the button has not been disabled meanwhile.

Check also if the button was successfully created (Create returns TRUE).
tiwal 12-Dec-17 4:56am    
The CButton is a private member of my CView-derived class... I can see the button on the view and click it , but the event handler is not called.
Jochen Arndt 12-Dec-17 5:15am    
What is the base class of your CMFC_csv2hstView (e.g. CEditView, CListView)?
tiwal 12-Dec-17 5:34am    
It's Cview .
Jochen Arndt 12-Dec-17 5:55am    
So a plain CView. As already said, it should work.

What you should check too:
- CMFC_csv2hstView::OnButton should be declared as afx_msg
- Ensure that the button ID is unique (not used for other controls in the same view)

To check if the message is handled, you can send it manually for testing:
SendMessage(GetSafeHwnd(), WM_COMMAND, (BN_CLICKED << 16) | 1, CB.GetSafeHwnd());
The above must be called from the view class. But it can be also called from elsewhere when passing a pointer to the view and using that for the first and last parameter (CB must be public then).

Alternatively think about using a CFormView based class where controls are defined in a template and does not need to be created in your code therefore.

1 solution

I have just tested it with a new MFC application and it works as expected:
C++
// ViewTestView.h
class CViewTestView : public CView
{
    // ...
protected:
    CButton m_button;
    DECLARE_MESSAGE_MAP()
    afx_msg void OnButton();
};
C++
// ViewTestView.cpp
BEGIN_MESSAGE_MAP(CViewTestView, CView)
	// ...
	ON_BN_CLICKED(1, &CViewTestView::OnButton)
END_MESSAGE_MAP()

// ...

void CViewTestView::OnInitialUpdate()
{
    CView::OnInitialUpdate();
    
    m_button.Create(_T("Hello"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 
        CRect(100, 100, 250,250), this, 1);
}

void CViewTestView::OnButton()
{
    AfxMessageBox(_T("Button activated"));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900