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 }