如何使复选框在单击时更改其状态?
How to make a checkbox change its state when clicked?
我使用以下代码在 WinAPI 中创建了一个复选框:
HWND checkbox = CreateWindowEx(NULL, "BUTTON", "Click Me!", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 10, 10, 60, 20, hWnd, NULL, hInstance, NULL);
我希望复选框在单击时自动更改其状态,但它没有!
我在 MSDN 上没有找到任何示例,那么如何让复选框在单击时更改其状态?我应该处理 WM_COMMAND
消息并查看它处于什么状态,然后将其状态更改为相反的状态吗?
使用 BS_AUTOCHECKBOX
样式而不是 BS_CHECKBOX
。
BS_AUTOCHECKBOX
Creates a button that is the same as a check box, except that the check state automatically toggles between checked and cleared each time the user selects the check box.
Elements of a Button State
A button's state can be characterized by its focus state, push state, and check state.
...
Check State
...
The system automatically changes the check state of an automatic button, but the application must change the check state of a non-automatic button.
Changes to a Button State
When the user selects a button, it is generally necessary to change one or more of the button's state elements. The system automatically changes ... the check state for all automatic buttons. The application must make all other state changes, taking into account the button's type, style, and current state.
我使用以下代码在 WinAPI 中创建了一个复选框:
HWND checkbox = CreateWindowEx(NULL, "BUTTON", "Click Me!", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 10, 10, 60, 20, hWnd, NULL, hInstance, NULL);
我希望复选框在单击时自动更改其状态,但它没有!
我在 MSDN 上没有找到任何示例,那么如何让复选框在单击时更改其状态?我应该处理 WM_COMMAND
消息并查看它处于什么状态,然后将其状态更改为相反的状态吗?
使用 BS_AUTOCHECKBOX
样式而不是 BS_CHECKBOX
。
BS_AUTOCHECKBOX
Creates a button that is the same as a check box, except that the check state automatically toggles between checked and cleared each time the user selects the check box.
Elements of a Button State
A button's state can be characterized by its focus state, push state, and check state.
...
Check State
...
The system automatically changes the check state of an automatic button, but the application must change the check state of a non-automatic button.Changes to a Button State
When the user selects a button, it is generally necessary to change one or more of the button's state elements. The system automatically changes ... the check state for all automatic buttons. The application must make all other state changes, taking into account the button's type, style, and current state.