MFC - Button


Advertisements

A button is an object that the user clicks to initiate an action. Button control is represented by CButton class.

Here is the list of messages mapping for Button control −

Message Map entry Description
BN_CLICKED ON_BN_CLICKED( <id>, <memberFxn> ) The framework calls this member function when the button is clicked.
BN_DISABLE ON_BN_DISABLE( <id>, <memberFxn> ) The framework calls this member function when the button is disabled.
BN_DOUBLECLICKED ON_BN_DOUBLECLICKED( <id>, <memberFxn> ) The framework calls this member function when the button is double clicked.
BN_PAINT ON_BN_PAINT( <id>, <memberFxn> ) The framework calls this member function when an application makes a request to repaint a button.

Let us look into a simple example by dragging two buttons from the Toolbox.

Step 1 − Change the Caption from Start, Stop and ID to IDC_BUTTON_START, IDC_BUTTON_STOP for both buttons.

Button1

Step 2 − Let us add event handler for both buttons.

Step 3 − Here is an implementation of both events in which we will start and stop animation.

void CMFCAnimationDemoDlg::OnBnClickedButtonStart() {
   
   // TODO: Add your control notification handler code here
   m_animationCtrl.Open(L"res\\copyfile.avi");
}

void CMFCAnimationDemoDlg::OnBnClickedButtonStop() {
   
   // TODO: Add your control notification handler code here
   m_animationCtrl.Stop();
}

Step 4 − When the above code is compiled and executed, you will see the following output.

Button2

Step 5 − When you click the Stop button, the animation stops and when you press the Start button, it starts again.

mfc_windows_controls.htm
Advertisements