Fork me on GitHub

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 }
View Code

运行结果

 

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 }
View Code

运行结果:

 

posted @ 2018-07-20 19:23  克拉默与矩阵  阅读(741)  评论(0编辑  收藏  举报