Now we will add a check-box to our calculator application.
A checkbox is a button with either the BS_CHECKBOX or BS_AUTOCHECKBOX style set:
CreateWindow("BUTTON", "Hex", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, 120, 155, 50, 14, hwndDlg, (HMENU )HexCheckId, InstanceHandle, 0);We use BS_AUTOCHECKBOX to let windows handle checking and unchecking the button.
When the button is checked and the user hits the Calculate button we will show the result in hex, instead of the normal decimal number.
First a template function to convert a number to a string in hexadecimal format:
template <typename T> std::string ToStringHex(T aValue) { std::stringstream ss; ss << std::hex << aValue; return ss.str(); }We use BM_GETCHECK to know if the button is checked or unchecked:
SendDlgItemMessage(hwndDlg, HexCheckId, BM_GETCHECK, 0, 0);Which returns BST_CHECKED or BST_UNCHECKED or BST_INDETERMINATE.
The complete code for the application can be found here.