对于按钮控件已经说过了push button,check boxes,group boxes,这次来说说 Radio Buttons
老规矩,先看MSDN:
A radio button (also called option button) consists of a round button and an application-defined label, icon, or bitmap that indicates a choice the user can make by selecting the button. An application typically uses radio buttons in a group box to enable the user to choose one of a set of related but mutually exclusive options.
A radio button can be one of two styles: standard or automatic, as defined by the style constants BS_RADIOBUTTON and BS_AUTORADIOBUTTON. Each style can assume two check states: checked (a dot in the button) or cleared (no dot in the button).
When the user selects either state, the radio button receives the keyboard focus. The system sends the button's parent window a WM_COMMAND message containing the BN_CLICKED notification code. The parent window need not handle this message if it comes from an automatic radio button, because the system automatically sets the check state for that style. But the parent window should handle the message if it comes from a non-automatic radio button, because the parent window is responsible for setting the check state for that style. Regardless of the radio button style, the system automatically repaints the button as its state changes.
Radio buttons are arranged in groups, and only one button in the group can be checked at any time. If the WS_GROUP flag is set for any radio button, that button is the first button in a group, and all buttons that follow it immediately in the tab order (but do not themselves have the WS_GROUP flag) are part of its group. If no radio buttons have the WS_GROUP flag, all the radio buttons in the dialog box are treated as a single group.
其实还是子窗口,static那节说的很明白了,就不重复了 ,这个控件的风格是 BS_RADIOBUTTON 和 BS_AUTORADIOBUTTON
还是以我的博客“[Win32SDK基本] 窗口详解(超详细)”(地址:http://blog.csdn.net/zuishikonghuan/article/details/46378475)为模板,进一步编写。
先创建全局变量
HWND button7 = 0; HWND button8 = 0;
在上次创建的group boxes里创建 Radio Buttons
有必要说一句的是,对于上次的check boxes,我是先用的非自动的,再用的自动的 BS_AUTOCHECKBOX ,但是由于 Radio Buttons 非自动太麻烦,所以我上来就用的自动的 BS_AUTORADIOBUTTON 了
代码:
button7 = CreateWindow(TEXT("Button"), TEXT("Radio Buttons 1"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 10, 25, 200, 25, button6, (HMENU)1, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); SendMessage(button7, WM_SETFONT, (WPARAM)GetStockObject(17), 0); button8 = CreateWindow(TEXT("Button"), TEXT("Radio Buttons 2"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 10,50, 200, 25, button6, (HMENU)2, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); SendMessage(button8, WM_SETFONT, (WPARAM)GetStockObject(17), 0); SendMessage(button7, BM_SETCHECK, BST_CHECKED, 0);
效果图:
另外,控制消息和check boxes的一样一样的,还是原来的配方,还是原来的味道:
1。将 Radio Buttons 打勾
SendMessage(hwnd, BM_SETCHECK, BST_CHECKED, 0) ;
2。取消打勾
SendMessage(hwnd, BM_SETCHECK, BST_UNCHECKED, 0) ;
3。判断是否打勾
通过 BM_GETCHECK 获取状态,打勾时返回TRUE,没有被打勾时返回FLASE。