15 Windows编程——系统内置窗口子类型之button

button子类型BS_3STATE、BS_AUTO3STATE、BS_AUTOCHECKBOX

源码

  1 #include<Windows.h>
  2 #include<Windowsx.h>
  3
  4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  5
  6 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
  7 {
  8     WNDCLASS WndClass;
  9     TCHAR* ClassName = TEXT("MyClass");
 10     HWND hwnd;
 11     MSG msg;
 12
 13     WndClass.cbClsExtra = 0;
 14     WndClass.cbWndExtra = 0;
 15     WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
 16     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
 17     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 18     WndClass.hInstance = hInst;
 19     WndClass.lpfnWndProc = WindProc;
 20     WndClass.lpszClassName = ClassName;
 21     WndClass.lpszMenuName = NULL;
 22     WndClass.style = CS_VREDRAW | CS_HREDRAW;
 23
 24     if (!RegisterClass(&WndClass))
 25     {
 26         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
 27         return 0;
 28     }
 29
 30     //CreateWindow返回之前,会发送WM_CREATE消息
 31     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
 32     if (hwnd == NULL)
 33     {
 34         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
 35         return 0;
 36     }
 37     ShowWindow(hwnd, nShow);
 38     UpdateWindow(hwnd);
 39
 40     while (GetMessage(&msg, NULL, 0, 0))
 41     {
 42         TranslateMessage(&msg);
 43         DispatchMessage(&msg);
 44     }
 45
 46     return 0;
 47 }
 48
 49 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 50 {
 51     PAINTSTRUCT pt;
 52     static int check_status = 0;
 53     static HWND button,button1, button2,statichwnd, statichwnd1, statichwnd2;
 54     HDC hdc;
 55     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
 56     switch (message)
 57     {
 58     case WM_CREATE:
 59         button = CreateWindow(TEXT("button"), TEXT("BS_3STATE"), WS_CHILD | WS_VISIBLE|BS_3STATE, 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
 60         statichwnd = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)10, GetModuleHandle(NULL), NULL);
 61         button1 = CreateWindow(TEXT("button"), TEXT("BS_AUTO3STATE"), WS_CHILD | WS_VISIBLE | BS_AUTO3STATE, 0, 0, 0, 0, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL);
 62         statichwnd1 = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)20, GetModuleHandle(NULL), NULL);
 63         button2 = CreateWindow(TEXT("button"), TEXT("BS_AUTOCHECKBOX"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 0, 0, 0, 0, hwnd, (HMENU)3, GetModuleHandle(NULL), NULL);
 64         statichwnd2 = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)30, GetModuleHandle(NULL), NULL);
 65         return 0;
 66     case WM_SIZE:
 67         MoveWindow(button, 10, 10, 160, 20, TRUE);
 68         MoveWindow(statichwnd, 200, 10, 200, 20, TRUE);
 69         MoveWindow(button1, 10, 40, 160, 20, TRUE);
 70         MoveWindow(statichwnd1, 200, 40, 200, 20, TRUE);
 71         MoveWindow(button2, 10, 70, 160, 20, TRUE);
 72         MoveWindow(statichwnd2, 200, 70, 200, 20, TRUE);
 73         return 0;
 74     case WM_COMMAND:
 75         //处理button按钮,手动点选按钮
 76         if (LOWORD(wParam)==1)
 77         {
 78             switch (HIWORD(wParam))
 79             {
 80             case BN_CLICKED:
 81                 check_status++;
 82                 if (check_status % 3 == 1)
 83                 {
 84                     SendMessage((HWND)lParam, BM_SETCHECK, BST_CHECKED, NULL);
 85                     SetWindowText(statichwnd, TEXT("BST_CHECKED"));
 86                 }
 87                 else if (check_status % 3 == 2)
 88                 {
 89                     SendMessage((HWND)lParam, BM_SETCHECK, BST_INDETERMINATE, NULL);
 90                     SetWindowText(statichwnd, TEXT("BST_INDETERMINATE"));
 91                 }
 92                 else
 93                 {
 94                     SendMessage((HWND)lParam, BM_SETCHECK, BST_UNCHECKED, NULL);
 95                     SetWindowText(statichwnd, TEXT("BST_UNCHECKED"));
 96                 }
 97                 break;
 98             default:
 99                 break;
100             }
101         }
102         //处理button1按钮,自动点选按钮
103         else if (LOWORD(wParam) == 2)
104         {
105             switch (HIWORD(wParam))
106             {
107             case BN_CLICKED:
108                 if (SendMessage(button1,BM_GETCHECK,NULL,NULL)==BST_CHECKED)
109                 {
110                     SetWindowText(statichwnd1, TEXT("BST_CHECKED"));
111                 }
112                 else if (SendMessage(button1, BM_GETCHECK, NULL, NULL) == BST_INDETERMINATE)
113                 {
114                     SetWindowText(statichwnd1, TEXT("BST_INDETERMINATE"));
115                 }
116                 else
117                 {
118                     SetWindowText(statichwnd1, TEXT("BST_UNCHECKED"));
119                 }
120                 break;
121             default:
122                 break;
123             }
124         }
125         //处理button2按钮,自动点选按钮,这个按钮只有2种状态
126         else if (LOWORD(wParam) == 3)
127         {
128             switch (HIWORD(wParam))
129             {
130             case BN_CLICKED:
131                 if (SendMessage(button2, BM_GETCHECK, NULL, NULL) == BST_CHECKED)
132                 {
133                     SetWindowText(statichwnd2, TEXT("BST_CHECKED"));
134                 }
135                 else
136                 {
137                     SetWindowText(statichwnd2, TEXT("BST_UNCHECKED"));
138                 }
139                 break;
140             default:
141                 break;
142             }
143         }
144         return 0;
145     case WM_DESTROY:
146         PostQuitMessage(0);
147         return 0;
148     default:
149         break;
150     }
151
152     return DefWindowProc(hwnd, message, wParam, lParam);
153 }

运行结果

button子类型BS_AUTORADIOBUTTON、BS_GROUPBOX、BS_DEFPUSHBUTTON

源码

  1 #include<Windows.h>
  2 #include<WinUser.h>
  3 #include<tchar.h>
  4 #include<stdio.h>
  5
  6 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  7
  8 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
  9 {
 10     WNDCLASS WndClass;
 11     TCHAR* ClassName = TEXT("MyClass");
 12     HWND hwnd;
 13     MSG msg;
 14
 15     WndClass.cbClsExtra = 0;
 16     WndClass.cbWndExtra = 0;
 17     WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
 18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
 19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 20     WndClass.hInstance = hInst;
 21     WndClass.lpfnWndProc = WindProc;
 22     WndClass.lpszClassName = ClassName;
 23     WndClass.lpszMenuName = NULL;
 24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
 25
 26     if (!RegisterClass(&WndClass))
 27     {
 28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
 29         return 0;
 30     }
 31
 32     //CreateWindow返回之前,会发送WM_CREATE消息
 33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 800, NULL, NULL, hInst, NULL);
 34     if (hwnd == NULL)
 35     {
 36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
 37         return 0;
 38     }
 39     ShowWindow(hwnd, nShow);
 40     UpdateWindow(hwnd);
 41
 42     while (GetMessage(&msg, NULL, 0, 0))
 43     {
 44         TranslateMessage(&msg);
 45         DispatchMessage(&msg);
 46     }
 47
 48     return 0;
 49 }
 50
 51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 52 {
 53     PAINTSTRUCT pt;
 54     static int check_status = 0;
 55     HDC hdc;
 56     static HWND radio1[3], radio2[3];
 57     static HWND defpushbutton;
 58     static HWND shwnd;
 59     static HWND group1, group2;
 60     TCHAR *r_str1 [3] = {
 61         TEXT("男人"),TEXT("女人"),TEXT("人妖")};
 62     TCHAR *r_str2[3] = {
 63         TEXT("已婚"),TEXT("未婚"),TEXT("热恋") };
 64     TCHAR buf[100];
 65     int i;
 66     static int sex = 0;
 67     static int marry = 0;
 68     switch (message)
 69     {
 70     case WM_CREATE:
 71         for (i = 0; i < 3; i++)
 72         {
 73             radio1[i]= CreateWindow(TEXT("button"), r_str1[i], WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 0, 0, 0, 0, hwnd, (HMENU)(100+i), GetModuleHandle(NULL), NULL);
 74         }
 75         for (i = 0; i < 3; i++)
 76         {
 77             radio2[i] = CreateWindow(TEXT("button"), r_str2[i], WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 0, 0, 0, 0, hwnd, (HMENU)(200 + i), GetModuleHandle(NULL), NULL);
 78         }
 79         group1= CreateWindow(TEXT("button"), TEXT("性别"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 0, 0, 0, 0, hwnd, (HMENU)(300), GetModuleHandle(NULL), NULL);
 80         group2 = CreateWindow(TEXT("button"), TEXT("婚姻"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 0, 0, 0, 0, hwnd, (HMENU)(400), GetModuleHandle(NULL), NULL);
 81
 82         defpushbutton = CreateWindow(TEXT("button"), TEXT("确定"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 0, 0, 0, 0, hwnd, (HMENU)(500), GetModuleHandle(NULL), NULL);
 83         shwnd = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE , 0, 0, 0, 0, hwnd, (HMENU)(600), GetModuleHandle(NULL), NULL);
 84         return 0;
 85     case WM_SIZE:
 86         for (i = 0; i < 3; i++)
 87         {
 88             MoveWindow(radio1[i], 10, 40+i*30, 85, 20, TRUE);
 89         }
 90         for (i = 0; i < 3; i++)
 91         {
 92             MoveWindow(radio2[i], 10, 160 + i * 30, 85, 20, TRUE);
 93         }
 94         MoveWindow(group1, 5, 10, 110, 120, TRUE);
 95         MoveWindow(group2, 5, 130, 110, 120, TRUE);
 96         SetWindowLong(radio1[0], GWL_STYLE, (LONG)WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP);
 97         SetWindowLong(radio2[0], GWL_STYLE, (LONG)WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | WS_GROUP);
 98
 99         MoveWindow(defpushbutton, 20, 260, 70, 20, TRUE);
100         MoveWindow(shwnd, 200, 10, 200, 235, TRUE);
101         return 0;
102     case WM_COMMAND:
103         //HIWORD(wParam)控件通知码     lParam控件的窗口句柄
104         if (HIWORD(wParam) == BN_CLICKED && (LPARAM)defpushbutton !=lParam)
105         {
106             if ((LPARAM)radio1[0] == lParam)
107             {
108                 sex = 1;
109             }
110             else if ((LPARAM)radio1[1] == lParam)
111             {
112                 sex = 2;
113             }
114             else if ((LPARAM)radio1[2] == lParam)
115             {
116                 sex = 3;
117             }
118
119             if ((LPARAM)radio2[0] == lParam)
120             {
121                 marry = 1;
122             }
123             else if ((LPARAM)radio2[1] == lParam)
124             {
125                 marry = 2;
126             }
127             else if ((LPARAM)radio2[2] == lParam)
128             {
129                 marry = 3;
130             }
131         }
132         if ((LPARAM)defpushbutton == lParam)
133         {
134             if (sex == 0 | marry == 0)
135             {
136                 _stprintf(buf, TEXT("请选择性别或婚姻状态"));
137                 SetWindowText(shwnd, buf);
138             }
139             else
140             {
141                 _stprintf(buf, TEXT("我是%s,我%s"), r_str1[sex - 1], r_str2[marry - 1]);
142                 SetWindowText(shwnd, buf);
143             }
144         }
145         return 0;
146     case WM_DESTROY:
147         PostQuitMessage(0);
148         return 0;
149     default:
150         break;
151     }
152
153     return DefWindowProc(hwnd, message, wParam, lParam);
154 }

运行结果:

原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9343211.html

时间: 2024-10-14 02:31:23

15 Windows编程——系统内置窗口子类型之button的相关文章

16 Windows编程——系统内置窗口子类型之edit、ComboBox

edit类型的子窗口 ES_MULTILINE:多行输入文本框 窗口的消息: WL_COMMAND: EN_CHANGE:当edit窗口内的文本内容改变的时候,edit子窗口给父窗口发送一个WL_COMMAND消息,其通知码是EN_CHANGE. WM_GETTEXT: 用SendMessage函数给edit窗口发送这个消息,同时,给wparam参数传递我们想要获得的字符个数,lParam传递一个内存块地址,sendMessage函数返回后,lParam指向的内存中,将保存获得的字符. WL_G

走进windows编程的世界-----窗口的注册及创建

1   窗口注册和创建 1.1WIN32 窗口程序创建步骤 1.WinMain入口函数的定义 2.WindowProc函数的定义 3.注册窗口类 RegisterClass.RegisterClassEX 4.创建窗口 CreateWindow.CreateWindowEx HWND CreateWindow( LPCTSTRlpClassName,//指向已注册的窗口类的名称的指针 LPCTSTRlpWindowName,//指向窗口名称的指针 DWORDdwStyle,//窗口的风格 int

Python学习15之python内置六大标准类型

1.六大标准类型:数值型,str,list,set,tuple,dic 2.数值型:int,float,bool,complex 3.区别: 1)数值型和str,tuple都是不可变类型 而list,set,dict值可变类型 2)数值,str是标量类型 str,list,tuple,set,dict是容器类型: 3)容器类型又分为序列类型和哈希类型 序列类型内部元素存储按线性顺序存储,因此可通过索引,下标访问,有list,string,tuple 哈希类型,内部元素是按照哈希算法存储的,没有索

Windows 编程中恼人的各种字符以及字符指针类型

在Windows编程中,很容易见到这些数据类型:LPSTR,LPTSTR,LPCTSTR... 像很多童鞋一样,当初在学Windows编程的时候,对着些数据类型真的是丈二和尚,摸不着头脑,长时间不用就会想不起来.所以,下面就对此做一个总结. 首先,先来看一下ASCII编码和UNICODE编码的区别.下面是<Windows 程序设计>一书中对Unicode编码的说明: 简单地说,Unicode扩展自ASCII字符集.在严格的ASCII中,每个字符用7位表示,或者计算机上普遍使用的每字符有8位宽:

windows系统内置ftp客户端和相关命令

目的:学会使用windows系统内置ftp客户端和相关命令. 环境:windows 7 情景:多个设备需要备份它们的配置文件,设置为明天自动备份,以FTP服务下载到服务器,并以日期保存. 这里需要使用FTP服务,这个服务一般系统都自带有,可以使用命令模式,或写成批处理脚本运行. ftp命令支持交互和执行配置操作: 有如下参数: -v              禁止显示远程服务器响应. -n              禁止在初始连接时自动登录. -i              关闭多文件传输过程中

设置windows窗口ICON 【windows 编程】【API】【原创】

1. ICON介绍 最近开始接触windows 编程,因此将自己所接触的一些零散的知识进行整理并记录.本文主要介绍了如何更改windows对话框窗口的ICON图标.这里首先介绍一下windows ICON定义.在我们使用的windows APP上,一般都存在两个ICON.一个是打开APP后显示与左上角的小的图标文件(SMALL ICON)以及按下Alt+Tab切换窗口时显示的一个大的图标文件(BIG ICON).以windows自带的记事本程序为例,小的图标文件如下图1所示: 图1 记事本程序的

Flink实战(七) - Time &amp; Windows编程

0 相关源码 掌握Flink中三种常用的Time处理方式,掌握Flink中滚动窗口以及滑动窗口的使用,了解Flink中的watermark. Flink 在流处理工程中支持不同的时间概念. 1 处理时间(Processing time) 执行相应算子操作的机器的系统时间. 当流程序在处理时间运行时,所有基于时间的 算子操作(如时间窗口)将使用运行相应算子的机器的系统时钟.每小时处理时间窗口将包括在系统时钟指示整个小时之间到达特定算子的所有记录. 例如,如果应用程序在上午9:15开始运行,则第一个

Win32 Windows编程 十二

一.对话框 1.对话框的分类 2.对话框的基本使用方式 3.对话框资源 4.有模式对话框的使用 5. 无模式对话框的使用 5.1 添加对话框资源 5.2 定义窗口处理函数 BOOL CALLBACK DialogProc( HWND hWnd, UINT UMsg, WPARAM wParam, LPARAM lParam ) 5.3 创建对话框 HWND CreateDialog( HINSTANCE hInstance, // handle to module LPCTSTR lpTempl

Win32 Windows编程 五

一 Win32消息机制 1 消息机制 过程驱动:程序是按照我们预先定义好的顺序执行,每执行一步,下一步都已经按照预定的顺序继续执行,直到程序结束 事件驱动: 程序的执行顺序是无序的.某个事件点所执行的代码,是由外界通知.由于我们无法决定用户执行顺序,所以代码的执行也是无序 Win32的消息机制 - 事件驱动 2 Win32消息程序 2.1 Win32 窗口注册 2.2 Win32窗口创建 2.3 Win32消息循环 2.3.1 GetMessage BOOL GetMessage( LPMSG