CButton myButton; // create a CButton object myButton.Create("Click me", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(10, 10, 100, 30), parentWnd, ID_MYBUTTON); // create the button control
void CMyWindow::OnMyButtonClicked() { // handle the button click event here } BEGIN_MESSAGE_MAP(CMyWindow, CFrameWnd) ON_CONTROL(BN_CLICKED, ID_MYBUTTON, OnMyButtonClicked) END_MESSAGE_MAP()
CButton myButton; // create a CButton object myButton.Create("Button", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, CRect(10, 10, 100, 30), parentWnd, ID_MYBUTTON); // create the check box control myButton.SetCheck(BST_CHECKED); // set the initial state to checkedThis code creates a check box control with the caption "Button", at position (10,10) and size 90x20, as a child of the window specified by the variable parentWnd, with a unique ID of ID_MYBUTTON. The SetCheck() method is used to set the initial state of the check box to checked. In conclusion, the CButton class is part of the MFC library for developing desktop applications on Windows, and it represents a push button control. We looked at some code examples that create, handle events, and change the state of button controls using the CButton class.