您的位置:首页 > 其它

[Win32SDK基本]Button Control(2)Check Boxes

2015-06-30 13:40 246 查看
本文由CSDN用户zuishikonghuan所作,转载请注明出处:/article/9672504.html
上一篇中介绍了push button,这次来说说 Check Boxes。

MSDN:

A check box consists of a square box and an application-defined label, icon, or bitmap that indicates a choice the user can make by selecting the button. Applications typically display check boxes to enable the user to choose one or more options that are not
mutually exclusive.

A check box can be one of four styles: standard, automatic, three-state, and automatic three-state, as defined by the constants BS_CHECKBOX, BS_AUTOCHECKBOX, BS_3STATE, and BS_AUTO3STATE, respectively. Each style can assume two check states: checked (a check
mark inside the box) or cleared (no check mark). In addition, a three-state check box can assume an indeterminate state (a shaded box inside the check box), which might signify that the user has not made a choice. Repeatedly clicking a standard or automatic
check box toggles it from checked to cleared and back again. Repeatedly clicking a three-state check box toggles it from checked to cleared to indeterminate and then repeats the cycle.

When the user clicks a check box (of any style), the check box receives the keyboard focus. The system sends the check box's parent window a WM_COMMAND message containing the BN_CLICKED notification code. The parent window does not have to handle this message
if it comes from an automatic check box or automatic three-state check box, because the system automatically sets the check state for those styles. But the parent window must handle the message if it comes from a non-automatic check box or three-state check
box, because the parent window is responsible for setting the check state for those styles. Regardless of the check box style, the system automatically repaints the check box once its state is changed.

The application can ascertain the state of a check box by using the IsDlgButtonChecked function.

其实和static和push button一样一样的,都是子窗口,static那节已经说的很明白了,这个控件的风格是 BS_CHECKBOX

还是以我的博客“[Win32SDK基本] 窗口详解(超详细)”(地址:/article/9672496.html)为模板,进一步编写。

先创建全局变量

HWND button5 = 0;


创建Check Boxes:

button5 = CreateWindow(TEXT("Button"), TEXT("Check Boxes"), WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 10, 260, 100, 20, hwnd, (HMENU)8, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);


效果图:



但是你可能会发现鼠标点击时无效果!是的,我们需要自己处理WM_COMMAND单机事件

先来看看控制消息

BM_SETCHECK 的 MSDN:https://msdn.microsoft.com/en-us/library/bb775989

1。将 Check Boxes 打勾

SendMessage(hwnd, BM_SETCHECK, BST_CHECKED, 0) ;
2。取消打勾
SendMessage(hwnd, BM_SETCHECK, BST_UNCHECKED, 0) ;


3。判断是否打勾

通过 BM_GETCHECK 获取状态,打勾时返回TRUE,没有被打勾时返回FLASE。

4。对于 BS_3STATE 而言,当 wParam 为 BST_INDETERMINATE 时这个类型的复选框就会被设为这种状态。

那么好了,我们只需要在回调函数中这样做:

case WM_COMMAND:
int id;
int ent;
ent = HIWORD(wParam);//通知吗
id = LOWORD(wParam);//子窗口ID
。。。。。。//省略菜单的单机事件
if (ent == BN_CLICKED){
switch (id)
{
case 4://按钮的子窗口ID
。。。。。。。。。//省略其他按钮的事件
break;

case 8://Check Boxes被单机
if (SendMessage((HWND)lParam, BM_GETCHECK, 0, 0) == TRUE)//是否打勾了
SendMessage((HWND)lParam, BM_SETCHECK, BST_UNCHECKED, 0);//取消打勾
else SendMessage((HWND)lParam, BM_SETCHECK, BST_CHECKED, 0);//打勾
break;
}
}
break;


这样就可以单机时打勾/取消打勾了






方法二:使用自动Check Boxes(推荐)
虽然接收这个单机消息后手动打勾可以解决问题,但是不方便,不如直接用 BS_AUTOCHECKBOX 这样的Check Boxes可以自动打勾取消打勾
只需要将上面的创建代码改成这样:
button5 = CreateWindow(TEXT("Button"), TEXT("Check Boxes"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 10, 260, 100, 20, hwnd, (HMENU)8, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
同时去掉WM_COMMAND里的被单机事件就行了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: