在实际开发中,我们往往需要知道哪个按钮被选中了,才能进行下一步的操作。例如对于一组单选按钮,只有一个选项能被选中,最好的办法是将这组按钮作为参数传入一个函数,通过函数返回值判断哪个按钮被选中了。
但遗憾的是,在Windows中不能一次性获得一组按钮的选中状态,只能一个一个地遍历。复选框也是如此。
可以通过SendMessage函数发送BM_GETCHECK消息来获取按钮的选中状态,返回 BST_CHECKED 表示按钮被选中,返回 BST_UNCHECKED 是未被选中。
SendMessage 原型为:
LRESULT WINAPI SendMessage(
HWND hWnd, //窗口(控件)句柄
UINT Msg, //消息类型
WPARAM wParam, //附加消息1
LPARAM lParam //附加消息2
);
注意:发送 BM_GETCHECK 消息时,wParam 和 lParam 两个参数必须都为 0。
下面的代码会判断一个按钮是否被选中。如果被选中,那么会获取按钮文本并保存到缓冲区:
if( SendMessage(hCheckBox, BM_GETCHECK, 0, 0) == BST_CHECKED ){
GetWindowText(hCheckBox, szBuffer, 10);
}
我们对上节的例子进行改进,添加一个提交按钮,当用户点击该按钮时,获取用户的选项:
//窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
static HFONT hFont; //逻辑字体
//单选按钮
static HWND labSex; //静态文本框--性别
static HWND radioMale; //单选按钮--男
static HWND radioFemale; //单选按钮--女
static HWND radioOther;
//单选按钮
static HWND labMarriage; //静态文本框--婚姻状况
static HWND radioMarried; //单选按钮--已婚
static HWND radioSingle; //单选按钮--未婚
static HWND radioSecrecy; //单选按钮--保密
//复选框
static HWND labPet; //静态文本框--你的宠物
static HWND checkboxDog; //复选框--狗
static HWND checkboxCat; //复选框--猫
static HWND checkboxFish; //复选框--鱼
static HWND checkboxOther; //复选框--其他
//分组框控件
static HWND hGroupSex; //分组框--性别
static HWND hRadioMale; //单选按钮--男
static HWND hRadioFemale; //单选按钮--女
//设置缓冲区
static TCHAR szBufSex[10]; //保存性别
static TCHAR szBufMarriage[10]; //保存婚姻状况
static TCHAR szBufPet[20]; //保存宠物
static TCHAR szBufSubmit[100]; //保存提交时显示的文本
static TCHAR szBufTmp[10]; //临时缓冲区
/**
* 为了节省空间,这里省略了各种控件句柄的定义
* 这里的代码与上节相同,请参考上节代码
**/
static HWND btnSubmit; //按钮控件
int wmId, wmEvent;
switch (message) {
case WM_CREATE:
btnSubmit = CreateWindow(TEXT("button"), TEXT("提 交"),
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
20, 250, 200, 36,
hWnd, (HMENU)13, hInst, NULL
);
hFont = CreateFont(
-14, -7, 0, 0, 400,
FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
FF_DONTCARE, TEXT("微软雅黑")
);
//选择性别
labSex = CreateWindow(
TEXT("static"), TEXT("你的性别:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT/*文字居右*/,
10, 10, 80, 26,
hWnd, (HMENU)1, hInst, NULL
);
radioMale = CreateWindow(
TEXT("button"), TEXT("男"),
WS_CHILD | WS_VISIBLE | BS_LEFT/*文字居左*/ | BS_AUTORADIOBUTTON /*单选按钮*/ | WS_GROUP,
95, 10, 50, 26,
hWnd, (HMENU)2, hInst, NULL
);
radioFemale = CreateWindow(
TEXT("button"), TEXT("女"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
150, 10, 50, 26,
hWnd, (HMENU)3, hInst, NULL
);
radioOther = CreateWindow(
TEXT("button"), TEXT("其他"),
WS_CHILD | WS_VISIBLE | BS_LEFT/*文字居左*/ | BS_AUTORADIOBUTTON /*单选按钮*/ ,
205, 10, 50, 26,
hWnd, (HMENU)4, hInst, NULL
);
//选择婚姻状况
labMarriage = CreateWindow(
TEXT("static"), TEXT("婚姻状况:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
10, 40, 80, 26,
hWnd, (HMENU)14, hInst, NULL
);
radioMarried = CreateWindow(
TEXT("button"), TEXT("已婚"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON | WS_GROUP,
95, 40, 65, 26,
hWnd, (HMENU)5, hInst, NULL
);
radioSingle = CreateWindow(
TEXT("button"), TEXT("未婚"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
165, 40, 65, 26,
hWnd, (HMENU)6, hInst, NULL
);
radioSecrecy = CreateWindow(
TEXT("button"), TEXT("保密"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
235, 40, 100, 26,
hWnd, (HMENU)7, hInst, NULL
);
//你的宠物
labPet = CreateWindow(
TEXT("static"), TEXT("你的宠物:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
10, 70, 80, 26,
hWnd, (HMENU)18, hInst, NULL
);
checkboxDog = CreateWindow(
TEXT("button"), TEXT("狗"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX/*复选框*/,
95, 70, 50, 26,
hWnd, (HMENU)9, hInst, NULL
);
checkboxCat = CreateWindow(
TEXT("button"), TEXT("猫"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
150, 70, 50, 26,
hWnd, (HMENU)10, hInst, NULL
);
checkboxFish = CreateWindow(
TEXT("button"), TEXT("鱼"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
205, 70, 50, 26,
hWnd, (HMENU)11, hInst, NULL
);
checkboxOther = CreateWindow(
TEXT("button"), TEXT("蜘蛛"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX,
260, 70, 65, 26,
hWnd, (HMENU)12, hInst, NULL
);
SendMessage(btnSubmit, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(labSex, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(radioMale, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(radioFemale, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(radioOther, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(labMarriage, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(radioMarried, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(radioSingle, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(radioSecrecy, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(labPet, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(checkboxDog, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(checkboxCat, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(checkboxFish, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage(checkboxOther, WM_SETFONT, (WPARAM)hFont, 0);
//创建分组框控件
hGroupSex = CreateWindow(
TEXT("button"), TEXT("你的性别"),
WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
20, 120, 300, 70,
hWnd, (HMENU)21, hInst, NULL
);
//接下来添加单选按钮
hRadioMale = CreateWindow(
TEXT("button"), TEXT("男"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON /*单选按钮*/,
15, 30, 50, 26,
hGroupSex /*父窗口为 GroupBox 控件*/,
(HMENU)22, hInst, NULL
);
hRadioFemale = CreateWindow(
TEXT("button"), TEXT("女"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
80, 30, 50, 26,
hGroupSex,
(HMENU)23, hInst, NULL
);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
if (wmEvent == BN_CLICKED) {
switch (wmId) {
//获取性别
case 2:
case 3:
case 4:
GetWindowText((HWND)lParam, szBufSex, 10);
break;
//获取婚姻状况
case 5:
case 6:
case 7:
GetWindowText((HWND)lParam, szBufMarriage, 10);
break;
//获取宠物
case 9:
case 10:
case 11:
case 12:
memset(szBufPet, 0, sizeof(szBufPet));//清空szBuffer内容
//是否选中狗
if (SendMessage(checkboxDog, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxDog, szBufTmp, 10);
StringCchPrintf(szBufPet,20, TEXT("%s %s"), szBufPet, szBufTmp);
}
//是否选中猫
if (SendMessage(checkboxCat, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxCat, szBufTmp, 10);
StringCchPrintf(szBufPet, 20, TEXT("%s %s"), szBufPet, szBufTmp);
}
//是否选中鱼
if (SendMessage(checkboxFish, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxFish, szBufTmp, 10);
StringCchPrintf(szBufPet, 20, TEXT("%s %s"), szBufPet, szBufTmp);
}
//是否选中其他
if (SendMessage(checkboxOther, BM_GETCHECK, 0, 0) == BST_CHECKED) {
GetWindowText(checkboxOther, szBufTmp, 10);
StringCchPrintf(szBufPet, 20, TEXT("%s %s"), szBufPet, szBufTmp);
}
break;
//点击提交按钮
case 13:
StringCchPrintf(szBufSubmit,100, TEXT("性别:%s\n婚姻状况:%s\r\n你的宠物:%s"), szBufSex, szBufMarriage, szBufPet);
MessageBox(hWnd, szBufSubmit, TEXT("信息提示"), MB_ICONINFORMATION);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}