MFC - Command Button
A command button is an enhanced version of the regular button. It displays a green arrow icon on the left, followed by a caption in regular size. Under the main caption, it can display another smaller caption that serves as a hint to provide more information.
Here is the list of messages mapping for Command 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 of command button by creating a new MFC dialog based project MFCCommandButton
Step 1 − From the Toolbox, add Command Button Control.
Step 2 − Change the Caption to My Command button.
Step 3 − Add the event handler for this button and add the following message in the event handler.
void CMFCCommandButtonDlg::OnBnClickedCommand1() { // TODO: Add your control notification handler code here MessageBox(L"My Command Button Clicked"); }
Step 4 − When the above code is compiled and executed, you will see the following output.
Step 5 − When the My Command Button is clicked; the following message will be displayed.