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);
}

运行结果

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

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-LMLPHP

05-11 22:02