15 Windows编程——系统内置窗口子类型之button
button子类型BS_3STATE、BS_AUTO3STATE、BS_AUTOCHECKBOX
源码
#include<Windows.h>
#include<Windowsx.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg; WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + );
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int check_status = ;
static HWND button,button1, button2,statichwnd, statichwnd1, statichwnd2;
HDC hdc;
HBRUSH hBrush = CreateSolidBrush(RGB(, , ));
switch (message)
{
case WM_CREATE:
button = CreateWindow(TEXT("button"), TEXT("BS_3STATE"), WS_CHILD | WS_VISIBLE|BS_3STATE, , , , , hwnd, (HMENU), GetModuleHandle(NULL), NULL);
statichwnd = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, , , , , hwnd, (HMENU), GetModuleHandle(NULL), NULL);
button1 = CreateWindow(TEXT("button"), TEXT("BS_AUTO3STATE"), WS_CHILD | WS_VISIBLE | BS_AUTO3STATE, , , , , hwnd, (HMENU), GetModuleHandle(NULL), NULL);
statichwnd1 = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, , , , , hwnd, (HMENU), GetModuleHandle(NULL), NULL);
button2 = CreateWindow(TEXT("button"), TEXT("BS_AUTOCHECKBOX"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, , , , , hwnd, (HMENU), GetModuleHandle(NULL), NULL);
statichwnd2 = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, , , , , hwnd, (HMENU), GetModuleHandle(NULL), NULL);
return ;
case WM_SIZE:
MoveWindow(button, , , , , TRUE);
MoveWindow(statichwnd, , , , , TRUE);
MoveWindow(button1, , , , , TRUE);
MoveWindow(statichwnd1, , , , , TRUE);
MoveWindow(button2, , , , , TRUE);
MoveWindow(statichwnd2, , , , , TRUE);
return ;
case WM_COMMAND:
//处理button按钮,手动点选按钮
if (LOWORD(wParam)==)
{
switch (HIWORD(wParam))
{
case BN_CLICKED:
check_status++;
if (check_status % == )
{
SendMessage((HWND)lParam, BM_SETCHECK, BST_CHECKED, NULL);
SetWindowText(statichwnd, TEXT("BST_CHECKED"));
}
else if (check_status % == )
{
SendMessage((HWND)lParam, BM_SETCHECK, BST_INDETERMINATE, NULL);
SetWindowText(statichwnd, TEXT("BST_INDETERMINATE"));
}
else
{
SendMessage((HWND)lParam, BM_SETCHECK, BST_UNCHECKED, NULL);
SetWindowText(statichwnd, TEXT("BST_UNCHECKED"));
}
break;
default:
break;
}
}
//处理button1按钮,自动点选按钮
else if (LOWORD(wParam) == )
{
switch (HIWORD(wParam))
{
case BN_CLICKED:
if (SendMessage(button1,BM_GETCHECK,NULL,NULL)==BST_CHECKED)
{
SetWindowText(statichwnd1, TEXT("BST_CHECKED"));
}
else if (SendMessage(button1, BM_GETCHECK, NULL, NULL) == BST_INDETERMINATE)
{
SetWindowText(statichwnd1, TEXT("BST_INDETERMINATE"));
}
else
{
SetWindowText(statichwnd1, TEXT("BST_UNCHECKED"));
}
break;
default:
break;
}
}
//处理button2按钮,自动点选按钮,这个按钮只有2种状态
else if (LOWORD(wParam) == )
{
switch (HIWORD(wParam))
{
case BN_CLICKED:
if (SendMessage(button2, BM_GETCHECK, NULL, NULL) == BST_CHECKED)
{
SetWindowText(statichwnd2, TEXT("BST_CHECKED"));
}
else
{
SetWindowText(statichwnd2, TEXT("BST_UNCHECKED"));
}
break;
default:
break;
}
}
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
}
运行结果

button子类型BS_AUTORADIOBUTTON、BS_GROUPBOX、BS_DEFPUSHBUTTON
源码
#include<Windows.h>
#include<WinUser.h>
#include<tchar.h>
#include<stdio.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
{
WNDCLASS WndClass;
TCHAR* ClassName = TEXT("MyClass");
HWND hwnd;
MSG msg; WndClass.cbClsExtra = ;
WndClass.cbWndExtra = ;
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + );
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInst;
WndClass.lpfnWndProc = WindProc;
WndClass.lpszClassName = ClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_VREDRAW | CS_HREDRAW; if (!RegisterClass(&WndClass))
{
MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
return ;
} //CreateWindow返回之前,会发送WM_CREATE消息
hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, , , NULL, NULL, hInst, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
return ;
}
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return ;
} LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT pt;
static int check_status = ;
HDC hdc;
static HWND radio1[], radio2[];
static HWND defpushbutton;
static HWND shwnd;
static HWND group1, group2;
TCHAR *r_str1 [] = {
TEXT("男人"),TEXT("女人"),TEXT("人妖")};
TCHAR *r_str2[] = {
TEXT("已婚"),TEXT("未婚"),TEXT("热恋") };
TCHAR buf[];
int i;
static int sex = ;
static int marry = ;
switch (message)
{
case WM_CREATE:
for (i = ; i < ; i++)
{
radio1[i]= CreateWindow(TEXT("button"), r_str1[i], WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, , , , , hwnd, (HMENU)(+i), GetModuleHandle(NULL), NULL);
}
for (i = ; i < ; i++)
{
radio2[i] = CreateWindow(TEXT("button"), r_str2[i], WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, , , , , hwnd, (HMENU)( + i), GetModuleHandle(NULL), NULL);
}
group1= CreateWindow(TEXT("button"), TEXT("性别"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX, , , , , hwnd, (HMENU)(), GetModuleHandle(NULL), NULL);
group2 = CreateWindow(TEXT("button"), TEXT("婚姻"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX, , , , , hwnd, (HMENU)(), GetModuleHandle(NULL), NULL); defpushbutton = CreateWindow(TEXT("button"), TEXT("确定"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, , , , , hwnd, (HMENU)(), GetModuleHandle(NULL), NULL);
shwnd = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE , , , , , hwnd, (HMENU)(), GetModuleHandle(NULL), NULL);
return ;
case WM_SIZE:
for (i = ; i < ; i++)
{
MoveWindow(radio1[i], , +i*, , , TRUE);
}
for (i = ; i < ; i++)
{
MoveWindow(radio2[i], , + i * , , , TRUE);
}
MoveWindow(group1, , , , , TRUE);
MoveWindow(group2, , , , , TRUE);
SetWindowLong(radio1[], GWL_STYLE, (LONG)WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP);
SetWindowLong(radio2[], GWL_STYLE, (LONG)WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP); MoveWindow(defpushbutton, , , , , TRUE);
MoveWindow(shwnd, , , , , TRUE);
return ;
case WM_COMMAND:
//HIWORD(wParam)控件通知码 lParam控件的窗口句柄
if (HIWORD(wParam) == BN_CLICKED && (LPARAM)defpushbutton !=lParam)
{
if ((LPARAM)radio1[] == lParam)
{
sex = ;
}
else if ((LPARAM)radio1[] == lParam)
{
sex = ;
}
else if ((LPARAM)radio1[] == lParam)
{
sex = ;
} if ((LPARAM)radio2[] == lParam)
{
marry = ;
}
else if ((LPARAM)radio2[] == lParam)
{
marry = ;
}
else if ((LPARAM)radio2[] == lParam)
{
marry = ;
}
}
if ((LPARAM)defpushbutton == lParam)
{
if (sex == | marry == )
{
_stprintf(buf, TEXT("请选择性别或婚姻状态"));
SetWindowText(shwnd, buf);
}
else
{
_stprintf(buf, TEXT("我是%s,我%s"), r_str1[sex - ], r_str2[marry - ]);
SetWindowText(shwnd, buf);
}
}
return ;
case WM_DESTROY:
PostQuitMessage();
return ;
default:
break;
} return DefWindowProc(hwnd, message, wParam, lParam);
}
运行结果:

15 Windows编程——系统内置窗口子类型之button的更多相关文章
- 16 Windows编程——系统内置窗口子类型之edit、ComboBox、ownerbutton、listbox
edit类型的子窗口 ES_MULTILINE:多行输入文本框 窗口的消息: WL_COMMAND: EN_CHANGE:当edit窗口内的文本内容改变的时候,edit子窗口给父窗口发送一个WL_CO ...
- 13 Windows编程——系统内置窗口子类型之静态子窗口
静态子窗口类型 wndclass:static 源码 #include<Windows.h> #include<Windowsx.h> HINSTANCE G_h; LRESU ...
- 12 Windows编程——子窗口和系统内置窗口类“BUTTON”
创建子窗口类,使得子窗口有自己的处理过程. 子窗口类型WS_CHILD不能和WS_POPUP一起使用!为什么子窗口要有自己的处理过程?如果使用主窗口类来创建子窗口,那么子窗口和主窗口将公用窗口处理过程 ...
- 学习windows编程 day3 之窗口绘画一:点线绘制
#include <windows.h> #include <math.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, ...
- Windows编程___创建窗口
创建Windows窗口不难,可以简要的概括为: 1,# 注册一个窗口类 填充WNDCLASS结构 书写窗口消息处理函数WinProc 2,# 创建一个窗口 填写基本的窗口信息 3,# 显示窗口 4,# ...
- 学习windows编程 day3 之窗口绘画二:边框绘制函数
#include <windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM l ...
- Python学习15之python内置六大标准类型
1.六大标准类型:数值型,str,list,set,tuple,dic 2.数值型:int,float,bool,complex 3.区别: 1)数值型和str,tuple都是不可变类型 而list, ...
- windows 编程 —— 子窗口 与 子窗口控件
目录: 子窗口与主窗口的交互 子窗口控件 按钮类别 button 滚动条类别 scrollbar 静态类别 static 编辑框类别 edit 清单方块 listbox 子窗口与主窗口的交互 创建窗 ...
- 【Windows编程】入门篇——win 32窗口的hello word!
✍ Windows编程基础 1.Win 32应用程序基本类型 1) 控制台程序 不需要完善的windows窗口,可以使用DOS窗口方式显示 2) Win 32窗口程序 包含窗口的程序,可以通过窗 ...
随机推荐
- Cas(03)——Cas Server中各配置文件介绍
Cas Server中各配置文件介绍 Cas Server中所有的配置文件都是放在WEB-INF目录及其子目录下的. 在WEB-INF/classes下的配置文件有: l cas-theme-def ...
- Node.js学习笔记(4):Yarn简明教程
Node.js学习笔记(4):Yarn简明教程. 引入Yarn NPM是常用的包管理工具,现在我们引入是新一代的包管理工具Yarn.其具有快速.安全.可靠的特点. 安装方式 使用npm工具安装yarn ...
- mysql开启缓存、设置缓存大小、缓存过期机制
目录 一.开启缓存 1.修改配置文件my.ini 2.命令方式 二.查看是否生效 1.query_cache_type 使用查询缓存的方式 2.have_query_cache 设置查询缓存是否可用 ...
- Vue + ElementUI的电商管理系统实例03 用户列表
1.通过路由展示用户列表页 新建user文件夹,里面新建Users.vue文件: <template> <div> <h3>用户列表组件</h3> &l ...
- Spring的定时任务@Scheduled(cron = "0 0 1 * * *")
指定某个方法在特定时间执行,如: cron="0 0 1 1 * ?" 即这个方法每月1号凌晨1点执行一次 关于这个注解的解释网上一大堆 但是今天遇到个问题,明明加了注解@Sche ...
- 理解Hybrid接口的应用
Hybrid接口既可以连接普通终端的接入链路又可以连接交换机间的干道链路,特允许多个vlan的帧通过,并可以在出接口方向将某些vlan帧的标签剥掉. 通过配置Hybrid接口,能过实现对VLAN标签的 ...
- 专业仿百度百科,维基wiki百科网站开发建设
专业仿百度百科,维基wiki百科网站开发建设,有需要的朋友可以欢迎私聊我 提供一站式服务:联系QQ:8582-36016(私聊),微信:lianweikj 电话:186-7597-7935 支持终端: ...
- Linux基础-12-yum管理软件包
1. yum的功能 yum是Yellow dog Updater, Modified的缩写,目的就是为了解决RPM的依赖关系的问题,方便使用者进行软件的安装.升级等等工作. 2. 光盘挂载和镜像挂载 ...
- 基于TCP的编程
前提:本文基于Linux系统下的学习 服务器端 1 创建通讯端口,返回socket设备的文件描述符 sfdsocket(2)#include <sys/types.h> /* See NO ...
- 『Python基础』第8节:格式化输出
现在有一个需求, 询问用户的姓名, 年龄, 工作, 爱好, 然后打印成以下格式 ************ info of Conan ************ name: Conan age: 23 ...