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窗口程序 包含窗口的程序,可以通过窗 ...
随机推荐
- Nginx 504响应超时
1.问题分析 nginx访问出现504 Gateway Time-out,一般是由于程序执行时间过长导致响应超时,例如程序需要执行90秒,而nginx最大响应等待时间为30秒,这样就会出现超时. ...
- Go micro 开发微服务步骤
1.写 proto文件,定义接口和服务 2.实现 接口,注册 handle 3.调用服务:直接用rpc 客户端调用,或者用 api http web等调用 api http web 等服务可以对客户端 ...
- Java使用Jsoup获得新闻联播所有文字稿
Jsoup的maven坐标: <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> ...
- 10点睛Spring4.1-Application Event
10.1 Application Event Spring使用Application Event给bean之间的消息通讯提供了手段 应按照如下部分实现bean之间的消息通讯 继承Application ...
- Java学习,从入门到放弃(二)Linux配置mvn
其实网上的教程很多,随便拿一个,比如:https://www.cnblogs.com/chuijingjing/p/10430649.html 但在实践过程中,发现可能需要将JAVA_HOME也加到 ...
- Mstar方案软件运行基本原理
1. MApp_Main.c里有个while(1)循环: 2. 通过 while(1)循环MApp_MultiTasks 里面的 MApp_ProcessUserInput 可以 得到 当前的 u8K ...
- rtsp向rtmp推流
package com.awifi.video.media.test; import org.bytedeco.javacpp.avcodec; import org.bytedeco.javacv. ...
- redis 主从 哨兵
数据库为什么要读写分离 写代码好多年了,大家先抛弃在代码框架里面各种花哨的设计之外,写的代码到最后无非就是为了增删查改数据库.一般项目数据库刚开始只是但一个库,随着数据量的增大,就开始优化数据库(抛开 ...
- JAVA线程中的发牌题
发牌题主要考虑的就是线程的问题,一个buffer缓冲区的问题, 首先,发牌的优先级当然是最高的了,但是取牌不能有优先级,否则会一直有牌先取,因此需要一个信号量order,当order=线程的数字时,取 ...
- TypeScript TSLint(TypeScript代码检查工具)
TSLint是TypeScript代码的样式风格检查工具.类似于JavaScript的ESLint,或者Ruby的Rubocop. 配置TSLint TSLint是一个外部工具,我们需要进行一次安装工 ...