大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
Windows单按钮、复选框、分组框控件
单选按钮(Radio Button)和复选框(Check Box)是常见的Windows控件,用于从给出的选项中选择一项或多项,如下图所示:
单选钮与复选框
单选按钮是互斥的,只能选择其中一项;而复选框没有限制,可以选择一项或多项。
单选按钮和复选框都是一种特殊的按钮,窗口类名称都是button,只不过增加了一些特殊的窗口样式罢了。单选按钮的样式为BS_AUTORADIOBUTTON,复选框的样式为BS_AUTOCHECKBOX。例如:
//创建一个单选按钮
CreateWindow(
TEXT("button"), TEXT("单选按钮"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON/*单选按钮*/,
235, 40, 100, 26,
hWnd, (HMENU)7, hInst, NULL
);
//创建一个复选框
CreateWindow(
TEXT("button"), TEXT("复选框"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTOCHECKBOX/*复选框*/,
95, 70, 50, 26,
hWnd, (HMENU)9, hInst, NULL
);
如上图所示,单选按钮一般是分组的,一组单选按钮之间是互斥的,只能选择其中一个。一个窗口可以包含多组单选按钮。
要想为单选按钮分组,可以增加WS_GROUP样式。设置了 WS_GROUP 样式的单选框为一组中的首元素,随后的所有单选按钮都和它在同一组,直到下一个设置了 WS_GROUP 样式的单选按钮。
下面的代码,将实现上图中所示的单选按钮和复选框:
//窗口过程
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 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; //复选框--其他
switch (message){
case WM_CREATE:
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)2, hInst, NULL
);
//选择婚姻状况
labMarriage = CreateWindow(
TEXT("static"), TEXT("婚姻状况:"),
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_RIGHT,
10, 40, 80, 26,
hWnd, (HMENU)4, 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)8, 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)11, hInst, NULL
);
SendMessage(labSex, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioMale, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioFemale, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(labMarriage, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioMarried, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioSingle, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(radioSecrecy, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(labPet, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxDog, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxCat, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxFish, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(checkboxOther, WM_SETFONT, (WPARAM)hFont, NULL);
break;
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;
}
分组框控件
使用分组框控件(GroupBox)可以对单选按钮、复选框进行更好的分组,如下图所示:
分组框控件也是一种特殊的按钮,它的样式为BS_GROUPBOX,例如:
//创建分组框控件
hGroupSex = CreateWindow(
TEXT("button"), TEXT("你的性别"),
WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
20, 20, 300, 70,
hWnd, (HMENU)1, hInst, NULL
);
//接下来添加单选按钮
hRadioMale = CreateWindow(
TEXT("button"), TEXT("男"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON /*单选按钮*/,
15, 30, 50, 26,
hGroupSex /*父窗口为 GroupBox 控件*/, (HMENU)2, hInst, NULL
);
hRadioFemale = CreateWindow(
TEXT("button"), TEXT("女"),
WS_CHILD | WS_VISIBLE | BS_LEFT | BS_AUTORADIOBUTTON,
80, 30, 50, 26,
hGroupSex, (HMENU)3, hInst, NULL
);
该代码将会创建上图中的第一组按钮。
需要注意的是:单选按钮的父窗口为分组框控件 hGroupSex,而不是顶级窗口 hWnd。
完整的示例代码较长,就不在这里展示了,如有需要,请继续查看本节完整代码。
链接:https://blog.csdn.net/weixin_45525272/article/details/104332539
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/170967.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...